This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gold patch 8/8] Add framework for incremental updates.


This patch adds the framework for doing the incremental update itself.
It's a big patch, but a lot of it is boilerplate from adding new classes
derived from Relobj and Dynobj to represent unchanged objects in an
incremental link.

I added a serial number to each input argument, and record the serial
number in the incremental info for each input file, in order to apply the
--incremental-changed/unchanged disposition reliably.

I also added a bit flag to the global symbol lists for input objects
indicating whether the symbol was defined in that object or not.

For now, I have disabled the .eh_frame_hdr generation and optimization
of the .eh_frame sections. In order to restore that, I may need to
record extra relocations for those sections.

I'm still working on a few more things before this is ready for use:
- Insertion of padding.
- Getting the right soname for shared libraries.
- Handling local symbols with dynamic relocations.
- Copying and applying relocations from unchanged inputs.
- String merge sections.
- COMDAT group handling.
- Symbol versions.
- Fill holes in .debug_info and .eh_frame sections.

-cary


	* archive.cc (Archive::include_member): Adjust call to report_object.
	(Add_archive_symbols::run): Track serial number of input argument.
	(Lib_group::include_member): Likewise.
	(Add_lib_group_symbols::run): Adjust call to report_object.
	* archive.h (Archive_member::arg_serial_): New data member.
	* dynobj.cc (Dynobj::Dynobj): Allow for NULL input_file.
	(Sized_dynobj::do_add_symbols): Track symbols for incremental links.
	(Sized_dynobj::do_for_all_local_got_entries): New function.
	* dynobj.h (Sized_dynobj::do_for_all_local_got_entries): New function.
	* fileread.cc (get_mtime): New function.
	* fileread.h (get_mtime): New function declaration.
	* gold.cc (queue_initial_tasks): Setup for incremental linking.
	(process_incremental_input): New function.
	(queue_middle_tasks): Add check for incremental update.
	* incremental-dump.cc (dump_incremental_inputs): Print serial numbers
	of input arguments.  Print more info for symbols.
	* incremental.cc (Output_section_incremental_inputs::update_data_size):
	New function.
	(Sized_incremental_binary::setup_readers): Setup readers for input
	files and maps of files added from libraries and scripts.
	(Sized_incremental_binary::check_input_args): New function.
	(Sized_incremental_binary::do_check_inputs): Check for changed script
	files.
	(Sized_incremental_binary::do_get_input_reader): Remove.
	(Sized_incremental_binary::do_file_has_changed): New function.
	(Sized_incremental_binary::do_init_layout): New function.
	(Sized_incremental_binary::do_reserve_layout): New function.
	(Sized_incremental_binary::get_symtab_view): New function.
	(Incremental_checker::can_incrementally_link_output_file): Remove.
	(Incremental_inputs::report_archive_begin): Add serial number of
	input argument.
	(Incremental_inputs::report_object): Likewise.
	(Incremental_inputs::report_script): Likewise.
	(Output_section_incremental_inputs::set_final_data_size): Count
	symbols from shared libraries.
	(Output_section_incremental_inputs::write_input_files): Write serial
	numbers for input file arguments.
	(Output_section_incremental_inputs::write_info_blocks): Map section
	index to original input section index.  Record def/undef flag for
	each global symbol.  Omit global symbols from shared libraries that
	are not used in the output symbol table.
	(Local_got_offset_visitor): Derive from new base class.  Change from
	functor to a visit callback.
	(Global_got_offset_visitor): Likewise.
	(Output_section_incremental_inputs::write_got_plt): Call new
	local GOT entry visitor instead of iterating over symbols.
	(Sized_incr_relobj::do_read_symbols): New function.
	(Sized_incr_relobj::do_layout): New function.
	(Sized_incr_relobj::do_layout_deferred_sections): New function.
	(Sized_incr_relobj::do_add_symbols): New function.
	(Sized_incr_relobj::do_should_include_member): New function.
	(Sized_incr_relobj::do_for_all_global_symbols): New function.
	(Sized_incr_relobj::do_for_all_local_got_entries): New function.
	(Sized_incr_relobj::do_section_size): New function.
	(Sized_incr_relobj::do_section_name): New function.
	(Sized_incr_relobj::do_section_contents): New function.
	(Sized_incr_relobj::do_section_flags): New function.
	(Sized_incr_relobj::do_section_entsize): New function.
	(Sized_incr_relobj::do_section_address): New function.
	(Sized_incr_relobj::do_section_type): New function.
	(Sized_incr_relobj::do_section_link): New function.
	(Sized_incr_relobj::do_section_info): New function.
	(Sized_incr_relobj::do_section_addralign): New function.
	(Sized_incr_relobj::do_initialize_xindex): New function.
	(Sized_incr_relobj::do_get_global_symbol_counts): New function.
	(Sized_incr_relobj::do_read_relocs): New function.
	(Sized_incr_relobj::do_gc_process_relocs): New function.
	(Sized_incr_relobj::do_scan_relocs): New function.
	(Sized_incr_relobj::do_count_local_symbols): New function.
	(Sized_incr_relobj::do_finalize_local_symbols): New function.
	(Sized_incr_relobj::do_set_local_dynsym_indexes): New function.
	(Sized_incr_relobj::do_relocate): New function.
	(Sized_incr_relobj::do_set_section_offset): New function.
	(Sized_incr_dynobj::do_read_symbols): New function.
	(Sized_incr_dynobj::do_layout): New function.
	(Sized_incr_dynobj::do_add_symbols): New function.
	(Sized_incr_dynobj::do_should_include_member): New function.
	(Sized_incr_dynobj::do_for_all_global_symbols): New function.
	(Sized_incr_dynobj::do_for_all_local_got_entries): New function.
	(Sized_incr_dynobj::do_section_size): New function.
	(Sized_incr_dynobj::do_section_name): New function.
	(Sized_incr_dynobj::do_section_contents): New function.
	(Sized_incr_dynobj::do_section_flags): New function.
	(Sized_incr_dynobj::do_section_entsize): New function.
	(Sized_incr_dynobj::do_section_address): New function.
	(Sized_incr_dynobj::do_section_type): New function.
	(Sized_incr_dynobj::do_section_link): New function.
	(Sized_incr_dynobj::do_section_info): New function.
	(Sized_incr_dynobj::do_section_addralign): New function.
	(Sized_incr_dynobj::do_initialize_xindex): New function.
	(Sized_incr_dynobj::do_get_global_symbol_counts): New function.
	(make_sized_incremental_object): New function.
	(Incremental_library::copy_unused_symbols): New function.
	(Incremental_library::do_for_all_unused_symbols): New function.
	* incremental.h (Incremental_checker): Remove class.
	(Incremental_input_entry::arg_serial): New function.
	(Incremental_input_entry::arg_serial_): New data member.  Adjust ctor.
	(Script_info): Move class here from script.h.
	(Script_info::filename): New function.
	(Script_info::filename_): New data member.
	(Incremental_script_entry): Add arg_serial parameter to ctor; adjust
	all callers.
	(Incremental_object_entry): Likewise.
	(Incremental_object_entry::add_input_section): Push new input section
	onto list.
	(Incremental_object_entry::get_input_section_index): New function.
	(Incremental_object_entry::Input_section): Add constructor; rename
	data members; adjust callers.
	(Incremental_archive_entry): Add arg_serial parameter to ctor; adjust
	all callers.
	(Incremental_inputs::report_archive_begin): Add arg_serial parameter.
	(Incremental_inputs::report_object): Likewise.
	(Incremental_inputs::report_script): Likewise.
	(Incremental_inputs_reader::arg_serial): New function.
	(Incremental_inputs_reader::Global_symbol_info::is_def): New data
	member.
	(Incremental_inputs_reader::get_global_symbol_info): Add shared library
	case.
	(Incremental_inputs_reader::input_file_offset): New function.
	(Incremental_inputs_reader::input_file_index): New function.
	(Incremental_inputs_reader::input_file): Rewrite.
	(Incremental_inputs_reader::input_file_at_offset): New function.
	(Incremental_binary::check_inputs): Add cmdline parameter.
	(Incremental_binary::Input_reader::arg_serial): New function.
	(Incremental_binary::Input_reader::get_unused_symbol_count): New
	function.
	(Incremental_binary::Input_reader::get_unused_symbol): New function.
	(Incremental_binary::Input_reader::do_arg_serial): New function.
	(Incremental_binary::Input_reader::do_get_unused_symbol_count): New
	function.
	(Incremental_binary::Input_reader::do_get_unused_symbol): New function.
	(Incremental_binary::get_input_reader): Remove.
	(Incremental_binary::input_file_count): New function.
	(Incremental_binary::get_input_reader): New function.
	(Incremental_binary::file_has_changed): New function.
	(Incremental_binary::get_input_argument): New function.
	(Incremental_binary::get_library): New function.
	(Incremental_binary::get_script_info): New function.
	(Incremental_binary::init_layout): New function.
	(Incremental_binary::reserve_layout): New function.
	(Incremental_binary::output_file): New function.
	(Incremental_binary::do_check_inputs): Add cmdline parameter.
	(Incremental_binary::do_file_has_changed): New function.
	(Incremental_binary::do_init_layout): New function.
	(Incremental_binary::do_reserve_layout): New function.
	(Incremental_binary::do_input_file_count): New function.
	(Incremental_binary::do_get_input_reader): Change signature.
	(Incremental_binary::input_args_map_): New data member.
	(Incremental_binary::library_map_): New data member.
	(Incremental_binary::script_map_): New data member.
	(Sized_incremental_binary::output_section): New function.
	(Sized_incremental_binary::inputs_reader): Add const.
	(Sized_incremental_binary::symtab_reader): Add const.
	(Sized_incremental_binary::relocs_reader): Add const.
	(Sized_incremental_binary::got_plt_reader): Add const.
	(Sized_incremental_binary::get_symtab_view): New function.
	(Sized_incremental_binary::Inputs_reader): New typedef.
	(Sized_incremental_binary::Input_entry_reader): New typedef.
	(Sized_incremental_binary::do_check_inputs): Add cmdline parameter.
	(Sized_incremental_binary::do_file_has_changed): New function.
	(Sized_incremental_binary::do_init_layout): New function
	(Sized_incremental_binary::do_reserve_layout): New function
	(Sized_incremental_binary::Sized_input_reader): Remove typedefs.
	(Sized_incremental_binary::Sized_input_reader::do_arg_serial): New
	function.
	(Sized_incremental_binary::Sized_input_reader::
	do_get_unused_symbol_count): New function.
	(Sized_incremental_binary::Sized_input_reader::do_get_unused_symbol):
	New function.
	(Sized_incremental_binary::do_get_input_reader): Change signature.
	(Sized_incremental_binary::do_input_file_count): New function.
	(Sized_incremental_binary::section_map_): New data member.
	(Sized_incremental_binary::input_entry_readers_): New data member.
	(Sized_incr_relobj): New class.
	(Sized_incr_dynobj): New class.
	(make_sized_incremental_object): New function.
	(Incremental_library): New class.
	* layout.cc (Free_list::init): New function.
	(Free_list::remove): New function.
	(Free_list::allocate): New function.
	(Free_list::dump): New function.
	(Layout_task_runner::run): Check for existing output file when doing
	incremental update.
	(Layout::Layout): Initialize new data members.
	(Layout::set_incremental_base): New function.
	(Layout::init_fixed_output_section): New function.
	(Layout::layout_eh_frame): Disable .eh_frame_hdr for incremental links.
	(Layout::create_gold_note): Skip for incremental update.
	(Layout::set_section_offsets): Allocate from free space for incremental
	updates.
	(Layout::create_symtab_sections): Likewise.
	(Layout::create_shdrs): Likewise.
	(Layout::finish_dynamic_section): Don't check --as-needed during
	incremental updates.
	(Layout::init_fixed_output_section): Instantiate templates.
	* layout.h (Free_list): New class.
	(Layout::set_incremental_base): New function.
	(Layout::incremental_base): New function.
	(Layout::init_fixed_output_section): New function.
	(Layout::allocate): New function.
	(Layout::incremental_base_): New data member.
	(Layout::free_list_): New data member.
	* object.cc (Sized_relobj::do_layout): Don't report non-data input
	sections.
	(Sized_relobj::do_for_all_local_got_entries): New function.
	(Sized_relobj::write_local_symbols): Add symbol table file offset to
	relative local symbol offsets.
	* object.h (Got_offset_list::for_all_got_offsets): Change to call
	visitor class.
	(Object::Object): Handle case where input_file is NULL.
	(Object::~Object): Likewise.
	(Object::input_file): Likewise.
	(Object::lock): Likewise.
	(Object::unlock): Likewise.
	(Object::is_locked): Likewise.
	(Object::token): Likewise.
	(Object::release): Likewise.
	(Object::is_incremental): New function.
	(Object::get_mtime): New function.
	(Object::for_all_local_got_entries): New function.
	(Object::clear_view_cache_marks): Handle case where input_file is NULL.
	(Object::do_is_incremental): New function.
	(Object::do_get_mtime): New function.
	(Object::do_for_all_local_got_entries): New function.
	(Sized_relobj::local_got_offset_list): Remove.
	(Sized_relobj::do_for_all_local_got_entries): New function.
	(Sized_relobj::write_local_symbols): Change signature.
	(Sized_relobj::local_symbol_offset_): Now relative to symbol table.
	* options.cc (Input_argument::add_file): Set serial number of input
	argument.
	* options.h (Input_file_argument::set_arg_serial): New function.
	(Input_file_argument::arg_serial): New function.
	(Input_file_argument::arg_serial_): New data member; adjust ctors.
	(Input_arguments::add_file): Remove const.
	(Input_arguments::file_count_): New data member.
	* output.cc (Output_segment_headers::Output_segment_headers): Keep
	current size up to date.
	(Output_section::Input_section::current_data_size): New function.
	(Output_section::Output_section): Initialize new data members.
	(Output_section::add_input_section): Allocate from free space for
	incremental updates.
	(Output_section::add_output_section_data): Likewise.
	(Output_section::update_data_size): New function.
	(Output_section::set_fixed_layout): New function.
	(Output_section::reserve): New function.
	(Output_segment::set_section_addresses): Remove const.
	(Output_segment::set_section_list_addresses): Remove const.  Allocate
	from free space for incremental updates.
	* output.h (Output_data::current_data_size): Moved here from derived
	classes.
	(Output_data::pre_finalize_data_size): New function.
	(Output_data::update_data_size): New function.
	(Output_section_headers::update_data_size): New function.
	(Output_section_data_build::current_data_size): Move to base class.
	(Output_data_strtab::update_data_size): New function.
	(Output_section::Input_section::current_data_size): New function.
	(Output_section::current_data_size): Move to base class.
	(Output_section::set_fixed_layout): New function.
	(Output_section::has_fixed_layout): New function.
	(Output_section::reserve): New function.
	(Output_section::update_data_size): New function.
	(Output_section::has_fixed_layout_): New data member.
	(Output_section::free_list_): New data member.
	(Output_segment::set_section_addresses): Remove const.
	(Output_segment::set_section_list_addresses): Remove const.
	* plugin.cc (Sized_pluginobj::do_for_all_local_got_entries): New
	function.
	* plugin.h (Sized_pluginobj::do_for_all_local_got_entries): New
	function.
	* readsyms.cc (Read_symbols::do_read_symbols): Track serial number of
	input argument.
	(Add_symbols::run): Likewise.
	(Read_member::~Read_member): New function.
	(Read_member::is_runnable): New function.
	(Read_member::locks): New function.
	(Read_member::run): New function.
	(Add_base_symbols::~Add_base_symbols): New function.
	(Add_base_symbols::is_runnable): New function.
	(Add_base_symbols::locks): New function.
	(Add_base_symbols::run): New function.
	(Check_script::~Check_script): New function.
	(Check_script::is_runnable): New function.
	(Check_script::locks): New function.
	(Check_script::run): New function.
	(Check_library::~Check_library): New function.
	(Check_library::is_runnable): New function.
	(Check_library::locks): New function.
	(Check_library::run): New function.
	* readsyms.h (Read_member): New class.
	(Add_base_symbols): New class.
	(Check_script): New class.
	(Check_library): New class.
	* reloc.cc (Read_relocs::locks): Handle incremental objects that don't
	have input files.
	(Scan_relocs::locks): Likewise.
	(Relocate_task::locks): Likewise.
	(Sized_relobj::do_relocate): Pass file offset of symtab section to
	write_local_symbols.
	* script.cc (read_input_script): Track serial number of input
	argument.
	* script.h (Script_info): Move to incremental.h.
	* symtab.cc (Symbol_table::add_from_incrobj): New function.
	Instantiate templates.
	* symtab.h (Symbol_table::add_from_incrobj): New function.
	(Symbol_table::set_file_offset): New function.
---
 gold/archive.cc          |   15 +-
 gold/archive.h           |    4 +-
 gold/dynobj.cc           |   40 ++-
 gold/dynobj.h            |    5 +
 gold/fileread.cc         |   19 +
 gold/fileread.h          |    6 +
 gold/gold.cc             |  250 +++++++++-
 gold/incremental-dump.cc |   94 +++--
 gold/incremental.cc      | 1158 +++++++++++++++++++++++++++++++++++++++++++---
 gold/incremental.h       |  800 ++++++++++++++++++++++++++++----
 gold/layout.cc           |  332 ++++++++++++--
 gold/layout.h            |   64 +++
 gold/object.cc           |   38 ++-
 gold/object.h            |  314 ++++++++-----
 gold/options.cc          |    3 +-
 gold/options.h           |   24 +-
 gold/output.cc           |  204 +++++++-
 gold/output.h            |   83 +++-
 gold/plugin.cc           |   10 +
 gold/plugin.h            |    5 +
 gold/readsyms.cc         |  185 ++++++++-
 gold/readsyms.h          |  200 ++++++++
 gold/reloc.cc            |   15 +-
 gold/script.cc           |    6 +-
 gold/script.h            |   25 -
 gold/symtab.cc           |   72 +++
 gold/symtab.h            |   14 +
 27 files changed, 3517 insertions(+), 468 deletions(-)

diff --git a/gold/archive.cc b/gold/archive.cc
index c48d15a..46df3e7 100644
--- a/gold/archive.cc
+++ b/gold/archive.cc
@@ -901,7 +901,7 @@ Archive::include_member(Symbol_table* symtab, Layout* layout,
     {
       {
 	if (layout->incremental_inputs() != NULL)
-	  layout->incremental_inputs()->report_object(obj, this, NULL);
+	  layout->incremental_inputs()->report_object(obj, 0, this, NULL);
 	Read_symbols_data sd;
 	obj->read_symbols(&sd);
 	obj->layout(symtab, layout, &sd);
@@ -981,8 +981,12 @@ Add_archive_symbols::run(Workqueue* workqueue)
   // For an incremental link, begin recording layout information.
   Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
   if (incremental_inputs != NULL)
-    incremental_inputs->report_archive_begin(this->archive_,
-					this->input_argument_->script_info());
+    {
+      unsigned int arg_serial = this->input_argument_->file().arg_serial();
+      Script_info* script_info = this->input_argument_->script_info();
+      incremental_inputs->report_archive_begin(this->archive_, arg_serial,
+					       script_info);
+    }
 
   bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
 					   this->input_objects_,
@@ -1128,7 +1132,8 @@ Lib_group::include_member(Symbol_table* symtab, Layout* layout,
   if (input_objects->add_object(obj))
     {
       if (layout->incremental_inputs() != NULL)
-	layout->incremental_inputs()->report_object(obj, this, NULL);
+	layout->incremental_inputs()->report_object(obj, member.arg_serial_,
+						    this, NULL);
       obj->layout(symtab, layout, sd);
       obj->add_symbols(symtab, sd, layout);
     }
@@ -1188,7 +1193,7 @@ Add_lib_group_symbols::run(Workqueue*)
   // For an incremental link, begin recording layout information.
   Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
   if (incremental_inputs != NULL)
-    incremental_inputs->report_archive_begin(this->lib_, NULL);
+    incremental_inputs->report_archive_begin(this->lib_, 0, NULL);
 
   this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
 
diff --git a/gold/archive.h b/gold/archive.h
index 8ed7853..59f5dd1 100644
--- a/gold/archive.h
+++ b/gold/archive.h
@@ -48,7 +48,7 @@ class Incremental_archive_entry;
 struct Archive_member
 {
   Archive_member()
-      : obj_(NULL), sd_(NULL)
+      : obj_(NULL), sd_(NULL), arg_serial_(0)
   { }
   Archive_member(Object* obj, Read_symbols_data* sd)
       : obj_(obj), sd_(sd)
@@ -57,6 +57,8 @@ struct Archive_member
   Object* obj_;
   // The data to pass from read_symbols() to add_symbols().
   Read_symbols_data* sd_;
+  // The serial number of the file in the argument list.
+  unsigned int arg_serial_;
 };
 
 // This class serves as a base class for Archive and Lib_group objects.
diff --git a/gold/dynobj.cc b/gold/dynobj.cc
index face503..4fe9b58 100644
--- a/gold/dynobj.cc
+++ b/gold/dynobj.cc
@@ -49,16 +49,23 @@ Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
   // object's filename.  The only exception is when the dynamic object
   // is part of an archive (so the filename is the archive's
   // filename).  In that case, we use just the dynobj's name-in-archive.
-  this->soname_ = this->input_file()->found_name();
-  if (this->offset() != 0)
+  if (input_file == NULL)
+    this->soname_ = name;
+  else
     {
-      std::string::size_type open_paren = this->name().find('(');
-      std::string::size_type close_paren = this->name().find(')');
-      if (open_paren != std::string::npos && close_paren != std::string::npos)
+      this->soname_ = input_file->found_name();
+      if (this->offset() != 0)
 	{
-	  // It's an archive, and name() is of the form 'foo.a(bar.so)'.
-	  this->soname_ = this->name().substr(open_paren + 1,
-					      close_paren - (open_paren + 1));
+	  std::string::size_type open_paren = this->name().find('(');
+	  std::string::size_type close_paren = this->name().find(')');
+	  if (open_paren != std::string::npos
+	      && close_paren != std::string::npos)
+	    {
+	      // It's an archive, and name() is of the form 'foo.a(bar.so)'.
+	      open_paren += 1;
+	      this->soname_ = this->name().substr(open_paren,
+						  close_paren - open_paren);
+	    }
 	}
     }
 }
@@ -705,10 +712,11 @@ Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
   Version_map version_map;
   this->make_version_map(sd, &version_map);
 
-  // If printing symbol counts or a cross reference table, we want to
-  // track symbols.
+  // If printing symbol counts or a cross reference table or
+  // preparing for an incremental link, we want to track symbols.
   if (parameters->options().user_set_print_symbol_counts()
-      || parameters->options().cref())
+      || parameters->options().cref()
+      || parameters->incremental())
     {
       this->symbols_ = new Symbols();
       this->symbols_->resize(symcount);
@@ -787,6 +795,16 @@ Sized_dynobj<size, big_endian>::do_for_all_global_symbols(
     }
 }
 
+// Iterate over local symbols, calling a visitor class V for each GOT offset
+// associated with a local symbol.
+
+template<int size, bool big_endian>
+void
+Sized_dynobj<size, big_endian>::do_for_all_local_got_entries(
+    Got_offset_list::Visitor&) const
+{
+}
+
 // Get symbol counts.
 
 template<int size, bool big_endian>
diff --git a/gold/dynobj.h b/gold/dynobj.h
index 637081c..8eb76b2 100644
--- a/gold/dynobj.h
+++ b/gold/dynobj.h
@@ -186,6 +186,11 @@ class Sized_dynobj : public Dynobj
   do_for_all_global_symbols(Read_symbols_data* sd,
 			    Library_base::Symbol_visitor_base& v);
 
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const;
+
   // Get the size of a section.
   uint64_t
   do_section_size(unsigned int shndx)
diff --git a/gold/fileread.cc b/gold/fileread.cc
index f626028..30453bf 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -57,6 +57,25 @@ readv(int, const iovec*, int)
 namespace gold
 {
 
+// Get the last modified time of an unopened file.
+
+bool
+get_mtime(const char* filename, Timespec* mtime)
+{
+  struct stat file_stat;
+  
+  if (stat(filename, &file_stat) < 0)
+    return false;
+#ifdef HAVE_STAT_ST_MTIM
+  mtime->seconds = file_stat.st_mtim.tv_sec;
+  mtime->nanoseconds = file_stat.st_mtim.tv_nsec;
+#else
+  mtime->seconds = file_stat.st_mtime;
+  mtime->nanoseconds = 0;
+#endif
+  return true;
+}
+
 // Class File_read::View.
 
 File_read::View::~View()
diff --git a/gold/fileread.h b/gold/fileread.h
index 6d0db78..92cbe19 100644
--- a/gold/fileread.h
+++ b/gold/fileread.h
@@ -52,6 +52,12 @@ struct Timespec
   int nanoseconds;
 };
 
+// Get the last modified time of an unopened file.  Returns false if the
+// file does not exist.
+
+bool
+get_mtime(const char* filename, Timespec* mtime);
+
 class Position_dependent_options;
 class Input_file_argument;
 class Dirsearch;
diff --git a/gold/gold.cc b/gold/gold.cc
index 6bbf02e..9097488 100644
--- a/gold/gold.cc
+++ b/gold/gold.cc
@@ -48,9 +48,16 @@
 namespace gold
 {
 
+class Object;
+
 const char* program_name;
 
 void
+process_incremental_input(Incremental_binary*, unsigned int, Workqueue*,
+			  Input_objects*, Symbol_table*, Layout*, Dirsearch*,
+			  Mapfile*, Task_token*, Task_token*);
+
+void
 gold_exit(bool status)
 {
   if (parameters != NULL
@@ -178,19 +185,50 @@ queue_initial_tasks(const General_options& options,
     thread_count = cmdline.number_of_input_files();
   workqueue->set_thread_count(thread_count);
 
+  // For incremental links, the base output file.
+  Incremental_binary* ibase = NULL;
+
   if (parameters->incremental())
     {
-      Incremental_checker incremental_checker(
-          parameters->options().output_file_name(),
-          layout->incremental_inputs());
-      if (incremental_checker.can_incrementally_link_output_file())
-        {
-          // TODO: remove when incremental linking implemented.
-          printf("Incremental linking might be possible "
-              "(not implemented yet)\n");
-        }
-      // TODO: If we decide on an incremental build, fewer tasks
-      // should be scheduled.
+      if (options.relocatable())
+	gold_error(_("incremental linking is incompatible with -r"));
+      if (options.emit_relocs())
+	gold_error(_("incremental linking is incompatible with --emit-relocs"));
+      if (options.gc_sections())
+	gold_error(_("incremental linking is incompatible with --gc-sections"));
+      if (options.icf_enabled())
+	gold_error(_("incremental linking is incompatible with --icf"));
+      if (options.has_plugins())
+	gold_error(_("incremental linking is incompatible with --plugin"));
+
+      if (parameters->incremental_update())
+	{
+	  Output_file* of = new Output_file(options.output_file_name());
+	  if (!of->open_for_modification())
+	    gold_info(_("incremental update not possible: "
+			"cannot open %s"),
+		      options.output_file_name());
+	  else
+	    {
+	      ibase = open_incremental_binary(of);
+	      if (ibase != NULL
+		  && ibase->check_inputs(cmdline, layout->incremental_inputs()))
+		ibase->init_layout(layout);
+	      else
+		{
+		  delete ibase;
+		  ibase = NULL;
+		  of->close();
+		}
+	    }
+	  if (ibase == NULL)
+	    {
+	      if (set_parameters_incremental_full())
+		gold_info(_("linking with --incremental-full"));
+	      else
+		gold_fatal(_("restart link with --incremental-full"));
+	    }
+	}
     }
 
   // Read the input files.  We have to add the symbols to the symbol
@@ -198,16 +236,38 @@ queue_initial_tasks(const General_options& options,
   // each input file.  We associate the blocker with the following
   // input file, to give us a convenient place to delete it.
   Task_token* this_blocker = NULL;
-  for (Command_line::const_iterator p = cmdline.begin();
-       p != cmdline.end();
-       ++p)
+  if (ibase == NULL)
     {
-      Task_token* next_blocker = new Task_token(true);
-      next_blocker->add_blocker();
-      workqueue->queue(new Read_symbols(input_objects, symtab, layout,
-					&search_path, 0, mapfile, &*p, NULL,
-					NULL, this_blocker, next_blocker));
-      this_blocker = next_blocker;
+      // Normal link.  Queue a Read_symbols task for each input file
+      // on the command line.
+      for (Command_line::const_iterator p = cmdline.begin();
+	   p != cmdline.end();
+	   ++p)
+	{
+	  Task_token* next_blocker = new Task_token(true);
+	  next_blocker->add_blocker();
+	  workqueue->queue(new Read_symbols(input_objects, symtab, layout,
+					    &search_path, 0, mapfile, &*p, NULL,
+					    NULL, this_blocker, next_blocker));
+	  this_blocker = next_blocker;
+	}
+    }
+  else
+    {
+      // Incremental update link.  Process the list of input files
+      // stored in the base file, and queue an Add_base_symbols task
+      // for each unchanged file and a Read_symbols task for each
+      // changed file.
+      unsigned int input_file_count = ibase->input_file_count();
+      for (unsigned int i = 0; i < input_file_count; ++i)
+	{
+	  Task_token* next_blocker = new Task_token(true);
+	  next_blocker->add_blocker();
+	  process_incremental_input(ibase, i, workqueue, input_objects, symtab,
+				    layout, &search_path, mapfile,
+				    this_blocker, next_blocker);
+	  this_blocker = next_blocker;
+	}
     }
 
   if (options.has_plugins())
@@ -220,13 +280,11 @@ queue_initial_tasks(const General_options& options,
       this_blocker = next_blocker;
     }
 
-  if (parameters->options().relocatable()
-      && (parameters->options().gc_sections()
-	  || parameters->options().icf_enabled()))
+  if (options.relocatable()
+      && (options.gc_sections() || options.icf_enabled()))
     gold_error(_("cannot mix -r with --gc-sections or --icf"));
 
-  if (parameters->options().gc_sections()
-      || parameters->options().icf_enabled())
+  if (options.gc_sections() || options.icf_enabled())
     {
       workqueue->queue(new Task_function(new Gc_runner(options,
 						       input_objects,
@@ -248,6 +306,145 @@ queue_initial_tasks(const General_options& options,
     }
 }
 
+// Process an incremental input file: if it is unchanged from the previous
+// link, queue a task to add its symbols from the base file's incremental
+// info; if it has changed, queue a normal Read_symbols task.  We queue a
+// task for every input file, if only to report the file for rebuilding the
+// incremental info.
+
+void
+process_incremental_input(Incremental_binary* ibase,
+			  unsigned int input_file_index,
+			  Workqueue* workqueue,
+			  Input_objects* input_objects,
+			  Symbol_table* symtab,
+			  Layout* layout,
+			  Dirsearch* search_path,
+			  Mapfile* mapfile,
+			  Task_token* this_blocker,
+			  Task_token* next_blocker)
+{
+  const Incremental_binary::Input_reader* input_reader =
+      ibase->get_input_reader(input_file_index);
+  Incremental_input_type input_type = input_reader->type();
+
+  gold_debug(DEBUG_INCREMENTAL,   "Incremental object: %s, type %d",
+	     input_reader->filename(), input_type);
+
+  if (input_type == INCREMENTAL_INPUT_SCRIPT)
+    {
+      // Incremental_binary::check_inputs should have cancelled the
+      // incremental update if the script has changed.
+      gold_assert(!ibase->file_has_changed(input_file_index));
+      workqueue->queue(new Check_script(layout, ibase, input_file_index,
+					input_reader, this_blocker,
+					next_blocker));
+      return;
+    }
+
+  if (input_type == INCREMENTAL_INPUT_ARCHIVE)
+    {
+      Incremental_library* lib = ibase->get_library(input_file_index);
+      gold_assert(lib != NULL);
+      if (lib->filename() == "<group>"
+	  || !ibase->file_has_changed(input_file_index))
+	{
+	  // Queue a task to check that no references have been added to any
+	  // of the library's unused symbols.
+	  workqueue->queue(new Check_library(symtab, layout, ibase,
+					     input_file_index, input_reader,
+					     this_blocker, next_blocker));
+	}
+      else
+	{
+	  // Queue a Read_symbols task to process the archive normally.
+	  const Input_argument* input_argument =
+	      ibase->get_input_argument(input_file_index);
+	  if (input_argument == NULL)
+	    {
+	      Input_file_argument file(
+		  input_reader->filename(),
+		  Input_file_argument::INPUT_FILE_TYPE_FILE,
+		  "", false, parameters->options());
+	      input_argument = new Input_argument(file);
+	    }
+	  workqueue->queue(new Read_symbols(input_objects, symtab, layout,
+					    search_path, 0, mapfile,
+					    input_argument, NULL, NULL,
+					    this_blocker, next_blocker));
+	}
+      return;
+    }
+
+  if (input_type == INCREMENTAL_INPUT_ARCHIVE_MEMBER)
+    {
+      // For archive members, check the timestamp of the containing archive.
+      Incremental_library* lib = ibase->get_library(input_file_index);
+      gold_assert(lib != NULL);
+      // Process members of a --start-lib/--end-lib group as normal objects.
+      if (lib->filename() != "<group>")
+	{
+	  if (ibase->file_has_changed(lib->input_file_index()))
+	    {
+	      workqueue->queue(new Read_member(input_objects, symtab, layout,
+					       mapfile, input_reader,
+					       this_blocker, next_blocker));
+	    }
+	  else
+	    {
+	      // The previous contributions from this file will be kept.
+	      // Mark the pieces of output sections contributed by this
+	      // object.
+	      ibase->reserve_layout(input_file_index);
+	      Object* obj = make_sized_incremental_object(ibase,
+							  input_file_index,
+							  input_type,
+							  input_reader);
+	      workqueue->queue(new Add_base_symbols(input_objects, obj,
+						    symtab, layout, ibase,
+						    input_file_index, lib,
+						    mapfile, input_reader,
+						    this_blocker,
+						    next_blocker));
+	    }
+	  return;
+	}
+    }
+
+  // Normal object file or shared library.  Check if the file has changed
+  // since the last incremental link.
+  if (ibase->file_has_changed(input_file_index))
+    {
+      const Input_argument* input_argument =
+	  ibase->get_input_argument(input_file_index);
+      if (input_argument == NULL)
+	{
+	  Input_file_argument file(input_reader->filename(),
+				   Input_file_argument::INPUT_FILE_TYPE_FILE,
+				   "", false, parameters->options());
+	  input_argument = new Input_argument(file);
+	}
+      workqueue->queue(new Read_symbols(input_objects, symtab, layout,
+					search_path, 0, mapfile, input_argument,
+					NULL, NULL, this_blocker,
+					next_blocker));
+    }
+  else
+    {
+      // The previous contributions from this file will be kept.
+      // Mark the pieces of output sections contributed by this object.
+      ibase->reserve_layout(input_file_index);
+      Object* obj = make_sized_incremental_object(ibase,
+						  input_file_index,
+						  input_type,
+						  input_reader);
+      workqueue->queue(new Add_base_symbols(input_objects, obj, symtab,
+					    layout, ibase, input_file_index,
+					    NULL, mapfile, input_reader,
+					    this_blocker, next_blocker));
+    }
+}
+
 // Queue up a set of tasks to be done before queueing the middle set
 // of tasks.  This is only necessary when garbage collection
 // (--gc-sections) of unused sections is desired.  The relocs are read
@@ -395,7 +592,8 @@ queue_middle_tasks(const General_options& options,
   // generate an empty file.  Existing builds depend on being able to
   // pass an empty archive to the linker and get an empty object file
   // out.  In order to do this we need to use a default target.
-  if (input_objects->number_of_input_objects() == 0)
+  if (input_objects->number_of_input_objects() == 0
+      && layout->incremental_base() == NULL)
     parameters_force_valid_target();
 
   int thread_count = options.thread_count_middle();
diff --git a/gold/incremental-dump.cc b/gold/incremental-dump.cc
index 6f79f4d..ac9c1ca 100644
--- a/gold/incremental-dump.cc
+++ b/gold/incremental-dump.cc
@@ -129,6 +129,8 @@ dump_incremental_inputs(const char* argv0, const char* filename,
 	     mtime.nanoseconds,
 	     ctime(&mtime.seconds));
 
+      printf("    Serial Number: %d\n", input_file.arg_serial());
+
       Incremental_input_type input_type = input_file.type();
       printf("    Type: ");
       switch (input_type)
@@ -203,42 +205,6 @@ dump_incremental_inputs(const char* argv0, const char* filename,
 	}
     }
 
-  printf("\nGlobal symbols per input file:\n");
-  for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
-    {
-      Entry_reader input_file(incremental_inputs.input_file(i));
-
-      if (input_file.type() != INCREMENTAL_INPUT_OBJECT
-	  && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
-	continue;
-
-      const char* objname = input_file.filename();
-      if (objname == NULL)
-	{
-	  fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
-		  argv0, filename, i);
-	  exit(1);
-	}
-
-      printf("[%d] %s\n", i, objname);
-
-      unsigned int nsyms = input_file.get_global_symbol_count();
-      if (nsyms > 0)
-	printf("    %6s  %8s  %8s  %8s  %8s\n",
-	       "outndx", "offset", "chain", "#relocs", "rbase");
-      for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
-	{
-	  typename Entry_reader::Global_symbol_info info(
-	      input_file.get_global_symbol_info(symndx));
-	  printf("    %6d  %8d  %8d  %8d  %8d\n",
-		 info.output_symndx,
-		 input_file.get_symbol_offset(symndx),
-		 info.next_offset,
-		 info.reloc_count,
-		 info.reloc_offset);
-	}
-    }
-
   // Get a view of the .symtab section.
 
   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file(inc);
@@ -280,8 +246,60 @@ dump_incremental_inputs(const char* argv0, const char* filename,
   unsigned int nsyms = symtab_location.data_size / sym_size;
   unsigned int nglobals = isymtab.symbol_count();
   unsigned int first_global = nsyms - nglobals;
-  unsigned const char* sym_p = symtab_view.data() + first_global * sym_size;
+  unsigned const char* sym_p;
+
+  printf("\nGlobal symbols per input file:\n");
+  for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
+    {
+      Entry_reader input_file(incremental_inputs.input_file(i));
+
+      if (input_file.type() != INCREMENTAL_INPUT_OBJECT
+	  && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
+	  && input_file.type() != INCREMENTAL_INPUT_SHARED_LIBRARY)
+	continue;
+
+      const char* objname = input_file.filename();
+      if (objname == NULL)
+	{
+	  fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
+		  argv0, filename, i);
+	  exit(1);
+	}
+
+      printf("[%d] %s\n", i, objname);
+
+      unsigned int nsyms = input_file.get_global_symbol_count();
+      if (nsyms > 0)
+	printf("    %6s  %8s  %8s  %8s  %8s\n",
+	       "outndx", "offset", "chain", "#relocs", "rbase");
+      for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
+	{
+	  typename Entry_reader::Global_symbol_info info(
+	      input_file.get_global_symbol_info(symndx));
+	  sym_p = symtab_view.data() + info.output_symndx * sym_size;
+          elfcpp::Sym<size, big_endian> sym(sym_p);
+          const char* symname;
+          if (!strtab.get_c_string(sym.get_st_name(), &symname))
+	    symname = "<unknown>";
+	  if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
+	    printf("    %6d  %8s  %8s  %8s  %8s  %-5s  %s\n",
+		   info.output_symndx,
+		   "", "", "", "",
+		   info.is_def ? "DEF" : "UNDEF",
+		   symname);
+	  else
+	    printf("    %6d  %8d  %8d  %8d  %8d  %-5s  %s\n",
+		   info.output_symndx,
+		   input_file.get_symbol_offset(symndx),
+		   info.next_offset,
+		   info.reloc_count,
+		   info.reloc_offset,
+		   info.is_def ? "DEF" : "UNDEF",
+		   symname);
+	}
+    }
 
+  sym_p = symtab_view.data() + first_global * sym_size;
   printf("\nGlobal symbol table:\n");
   for (unsigned int i = 0; i < nglobals; i++)
     {
@@ -309,7 +327,7 @@ dump_incremental_inputs(const char* argv0, const char* filename,
 	  unsigned int r_off = sym_info.reloc_offset;
 	  for (unsigned int j = 0; j < sym_info.reloc_count; j++)
 	    {
-	      printf("      %4d  relocation type %3d  shndx %d"
+	      printf("      %4d  relocation type %3d  shndx %2d"
 		     "  offset %016llx  addend %016llx  %s\n",
 		     r_off,
 		     irelocs.get_r_type(r_off),
diff --git a/gold/incremental.cc b/gold/incremental.cc
index 07cfb55..a65229e 100644
--- a/gold/incremental.cc
+++ b/gold/incremental.cc
@@ -26,13 +26,17 @@
 #include "libiberty.h"
 
 #include "elfcpp.h"
+#include "options.h"
 #include "output.h"
 #include "symtab.h"
 #include "incremental.h"
 #include "archive.h"
+#include "object.h"
 #include "output.h"
 #include "target-select.h"
 #include "target.h"
+#include "fileread.h"
+#include "script.h"
 
 namespace gold {
 
@@ -54,6 +58,12 @@ class Output_section_incremental_inputs : public Output_section_data
   { }
 
  protected:
+  // This is called to update the section size prior to assigning
+  // the address and file offset.
+  void
+  update_data_size()
+  { this->set_final_data_size(); }
+
   // Set the final data size.
   void
   set_final_data_size();
@@ -247,47 +257,292 @@ Sized_incremental_binary<size, big_endian>::setup_readers()
 						  relocs_location.data_size);
   this->got_plt_reader_ =
       Incremental_got_plt_reader<big_endian>(got_plt_view.data());
+
+  // Walk the list of input files (a) to setup an Input_reader for each
+  // input file, and (b) to record maps of files added from archive
+  // libraries and scripts.
+  Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
+  unsigned int count = inputs.input_file_count();
+  this->input_entry_readers_.reserve(count);
+  this->library_map_.resize(count);
+  this->script_map_.resize(count);
+  for (unsigned int i = 0; i < count; i++)
+    {
+      Input_entry_reader input_file = inputs.input_file(i);
+      this->input_entry_readers_.push_back(Sized_input_reader(input_file));
+      switch (input_file.type())
+	{
+	case INCREMENTAL_INPUT_OBJECT:
+	case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
+	case INCREMENTAL_INPUT_SHARED_LIBRARY:
+	  // No special treatment necessary.
+	  break;
+	case INCREMENTAL_INPUT_ARCHIVE:
+	  {
+	    Incremental_library* lib =
+	        new Incremental_library(input_file.filename(), i,
+					&this->input_entry_readers_[i]);
+	    this->library_map_[i] = lib;
+	    unsigned int member_count = input_file.get_member_count();
+	    for (unsigned int j = 0; j < member_count; j++)
+	      {
+		int member_offset = input_file.get_member_offset(j);
+		int member_index = inputs.input_file_index(member_offset);
+		this->library_map_[member_index] = lib;
+	      }
+	  }
+	  break;
+	case INCREMENTAL_INPUT_SCRIPT:
+	  {
+	    Script_info* script = new Script_info(input_file.filename());
+	    this->script_map_[i] = script;
+	    unsigned int object_count = input_file.get_object_count();
+	    for (unsigned int j = 0; j < object_count; j++)
+	      {
+		int object_offset = input_file.get_object_offset(j);
+		int object_index = inputs.input_file_index(object_offset);
+		this->script_map_[object_index] = script;
+	      }
+	  }
+	  break;
+	default:
+	  gold_unreachable();
+	}
+    }
+
   this->has_incremental_info_ = true;
 }
 
+// Walk the list of input files given on the command line, and build
+// a direct map of file index to the corresponding input argument.
+
+void
+check_input_args(std::vector<const Input_argument*>& input_args_map,
+		 Input_arguments::const_iterator begin,
+		 Input_arguments::const_iterator end)
+{
+  for (Input_arguments::const_iterator p = begin;
+       p != end;
+       ++p)
+    {
+      if (p->is_group())
+	{
+	  const Input_file_group* group = p->group();
+	  check_input_args(input_args_map, group->begin(), group->end());
+	}
+      else if (p->is_lib())
+	{
+	  const Input_file_lib* lib = p->lib();
+	  check_input_args(input_args_map, lib->begin(), lib->end());
+	}
+      else
+        {
+          gold_assert(p->is_file());
+          unsigned int arg_serial = p->file().arg_serial();
+          if (arg_serial > 0)
+            input_args_map[arg_serial - 1] = &*p;
+        }
+    }
+}
+
 // Determine whether an incremental link based on the existing output file
 // can be done.
 
 template<int size, bool big_endian>
 bool
 Sized_incremental_binary<size, big_endian>::do_check_inputs(
+    const Command_line& cmdline,
     Incremental_inputs* incremental_inputs)
 {
+  Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
+
   if (!this->has_incremental_info_)
     {
       explain_no_incremental(_("no incremental data from previous build"));
       return false;
     }
 
-  if (this->inputs_reader_.version() != INCREMENTAL_LINK_VERSION)
+  if (inputs.version() != INCREMENTAL_LINK_VERSION)
     {
       explain_no_incremental(_("different version of incremental build data"));
       return false;
     }
 
-  if (incremental_inputs->command_line() != this->inputs_reader_.command_line())
+  if (incremental_inputs->command_line() != inputs.command_line())
     {
       explain_no_incremental(_("command line changed"));
       return false;
     }
 
+  // Walk the list of input files given on the command line, and build
+  // a direct map of argument serial numbers to the corresponding input
+  // arguments.
+  this->input_args_map_.resize(cmdline.number_of_input_files());
+  check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
+
+  // Walk the list of input files to check for conditions that prevent
+  // an incremental update link.
+  unsigned int count = inputs.input_file_count();
+  for (unsigned int i = 0; i < count; i++)
+    {
+      Input_entry_reader input_file = inputs.input_file(i);
+      switch (input_file.type())
+	{
+	case INCREMENTAL_INPUT_OBJECT:
+	case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
+	case INCREMENTAL_INPUT_SHARED_LIBRARY:
+	case INCREMENTAL_INPUT_ARCHIVE:
+	  // No special treatment necessary.
+	  break;
+	case INCREMENTAL_INPUT_SCRIPT:
+	  if (this->do_file_has_changed(i))
+	    {
+	      explain_no_incremental(_("%s: script file changed"),
+				     input_file.filename());
+	      return false;
+	    }
+	  break;
+	default:
+	  gold_unreachable();
+	}
+    }
+
   return true;
 }
 
+// Return TRUE if input file N has changed since the last incremental link.
 
 template<int size, bool big_endian>
-Incremental_binary::Input_reader*
-Sized_incremental_binary<size, big_endian>::do_get_input_reader(
-    const char*)
+bool
+Sized_incremental_binary<size, big_endian>::do_file_has_changed(
+    unsigned int n) const
 {
-  unsigned int file_index = this->current_input_file_++;
-  gold_assert(file_index < this->inputs_reader_.input_file_count());
-  return new Sized_input_reader(this->inputs_reader_.input_file(file_index));
+  Input_entry_reader input_file = this->inputs_reader_.input_file(n);
+  Incremental_disposition disp = INCREMENTAL_CHECK;
+  const Input_argument* input_argument = this->get_input_argument(n);
+  if (input_argument != NULL)
+    disp = input_argument->file().options().incremental_disposition();
+
+  if (disp != INCREMENTAL_CHECK)
+    return disp == INCREMENTAL_CHANGED;
+
+  const char* filename = input_file.filename();
+  Timespec old_mtime = input_file.get_mtime();
+  Timespec new_mtime;
+  if (!get_mtime(filename, &new_mtime))
+    {
+      // If we can't open get the current modification time, assume it has
+      // changed.  If the file doesn't exist, we'll issue an error when we
+      // try to open it later.
+      return true;
+    }
+
+  if (new_mtime.seconds > old_mtime.seconds)
+    return true;
+  if (new_mtime.seconds == old_mtime.seconds
+      && new_mtime.nanoseconds > old_mtime.nanoseconds)
+    return true;
+  return false;
+}
+
+// Initialize the layout of the output file based on the existing
+// output file.
+
+template<int size, bool big_endian>
+void
+Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
+{
+  typedef elfcpp::Shdr<size, big_endian> Shdr;
+  const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
+
+  // Get views of the section headers and the section string table.
+  const off_t shoff = this->elf_file_.shoff();
+  const unsigned int shnum = this->elf_file_.shnum();
+  const unsigned int shstrndx = this->elf_file_.shstrndx();
+  Location shdrs_location(shoff, shnum * shdr_size);
+  Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
+  View shdrs_view = this->view(shdrs_location);
+  View shstrndx_view = this->view(shstrndx_location);
+  elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
+			      shstrndx_location.data_size);
+
+  layout->set_incremental_base(this);
+
+  // Initialize the layout.
+  this->section_map_.resize(shnum);
+  const unsigned char* pshdr = shdrs_view.data() + shdr_size;
+  for (unsigned int i = 1; i < shnum; i++)
+    {
+      Shdr shdr(pshdr);
+      const char* name;
+      if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
+        name = NULL;
+      gold_debug(DEBUG_INCREMENTAL,
+		 "Output section: %2d %08lx %08lx %08lx %3d %s",
+	         i,
+	         static_cast<long>(shdr.get_sh_addr()),
+	         static_cast<long>(shdr.get_sh_offset()),
+	         static_cast<long>(shdr.get_sh_size()),
+	         shdr.get_sh_type(), name ? name : "<null>");
+      this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
+      pshdr += shdr_size;
+    }
+}
+
+// Mark regions of the input file that must be kept unchanged.
+
+template<int size, bool big_endian>
+void
+Sized_incremental_binary<size, big_endian>::do_reserve_layout(
+    unsigned int input_file_index)
+{
+  Input_entry_reader input_file =
+      this->inputs_reader_.input_file(input_file_index);
+
+  if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
+    return;
+
+  unsigned int shnum = input_file.get_input_section_count();
+  for (unsigned int i = 0; i < shnum; i++)
+    {
+      typename Input_entry_reader::Input_section_info sect =
+          input_file.get_input_section(i);
+      if (sect.output_shndx == 0 || sect.sh_offset == -1)
+        continue;
+      Output_section* os = this->section_map_[sect.output_shndx];
+      gold_assert(os != NULL);
+      gold_debug(DEBUG_INCREMENTAL,
+      		 "reserve_layout: %6ld  %6ld  %s",
+      		 static_cast<long>(sect.sh_offset),
+      		 static_cast<long>(sect.sh_size),
+      		 os->name());
+      os->reserve(sect.sh_offset, sect.sh_size);
+    }
+}
+
+// Get a view of the main symbol table and the symbol string table.
+
+template<int size, bool big_endian>
+void
+Sized_incremental_binary<size, big_endian>::get_symtab_view(
+    View* symtab_view,
+    unsigned int* nsyms,
+    elfcpp::Elf_strtab* strtab)
+{
+  unsigned int symtab_shndx =
+      this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
+  gold_assert(symtab_shndx != elfcpp::SHN_UNDEF);
+  Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
+  *symtab_view = this->view(symtab_location);
+  *nsyms = symtab_location.data_size / elfcpp::Elf_sizes<size>::sym_size;
+
+  unsigned int strtab_shndx = this->elf_file_.section_link(symtab_shndx);
+  gold_assert(strtab_shndx != elfcpp::SHN_UNDEF
+              && strtab_shndx < this->elf_file_.shnum());
+
+  Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
+  View strtab_view(this->view(strtab_location));
+  *strtab = elfcpp::Elf_strtab(strtab_view.data(), strtab_location.data_size);
 }
 
 namespace
@@ -399,21 +654,6 @@ open_incremental_binary(Output_file* file)
   return result;
 }
 
-// Analyzes the output file to check if incremental linking is possible and
-// (to be done) what files need to be relinked.
-
-bool
-Incremental_checker::can_incrementally_link_output_file()
-{
-  Output_file output(this->output_name_);
-  if (!output.open_for_modification())
-    return false;
-  Incremental_binary* binary = open_incremental_binary(&output);
-  if (binary == NULL)
-    return false;
-  return binary->check_inputs(this->incremental_inputs_);
-}
-
 // Class Incremental_inputs.
 
 // Add the command line to the string table, setting
@@ -467,14 +707,19 @@ Incremental_inputs::report_command_line(int argc, const char* const* argv)
 
 void
 Incremental_inputs::report_archive_begin(Library_base* arch,
+					 unsigned int arg_serial,
 					 Script_info* script_info)
 {
   Stringpool::Key filename_key;
   Timespec mtime = arch->get_mtime();
 
+  // For a file loaded from a script, don't record its argument serial number.
+  if (script_info != NULL)
+    arg_serial = 0;
+
   this->strtab_->add(arch->filename().c_str(), false, &filename_key);
   Incremental_archive_entry* entry =
-      new Incremental_archive_entry(filename_key, mtime);
+      new Incremental_archive_entry(filename_key, arg_serial, mtime);
   arch->set_incremental_info(entry);
 
   if (script_info != NULL)
@@ -529,15 +774,19 @@ Incremental_inputs::report_archive_end(Library_base* arch)
 // Add_symbols task after finding out the type of the file.
 
 void
-Incremental_inputs::report_object(Object* obj, Library_base* arch,
-				  Script_info* script_info)
+Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
+				  Library_base* arch, Script_info* script_info)
 {
   Stringpool::Key filename_key;
-  Timespec mtime = obj->input_file()->file().get_mtime();
+  Timespec mtime = obj->get_mtime();
+
+  // For a file loaded from a script, don't record its argument serial number.
+  if (script_info != NULL)
+    arg_serial = 0;
 
   this->strtab_->add(obj->name().c_str(), false, &filename_key);
   Incremental_object_entry* obj_entry =
-      new Incremental_object_entry(filename_key, obj, mtime);
+      new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
   this->inputs_.push_back(obj_entry);
 
   if (arch != NULL)
@@ -580,14 +829,15 @@ Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
 // of inputs added by this script.
 
 void
-Incremental_inputs::report_script(const std::string& filename,
-				  Script_info* script, Timespec mtime)
+Incremental_inputs::report_script(Script_info* script,
+				  unsigned int arg_serial,
+				  Timespec mtime)
 {
   Stringpool::Key filename_key;
 
-  this->strtab_->add(filename.c_str(), false, &filename_key);
+  this->strtab_->add(script->filename().c_str(), false, &filename_key);
   Incremental_script_entry* entry =
-      new Incremental_script_entry(filename_key, script, mtime);
+      new Incremental_script_entry(filename_key, arg_serial, script, mtime);
   this->inputs_.push_back(entry);
   script->set_incremental_info(entry);
 }
@@ -717,8 +967,20 @@ Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
 	    info_offset += 4;
 	    // Each global symbol.
 	    const Object::Symbols* syms = entry->object()->get_global_symbols();
-	    unsigned int nsyms = syms != NULL ? syms->size() : 0;
-	    info_offset += nsyms * 4;
+	    gold_assert(syms != NULL);
+	    unsigned int nsyms = syms->size();
+	    unsigned int nsyms_out = 0;
+	    for (unsigned int i = 0; i < nsyms; ++i)
+	      {
+		const Symbol* sym = (*syms)[i];
+		if (sym == NULL)
+		  continue;
+		if (sym->is_forwarder())
+		  sym = this->symtab_->resolve_forwards(sym);
+	        if (sym->symtab_index() != -1U)
+	          ++nsyms_out;
+	      }
+	    info_offset += nsyms_out * 4;
 	  }
 	  break;
 	case INCREMENTAL_INPUT_ARCHIVE:
@@ -865,8 +1127,8 @@ Output_section_incremental_inputs<size, big_endian>::write_input_files(
       Swap32::writeval(pov + 4, (*p)->get_info_offset());
       Swap64::writeval(pov + 8, mtime.seconds);
       Swap32::writeval(pov + 16, mtime.nanoseconds);
-      Swap16::writeval(pov + 20, (*p)->type());
-      Swap16::writeval(pov + 22, 0);
+      Swap32::writeval(pov + 20, (*p)->type());
+      Swap16::writeval(pov + 22, (*p)->arg_serial());
       pov += this->input_entry_size;
     }
   return pov;
@@ -935,6 +1197,7 @@ Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
 	    // offset within output section, and input section size.
 	    for (unsigned int i = 0; i < nsections; i++)
 	      {
+		unsigned int shndx = entry->get_input_section_index(i);
 		Stringpool::Key key = entry->get_input_section_name_key(i);
 		off_t name_offset = 0;
 		if (key != 0)
@@ -942,11 +1205,11 @@ Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
 		int out_shndx = 0;
 		off_t out_offset = 0;
 		off_t sh_size = 0;
-		Output_section* os = obj->output_section(i);
+		Output_section* os = obj->output_section(shndx);
 		if (os != NULL)
 		  {
 		    out_shndx = os->out_shndx();
-		    out_offset = obj->output_section_offset(i);
+		    out_offset = obj->output_section_offset(shndx);
 		    sh_size = entry->get_input_section_size(i);
 		  }
 		Swap32::writeval(pov, name_offset);
@@ -966,6 +1229,11 @@ Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
 		const Symbol* sym = (*syms)[i];
 		if (sym->is_forwarder())
 		  sym = this->symtab_->resolve_forwards(sym);
+		unsigned int def_flag = 0;
+		if (sym->source() == Symbol::FROM_OBJECT
+		    && sym->object() == obj
+		    && sym->is_defined())
+		  def_flag = 1U << 31;
 		unsigned int symtab_index = sym->symtab_index();
 		unsigned int chain = 0;
 		unsigned int first_reloc = 0;
@@ -980,7 +1248,7 @@ Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
 		    global_syms[symtab_index - first_global_index] =
 			pov - oview;
 		  }
-		Swap32::writeval(pov, symtab_index);
+		Swap32::writeval(pov, symtab_index | def_flag);
 		Swap32::writeval(pov + 4, chain);
 		Swap32::writeval(pov + 8, nrelocs);
 		Swap32::writeval(pov + 12, first_reloc * 3 * sizeof_addr);
@@ -998,18 +1266,34 @@ Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
 	    const Object* obj = entry->object();
 	    const Object::Symbols* syms = obj->get_global_symbols();
 
-	    // Write the global symbol count.
-	    unsigned int nsyms = syms != NULL ? syms->size() : 0;
-	    Swap32::writeval(pov, nsyms);
+	    // Skip the global symbol count for now.
+	    unsigned char* orig_pov = pov;
 	    pov += 4;
 
 	    // For each global symbol, write the global symbol table index.
+	    unsigned int nsyms = syms->size();
+	    unsigned int nsyms_out = 0;
 	    for (unsigned int i = 0; i < nsyms; i++)
 	      {
 		const Symbol* sym = (*syms)[i];
-		Swap32::writeval(pov, sym->symtab_index());
+		if (sym == NULL)
+		  continue;
+		if (sym->is_forwarder())
+		  sym = this->symtab_->resolve_forwards(sym);
+	        if (sym->symtab_index() == -1U)
+	          continue;
+		unsigned int def_flag = 0;
+		if (sym->source() == Symbol::FROM_OBJECT
+		    && sym->object() == obj
+		    && sym->is_defined())
+		  def_flag = 1U << 31;
+		Swap32::writeval(pov, sym->symtab_index() | def_flag);
 		pov += 4;
+		++nsyms_out;
 	      }
+
+	    // Now write the global symbol count.
+	    Swap32::writeval(orig_pov, nsyms_out);
 	  }
 	  break;
 
@@ -1099,7 +1383,7 @@ struct Got_plt_view_info
 // arrays in the output section.
 
 template<int size, bool big_endian>
-class Local_got_offset_visitor
+class Local_got_offset_visitor : public Got_offset_list::Visitor
 {
  public:
   Local_got_offset_visitor(struct Got_plt_view_info& info)
@@ -1107,7 +1391,7 @@ class Local_got_offset_visitor
   { }
 
   void
-  operator()(unsigned int got_type, unsigned int got_offset)
+  visit(unsigned int got_type, unsigned int got_offset)
   {
     unsigned int got_index = got_offset / this->got_entry_size_;
     gold_assert(got_index < this->info_.got_count);
@@ -1130,7 +1414,7 @@ class Local_got_offset_visitor
 // section.
 
 template<int size, bool big_endian>
-class Global_got_offset_visitor
+class Global_got_offset_visitor : public Got_offset_list::Visitor
 {
  public:
   Global_got_offset_visitor(struct Got_plt_view_info& info)
@@ -1138,7 +1422,7 @@ class Global_got_offset_visitor
   { }
 
   void
-  operator()(unsigned int got_type, unsigned int got_offset)
+  visit(unsigned int got_type, unsigned int got_offset)
   {
     unsigned int got_index = got_offset / this->got_entry_size_;
     gold_assert(got_index < this->info_.got_count);
@@ -1175,8 +1459,9 @@ class Global_symbol_visitor_got_plt
     const Got_offset_list* got_offsets = sym->got_offset_list();
     if (got_offsets != NULL)
       {
-        info_.got_descriptor = sym->symtab_index();
-	got_offsets->for_all_got_offsets(Got_visitor(info_));
+        this->info_.got_descriptor = sym->symtab_index();
+        Got_visitor v(this->info_);
+	got_offsets->for_all_got_offsets(v);
       }
     if (sym->has_plt_offset())
       {
@@ -1226,6 +1511,7 @@ Output_section_incremental_inputs<size, big_endian>::write_got_plt(
   memset(view_info.got_type_p, 0xff, view_info.got_count);
 
   // Write the incremental GOT descriptors for local symbols.
+  typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
   for (Incremental_inputs::Input_list::const_iterator p =
 	   this->inputs_->input_files().begin();
        p != this->inputs_->input_files().end();
@@ -1236,20 +1522,11 @@ Output_section_incremental_inputs<size, big_endian>::write_got_plt(
 	continue;
       Incremental_object_entry* entry = (*p)->object_entry();
       gold_assert(entry != NULL);
-      const Sized_relobj<size, big_endian>* obj =
-          static_cast<Sized_relobj<size, big_endian>*>(entry->object());
+      const Object* obj = entry->object();
       gold_assert(obj != NULL);
-      unsigned int nsyms = obj->local_symbol_count();
-      for (unsigned int i = 0; i < nsyms; i++)
-        {
-          const Got_offset_list* got_offsets = obj->local_got_offset_list(i);
-          if (got_offsets != NULL)
-            {
-	      typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
-	      view_info.got_descriptor = (*p)->get_offset();
-	      got_offsets->for_all_got_offsets(Got_visitor(view_info));
-	    }
-	}
+      view_info.got_descriptor = (*p)->get_offset();
+      Got_visitor v(view_info);
+      obj->for_all_local_got_entries(v);
     }
 
   // Write the incremental GOT and PLT descriptors for global symbols.
@@ -1257,26 +1534,783 @@ Output_section_incremental_inputs<size, big_endian>::write_got_plt(
   symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
 }
 
+// Class Sized_incr_relobj.  Most of these methods are not used for
+// Incremental objects, but are required to be implemented by the
+// base class Object.
+
+// Read the symbols.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
+{
+  gold_unreachable();
+}
+
+// Lay out the input sections.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_layout(
+    Symbol_table*,
+    Layout* layout,
+    Read_symbols_data*)
+{
+  unsigned int shnum = this->input_reader_.get_input_section_count();
+  Incremental_inputs* incremental_inputs = layout->incremental_inputs();
+  gold_assert(incremental_inputs != NULL);
+  Output_sections& out_sections(this->output_sections());
+  out_sections.resize(shnum);
+  this->section_offsets_.resize(shnum);
+  for (unsigned int i = 0; i < shnum; i++)
+    {
+      typename Input_entry_reader::Input_section_info sect =
+          this->input_reader_.get_input_section(i);
+      // Add the section to the incremental inputs layout.
+      incremental_inputs->report_input_section(this, i, sect.name, sect.sh_size);
+      if (sect.output_shndx == 0 || sect.sh_offset == -1)
+        continue;
+      Output_section* os = this->ibase_->output_section(sect.output_shndx);
+      gold_assert(os != NULL);
+      out_sections[i] = os;
+      this->section_offsets_[i] = static_cast<Address>(sect.sh_offset);
+    }
+}
+
+// Layout sections whose layout was deferred while waiting for
+// input files from a plugin.
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_layout_deferred_sections(Layout*)
+{
+}
+
+// Add the symbols to the symbol table.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_add_symbols(
+    Symbol_table* symtab,
+    Read_symbols_data*,
+    Layout*)
+{
+  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+  unsigned char symbuf[sym_size];
+  elfcpp::Sym<size, big_endian> sym(symbuf);
+  elfcpp::Sym_write<size, big_endian> osym(symbuf);
+
+  typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
+
+  unsigned int nsyms = this->input_reader_.get_global_symbol_count();
+  this->symbols_.resize(nsyms);
+
+  Incremental_binary::View symtab_view(NULL);
+  unsigned int symtab_count;
+  elfcpp::Elf_strtab strtab(NULL, 0);
+  this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
+
+  // Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
+  // Incremental_relocs_reader<size, big_endian> irelocs(this->ibase_->relocs_reader());
+  // unsigned int isym_count = isymtab.symbol_count();
+  // unsigned int first_global = symtab_count - isym_count;
+
+  unsigned const char* sym_p;
+  for (unsigned int i = 0; i < nsyms; ++i)
+    {
+      typename Input_entry_reader::Global_symbol_info info =
+	  this->input_reader_.get_global_symbol_info(i);
+      sym_p = symtab_view.data() + info.output_symndx * sym_size;
+      elfcpp::Sym<size, big_endian> gsym(sym_p);
+      const char* name;
+      if (!strtab.get_c_string(gsym.get_st_name(), &name))
+	name = "";
+
+      typename elfcpp::Elf_types<size>::Elf_Addr v;
+      unsigned int shndx = gsym.get_st_shndx();
+      elfcpp::STB st_bind = gsym.get_st_bind();
+      elfcpp::STT st_type = gsym.get_st_type();
+
+      // Local hidden symbols start out as globals, but get converted to
+      // to local during output.
+      if (st_bind == elfcpp::STB_LOCAL)
+        st_bind = elfcpp::STB_GLOBAL;
+
+      if (!info.is_def)
+	{
+	  shndx = elfcpp::SHN_UNDEF;
+	  v = 0;
+	}
+      else if (shndx != elfcpp::SHN_ABS)
+	{
+	  // Find the input section and calculate the section-relative value.
+	  gold_assert(shndx != elfcpp::SHN_UNDEF);
+	  v = gsym.get_st_value();
+	  Output_section* os = this->ibase_->output_section(shndx);
+	  gold_assert(os != NULL && os->has_fixed_layout());
+	  off_t v_rel = v - os->address();
+	  unsigned int nsect = this->input_reader_.get_input_section_count();
+	  unsigned int in_shndx = 0;
+	  for (; in_shndx < nsect; ++in_shndx)
+	    {
+	      typename Input_entry_reader::Input_section_info sect =
+		  this->input_reader_.get_input_section(in_shndx);
+	      if (sect.output_shndx == shndx
+		  && sect.sh_offset <= v_rel
+		  && sect.sh_offset + sect.sh_size > v_rel)
+		{
+		  v_rel -= sect.sh_offset;
+		  break;
+		}
+	    }
+	  gold_assert(in_shndx < nsect);
+	  shndx = in_shndx;
+	  v = v_rel;
+	}
+
+      osym.put_st_name(0);
+      osym.put_st_value(v);
+      osym.put_st_size(gsym.get_st_size());
+      osym.put_st_info(st_bind, st_type);
+      osym.put_st_other(gsym.get_st_other());
+      osym.put_st_shndx(shndx);
+
+      this->symbols_[i] =
+	symtab->add_from_incrobj(this, name, NULL, &sym);
+    }
+}
+
+// Return TRUE if we should include this object from an archive library.
+
+template<int size, bool big_endian>
+Archive::Should_include
+Sized_incr_relobj<size, big_endian>::do_should_include_member(
+    Symbol_table*,
+    Layout*,
+    Read_symbols_data*,
+    std::string*)
+{
+  gold_unreachable();
+}
+
+// Iterate over global symbols, calling a visitor class V for each.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_for_all_global_symbols(
+    Read_symbols_data*,
+    Library_base::Symbol_visitor_base&)
+{
+  // FIXME: Implement Sized_incr_relobj::do_for_all_global_symbols.
+}
+
+// Iterate over local symbols, calling a visitor class V for each GOT offset
+// associated with a local symbol.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_for_all_local_got_entries(
+    Got_offset_list::Visitor&) const
+{
+  // FIXME: Implement Sized_incr_relobj::do_for_all_local_got_entries.
+}
+
+// Get the size of a section.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_relobj<size, big_endian>::do_section_size(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Get the name of a section.
+
+template<int size, bool big_endian>
+std::string
+Sized_incr_relobj<size, big_endian>::do_section_name(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return a view of the contents of a section.
+
+template<int size, bool big_endian>
+Object::Location
+Sized_incr_relobj<size, big_endian>::do_section_contents(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section flags.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_relobj<size, big_endian>::do_section_flags(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section entsize.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_relobj<size, big_endian>::do_section_entsize(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section address.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_relobj<size, big_endian>::do_section_address(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section type.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_section_type(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section link field.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_section_link(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section link field.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_section_info(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section alignment.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_relobj<size, big_endian>::do_section_addralign(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the Xindex structure to use.
+
+template<int size, bool big_endian>
+Xindex*
+Sized_incr_relobj<size, big_endian>::do_initialize_xindex()
+{
+  gold_unreachable();
+}
+
+// Get symbol counts.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_get_global_symbol_counts(
+    const Symbol_table*, size_t*, size_t*) const
+{
+  gold_unreachable();
+}
+
+// Read the relocs.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_read_relocs(Read_relocs_data*)
+{
+}
+
+// Process the relocs to find list of referenced sections. Used only
+// during garbage collection.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_gc_process_relocs(Symbol_table*,
+							  Layout*,
+							  Read_relocs_data*)
+{
+  gold_unreachable();
+}
+
+// Scan the relocs and adjust the symbol table.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_scan_relocs(Symbol_table*,
+						    Layout* layout,
+						    Read_relocs_data*)
+{
+  // FIXME: Scan the relocs for incremental files.
+  this->allocate_incremental_reloc_counts();
+  this->finalize_incremental_relocs(layout);
+}
+
+// Count the local symbols.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_count_local_symbols(
+    Stringpool_template<char>*,
+    Stringpool_template<char>*)
+{
+  // FIXME: Count local symbols.
+}
+
+// Finalize the local symbols.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_finalize_local_symbols(
+    unsigned int index,
+    off_t,
+    Symbol_table*)
+{
+  // FIXME: Finalize local symbols.
+  return index;
+}
+
+// Set the offset where local dynamic symbol information will be stored.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_indexes(
+    unsigned int index)
+{
+  // FIXME: set local dynsym indexes.
+  return index;
+}
+
+// Set the offset where local dynamic symbol information will be stored.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t)
+{
+  return 0;
+}
+
+// Relocate the input sections and write out the local symbols.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_relocate(const Symbol_table*,
+						 const Layout*,
+						 Output_file*)
+{
+  // FIXME: Relocate.
+}
+
+// Set the offset of a section.
+
+template<int size, bool big_endian>
+void
+Sized_incr_relobj<size, big_endian>::do_set_section_offset(unsigned int,
+							   uint64_t)
+{
+}
+
+// Class Sized_incr_dynobj.  Most of these methods are not used for
+// Incremental objects, but are required to be implemented by the
+// base class Object.
+
+// Read the symbols.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
+{
+  gold_unreachable();
+}
+
+// Lay out the input sections.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_layout(
+    Symbol_table*,
+    Layout*,
+    Read_symbols_data*)
+{
+}
+
+// Add the symbols to the symbol table.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_add_symbols(
+    Symbol_table* symtab,
+    Read_symbols_data*,
+    Layout*)
+{
+  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+  unsigned char symbuf[sym_size];
+  elfcpp::Sym<size, big_endian> sym(symbuf);
+  elfcpp::Sym_write<size, big_endian> osym(symbuf);
+
+  typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
+
+  unsigned int nsyms = this->input_reader_.get_global_symbol_count();
+  this->symbols_.resize(nsyms);
+
+  Incremental_binary::View symtab_view(NULL);
+  unsigned int symtab_count;
+  elfcpp::Elf_strtab strtab(NULL, 0);
+  this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
+
+  // Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
+  // Incremental_relocs_reader<size, big_endian> irelocs(this->ibase_->relocs_reader());
+  // unsigned int isym_count = isymtab.symbol_count();
+  // unsigned int first_global = symtab_count - isym_count;
+
+  unsigned const char* sym_p;
+  for (unsigned int i = 0; i < nsyms; ++i)
+    {
+      typename Input_entry_reader::Global_symbol_info info =
+	  this->input_reader_.get_global_symbol_info(i);
+      sym_p = symtab_view.data() + info.output_symndx * sym_size;
+      elfcpp::Sym<size, big_endian> gsym(sym_p);
+      const char* name;
+      if (!strtab.get_c_string(gsym.get_st_name(), &name))
+	name = "";
+
+      typename elfcpp::Elf_types<size>::Elf_Addr v;
+      unsigned int shndx;
+      elfcpp::STB st_bind = gsym.get_st_bind();
+      elfcpp::STT st_type = gsym.get_st_type();
+
+      // Local hidden symbols start out as globals, but get converted to
+      // to local during output.
+      if (st_bind == elfcpp::STB_LOCAL)
+        st_bind = elfcpp::STB_GLOBAL;
+
+      if (!info.is_def)
+	{
+	  shndx = elfcpp::SHN_UNDEF;
+	  v = 0;
+	}
+      else
+	{
+	  // For a symbol defined in a shared object, the section index
+	  // is meaningless, as long as it's not SHN_UNDEF.
+	  shndx = 1;
+	  v = gsym.get_st_value();
+	}
+
+      osym.put_st_name(0);
+      osym.put_st_value(v);
+      osym.put_st_size(gsym.get_st_size());
+      osym.put_st_info(st_bind, st_type);
+      osym.put_st_other(gsym.get_st_other());
+      osym.put_st_shndx(shndx);
+
+      this->symbols_[i] =
+	symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
+    }
+}
+
+// Return TRUE if we should include this object from an archive library.
+
+template<int size, bool big_endian>
+Archive::Should_include
+Sized_incr_dynobj<size, big_endian>::do_should_include_member(
+    Symbol_table*,
+    Layout*,
+    Read_symbols_data*,
+    std::string*)
+{
+  gold_unreachable();
+}
+
+// Iterate over global symbols, calling a visitor class V for each.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
+    Read_symbols_data*,
+    Library_base::Symbol_visitor_base&)
+{
+  // FIXME: Implement Sized_incr_dynobj::do_for_all_global_symbols.
+}
+
+// Iterate over local symbols, calling a visitor class V for each GOT offset
+// associated with a local symbol.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
+    Got_offset_list::Visitor&) const
+{
+  // FIXME: Implement Sized_incr_dynobj::do_for_all_local_got_entries.
+}
+
+// Get the size of a section.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Get the name of a section.
+
+template<int size, bool big_endian>
+std::string
+Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return a view of the contents of a section.
+
+template<int size, bool big_endian>
+Object::Location
+Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section flags.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section entsize.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section address.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return section type.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section link field.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section link field.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the section alignment.
+
+template<int size, bool big_endian>
+uint64_t
+Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
+{
+  gold_unreachable();
+}
+
+// Return the Xindex structure to use.
+
+template<int size, bool big_endian>
+Xindex*
+Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
+{
+  gold_unreachable();
+}
+
+// Get symbol counts.
+
+template<int size, bool big_endian>
+void
+Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
+    const Symbol_table*, size_t*, size_t*) const
+{
+  gold_unreachable();
+}
+
+// Allocate an incremental object of the appropriate size and endianness.
+
+Object*
+make_sized_incremental_object(
+    Incremental_binary* ibase,
+    unsigned int input_file_index,
+    Incremental_input_type input_type,
+    const Incremental_binary::Input_reader* input_reader)
+{
+  Object* obj = NULL;
+  std::string name(input_reader->filename());
+
+  switch (parameters->size_and_endianness())
+    {
+#ifdef HAVE_TARGET_32_LITTLE
+    case Parameters::TARGET_32_LITTLE:
+      {
+	Sized_incremental_binary<32, false>* sized_ibase =
+	    static_cast<Sized_incremental_binary<32, false>*>(ibase);
+	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
+	  obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
+						 input_file_index);
+	else
+	  obj = new Sized_incr_relobj<32, false>(name, sized_ibase,
+						 input_file_index);
+      }
+      break;
+#endif
+#ifdef HAVE_TARGET_32_BIG
+    case Parameters::TARGET_32_BIG:
+      {
+	Sized_incremental_binary<32, true>* sized_ibase =
+	    static_cast<Sized_incremental_binary<32, true>*>(ibase);
+	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
+	  obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
+						input_file_index);
+	else
+	  obj = new Sized_incr_relobj<32, true>(name, sized_ibase,
+						input_file_index);
+      }
+      break;
+#endif
+#ifdef HAVE_TARGET_64_LITTLE
+    case Parameters::TARGET_64_LITTLE:
+      {
+	Sized_incremental_binary<64, false>* sized_ibase =
+	    static_cast<Sized_incremental_binary<64, false>*>(ibase);
+	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
+	  obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
+						 input_file_index);
+	else
+	  obj = new Sized_incr_relobj<64, false>(name, sized_ibase,
+						 input_file_index);
+     }
+      break;
+#endif
+#ifdef HAVE_TARGET_64_BIG
+    case Parameters::TARGET_64_BIG:
+      {
+	Sized_incremental_binary<64, true>* sized_ibase =
+	    static_cast<Sized_incremental_binary<64, true>*>(ibase);
+	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
+	  obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
+						input_file_index);
+	else
+	  obj = new Sized_incr_relobj<64, true>(name, sized_ibase,
+						input_file_index);
+      }
+      break;
+#endif
+    default:
+      gold_unreachable();
+    }
+
+  gold_assert(obj != NULL);
+  return obj;
+}
+
+// Copy the unused symbols from the incremental input info.
+// We need to do this because we may be overwriting the incremental
+// input info in the base file before we write the new incremental
+// info.
+void
+Incremental_library::copy_unused_symbols()
+{ 
+  unsigned int symcount = this->input_reader_->get_unused_symbol_count();
+  this->unused_symbols_.reserve(symcount);
+  for (unsigned int i = 0; i < symcount; ++i)
+    {
+      std::string name(this->input_reader_->get_unused_symbol(i));
+      this->unused_symbols_.push_back(name);
+    }
+}
+
+// Iterator for unused global symbols in the library.
+void
+Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base& v) const
+{
+  for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
+       p != this->unused_symbols_.end();
+       ++p)
+  v.visit(p->c_str());
+}
+
 // Instantiate the templates we need.
 
 #ifdef HAVE_TARGET_32_LITTLE
 template
 class Sized_incremental_binary<32, false>;
+
+template
+class Sized_incr_relobj<32, false>;
+
+template
+class Sized_incr_dynobj<32, false>;
 #endif
 
 #ifdef HAVE_TARGET_32_BIG
 template
 class Sized_incremental_binary<32, true>;
+
+template
+class Sized_incr_relobj<32, true>;
+
+template
+class Sized_incr_dynobj<32, true>;
 #endif
 
 #ifdef HAVE_TARGET_64_LITTLE
 template
 class Sized_incremental_binary<64, false>;
+
+template
+class Sized_incr_relobj<64, false>;
+
+template
+class Sized_incr_dynobj<64, false>;
 #endif
 
 #ifdef HAVE_TARGET_64_BIG
 template
 class Sized_incremental_binary<64, true>;
+
+template
+class Sized_incr_relobj<64, true>;
+
+template
+class Sized_incr_dynobj<64, true>;
 #endif
 
 } // End namespace gold.
diff --git a/gold/incremental.h b/gold/incremental.h
index 8e959a1..e3d5c7a 100644
--- a/gold/incremental.h
+++ b/gold/incremental.h
@@ -31,11 +31,11 @@
 #include "workqueue.h"
 #include "fileread.h"
 #include "output.h"
+#include "archive.h"
 
 namespace gold
 {
 
-class Archive;
 class Input_argument;
 class Incremental_inputs_checker;
 class Incremental_script_entry;
@@ -43,7 +43,9 @@ class Incremental_object_entry;
 class Incremental_archive_entry;
 class Incremental_inputs;
 class Incremental_binary;
+class Incremental_library;
 class Object;
+class Script_info;
 
 // Incremental input type as stored in .gnu_incremental_inputs.
 
@@ -62,43 +64,15 @@ enum Incremental_input_type
 Incremental_binary*
 open_incremental_binary(Output_file* file);
 
-// Code invoked early during an incremental link that checks what files need
-// to be relinked.
-
-class Incremental_checker
-{
- public:
-  // Check if the file named OUTPUT_NAME can be linked incrementally.
-  // INCREMENTAL_INPUTS must have the canonical form of the command line
-  // and input arguments filled - at this point of linking other fields are
-  // probably not filled yet.  TODO: for inputs that don't need to be
-  // rebuilt, this function should fill the incremental input information.
-  Incremental_checker(const char* output_name,
-                      Incremental_inputs* incremental_inputs)
-    : output_name_(output_name), incremental_inputs_(incremental_inputs)
-  { }
-
-  // Analyzes the output file to check if incremental linking is possible and
-  // what files needs to be relinked.
-  bool
-  can_incrementally_link_output_file();
-
- private:
-  // Name of the output file to analyze.
-  const char* output_name_;
-
-  // The Incremental_inputs object. At this stage of link, only the command
-  // line and inputs are filled.
-  Incremental_inputs* incremental_inputs_;
-};
-
 // Base class for recording each input file.
 
 class Incremental_input_entry
 {
  public:
-  Incremental_input_entry(Stringpool::Key filename_key, Timespec mtime)
-    : filename_key_(filename_key), offset_(0), info_offset_(0), mtime_(mtime)
+  Incremental_input_entry(Stringpool::Key filename_key, unsigned int arg_serial,
+			  Timespec mtime)
+    : filename_key_(filename_key), offset_(0), info_offset_(0),
+      arg_serial_(arg_serial), mtime_(mtime)
   { }
 
   virtual
@@ -135,6 +109,11 @@ class Incremental_input_entry
   get_filename_key() const
   { return this->filename_key_; }
 
+  // Get the serial number of the input file.
+  unsigned int
+  arg_serial() const
+  { return this->arg_serial_; }
+
   // Get the modification time of the input file.
   const Timespec&
   get_mtime() const
@@ -191,18 +170,54 @@ class Incremental_input_entry
   // Offset of the extra information in the output section.
   unsigned int info_offset_;
 
+  // Serial number of the file in the argument list.
+  unsigned int arg_serial_;
+
   // Last modification time of the file.
   Timespec mtime_;
 };
 
+// Information about a script input that will persist during the whole linker
+// run.  Needed only during an incremental build to retrieve the input files
+// added by this script.
+
+class Script_info
+{
+ public:
+  Script_info(const std::string& filename)
+    : filename_(filename), incremental_script_entry_(NULL)
+  { }
+
+  // Store a pointer to the incremental information for this script.
+  void
+  set_incremental_info(Incremental_script_entry* entry)
+  { this->incremental_script_entry_ = entry; }
+
+  // Return the filename.
+  const std::string&
+  filename() const
+  { return this->filename_; }
+
+  // Return the pointer to the incremental information for this script.
+  Incremental_script_entry*
+  incremental_info() const
+  { return this->incremental_script_entry_; }
+
+ private:
+  const std::string filename_;
+  Incremental_script_entry* incremental_script_entry_;
+};
+
 // Class for recording input scripts.
 
 class Incremental_script_entry : public Incremental_input_entry
 {
  public:
-  Incremental_script_entry(Stringpool::Key filename_key, Script_info* script,
+  Incremental_script_entry(Stringpool::Key filename_key,
+			   unsigned int arg_serial, Script_info* script,
 			   Timespec mtime)
-    : Incremental_input_entry(filename_key, mtime), script_(script), objects_()
+    : Incremental_input_entry(filename_key, arg_serial, mtime),
+      script_(script), objects_()
   { }
 
   // Add a member object to the archive.
@@ -245,8 +260,8 @@ class Incremental_object_entry : public Incremental_input_entry
 {
  public:
   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
-			   Timespec mtime)
-    : Incremental_input_entry(filename_key, mtime), obj_(obj),
+			   unsigned int arg_serial, Timespec mtime)
+    : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
       is_member_(false), sections_()
   {
     if (!obj_->is_dynamic())
@@ -271,27 +286,27 @@ class Incremental_object_entry : public Incremental_input_entry
   // Add an input section.
   void
   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
-  {
-    if (shndx >= this->sections_.size())
-      this->sections_.resize(shndx + 1);
-    this->sections_[shndx].name_key = name_key;
-    this->sections_[shndx].sh_size = sh_size;
-  }
+  { this->sections_.push_back(Input_section(shndx, name_key, sh_size)); }
 
   // Return the number of input sections in this object.
   unsigned int
   get_input_section_count() const
   { return this->sections_.size(); }
 
+  // Return the input section index for the Nth input section.
+  Stringpool::Key
+  get_input_section_index(unsigned int n) const
+  { return this->sections_[n].shndx_; }
+
   // Return the stringpool key of the Nth input section.
   Stringpool::Key
   get_input_section_name_key(unsigned int n) const
-  { return this->sections_[n].name_key; }
+  { return this->sections_[n].name_key_; }
 
   // Return the size of the Nth input section.
   off_t
   get_input_section_size(unsigned int n) const
-  { return this->sections_[n].sh_size; }
+  { return this->sections_[n].sh_size_; }
 
  protected:
   virtual Incremental_input_type
@@ -319,8 +334,12 @@ class Incremental_object_entry : public Incremental_input_entry
   // Input sections.
   struct Input_section
   {
-    Stringpool::Key name_key;
-    off_t sh_size;
+    Input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
+      : shndx_(shndx), name_key_(name_key), sh_size_(sh_size)
+    { }
+    unsigned int shndx_;
+    Stringpool::Key name_key_;
+    off_t sh_size_;
   };
   std::vector<Input_section> sections_;
 };
@@ -330,8 +349,10 @@ class Incremental_object_entry : public Incremental_input_entry
 class Incremental_archive_entry : public Incremental_input_entry
 {
  public:
-  Incremental_archive_entry(Stringpool::Key filename_key, Timespec mtime)
-    : Incremental_input_entry(filename_key, mtime), members_(), unused_syms_()
+  Incremental_archive_entry(Stringpool::Key filename_key,
+			    unsigned int arg_serial, Timespec mtime)
+    : Incremental_input_entry(filename_key, arg_serial, mtime), members_(),
+      unused_syms_()
   { }
 
   // Add a member object to the archive.
@@ -409,7 +430,8 @@ class Incremental_inputs
 
   // Record the initial info for archive file ARCHIVE.
   void
-  report_archive_begin(Library_base* arch, Script_info* script_info);
+  report_archive_begin(Library_base* arch, unsigned int arg_serial,
+		       Script_info* script_info);
 
   // Record the final info for archive file ARCHIVE.
   void
@@ -418,7 +440,8 @@ class Incremental_inputs
   // Record the info for object file OBJ.  If ARCH is not NULL,
   // attach the object file to the archive.
   void
-  report_object(Object* obj, Library_base* arch, Script_info* script_info);
+  report_object(Object* obj, unsigned int arg_serial, Library_base* arch,
+		Script_info* script_info);
 
   // Record an input section belonging to object file OBJ.
   void
@@ -427,7 +450,7 @@ class Incremental_inputs
 
   // Record the info for input script SCRIPT.
   void
-  report_script(const std::string& filename, Script_info* script,
+  report_script(Script_info* script, unsigned int arg_serial,
 		Timespec mtime);
 
   // Return the running count of incremental relocations.
@@ -594,6 +617,13 @@ class Incremental_inputs_reader
       return this->inputs_->get_string(offset);
     }
 
+    // Return the argument serial number.
+    unsigned int
+    arg_serial() const
+    {
+      return Swap16::readval(this->inputs_->p_ + this->offset_ + 22);
+    }
+
     // Return the timestamp.
     Timespec
     get_mtime() const
@@ -732,7 +762,8 @@ class Incremental_inputs_reader
     // Information about a global symbol.
     struct Global_symbol_info
     {
-      unsigned int output_symndx;
+      bool is_def : 1;
+      unsigned int output_symndx : 31;
       unsigned int next_offset;
       unsigned int reloc_count;
       unsigned int reloc_offset;
@@ -743,15 +774,40 @@ class Incremental_inputs_reader
     get_global_symbol_info(unsigned int n) const
     {
       Global_symbol_info info;
-      unsigned int section_count = this->get_input_section_count();
-      const unsigned char* p = (this->inputs_->p_
-				+ this->info_offset_ + 8
-				+ section_count * input_section_entry_size
-				+ n * 16);
-      info.output_symndx = Swap32::readval(p);
-      info.next_offset = Swap32::readval(p + 4);
-      info.reloc_count = Swap::readval(p + 8);
-      info.reloc_offset = Swap::readval(p + 12);
+      switch (this->type_)
+	{
+	case INCREMENTAL_INPUT_OBJECT:
+	case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
+	  {
+	    unsigned int section_count = this->get_input_section_count();
+	    const unsigned char* p = (this->inputs_->p_
+				      + this->info_offset_ + 8
+				      + section_count * input_section_entry_size
+				      + n * 16);
+	    unsigned int output_symndx = Swap32::readval(p);
+	    info.output_symndx = output_symndx & ((1U << 31) - 1);
+	    info.is_def = (output_symndx & (1U << 31)) != 0;
+	    info.next_offset = Swap32::readval(p + 4);
+	    info.reloc_count = Swap32::readval(p + 8);
+	    info.reloc_offset = Swap32::readval(p + 12);
+	  }
+	  break;
+	case INCREMENTAL_INPUT_SHARED_LIBRARY:
+	  {
+	    const unsigned char* p = (this->inputs_->p_
+				      + this->info_offset_ + 4
+				      + n * 4);
+	    unsigned int output_symndx = Swap32::readval(p);
+	    info.output_symndx = output_symndx & ((1U << 31) - 1);
+	    info.is_def = (output_symndx & (1U << 31)) != 0;
+	    info.next_offset = 0;
+	    info.reloc_count = 0;
+	    info.reloc_offset = 0;
+	  }
+	  break;
+	default:
+	  gold_unreachable();
+	}
       return info;
     }
 
@@ -768,13 +824,34 @@ class Incremental_inputs_reader
     unsigned int info_offset_;
   };
 
+  // Return the offset of an input file entry given its index N.
+  unsigned int
+  input_file_offset(unsigned int n) const
+  {
+    gold_assert(n < this->input_file_count_);
+    return 16 + n * 24;
+  }
+
+  // Return the index of an input file entry given its OFFSET.
+  unsigned int
+  input_file_index(unsigned int offset) const
+  {
+    int n = (offset - 16) / 24;
+    gold_assert(input_file_offset(n) == offset);
+    return n;
+  }
+
   // Return a reader for the Nth input file entry.
   Incremental_input_entry_reader
   input_file(unsigned int n) const
+  { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
+
+  // Return a reader for the input file entry at OFFSET.
+  Incremental_input_entry_reader
+  input_file_at_offset(unsigned int offset) const
   {
-    gold_assert(n < this->input_file_count_);
-    Incremental_input_entry_reader input(this, 16 + n * 24);
-    return input;
+    gold_assert(offset < 16 + this->input_file_count_ * 24);
+    return Incremental_input_entry_reader(this, offset);
   }
 
  private:
@@ -964,7 +1041,8 @@ class Incremental_binary
 {
  public:
   Incremental_binary(Output_file* output, Target* target)
-    : output_(output), target_(target)
+    : input_args_map_(), library_map_(), script_map_(),
+      output_(output), target_(target)
   { }
 
   virtual
@@ -974,14 +1052,15 @@ class Incremental_binary
   // Check the .gnu_incremental_inputs section to see whether an incremental
   // build is possible.
   bool
-  check_inputs(Incremental_inputs* incremental_inputs)
-  { return this->do_check_inputs(incremental_inputs); }
+  check_inputs(const Command_line& cmdline,
+	       Incremental_inputs* incremental_inputs)
+  { return this->do_check_inputs(cmdline, incremental_inputs); }
 
   // Report an error.
   void
   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
 
-  // Wrapper class for a sized Incremental_input_entry_reader.
+  // Proxy class for a sized Incremental_input_entry_reader.
   
   class Input_reader
   {
@@ -1005,6 +1084,18 @@ class Incremental_binary
     type() const
     { return this->do_type(); }
 
+    unsigned int
+    arg_serial() const
+    { return this->do_arg_serial(); }
+
+    unsigned int
+    get_unused_symbol_count() const
+    { return this->do_get_unused_symbol_count(); }
+
+    const char*
+    get_unused_symbol(unsigned int n) const
+    { return this->do_get_unused_symbol(n); }
+
    protected:
     virtual const char*
     do_filename() const = 0;
@@ -1014,11 +1105,64 @@ class Incremental_binary
 
     virtual Incremental_input_type
     do_type() const = 0;
+
+    virtual unsigned int
+    do_arg_serial() const = 0;
+
+    virtual unsigned int
+    do_get_unused_symbol_count() const = 0;
+
+    virtual const char*
+    do_get_unused_symbol(unsigned int n) const = 0;
   };
 
-  Input_reader*
-  get_input_reader(const char* filename)
-  { return this->do_get_input_reader(filename); }
+  // Return the number of input files.
+  unsigned int
+  input_file_count() const
+  { return this->do_input_file_count(); }
+
+  // Return an Input_reader for input file N.
+  const Input_reader*
+  get_input_reader(unsigned int n) const
+  { return this->do_get_input_reader(n); }
+
+  // Return TRUE if the input file N has changed since the last link.
+  bool
+  file_has_changed(unsigned int n) const
+  { return this->do_file_has_changed(n); }
+
+  // Return the Input_argument for input file N.  Returns NULL if
+  // the Input_argument is not available.
+  const Input_argument*
+  get_input_argument(unsigned int n) const
+  {
+    const Input_reader* input_file = this->do_get_input_reader(n);
+    unsigned int arg_serial = input_file->arg_serial();
+    if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
+      return NULL;
+    return this->input_args_map_[arg_serial - 1];
+  }
+
+  // Return an Incremental_library for the given input file.
+  Incremental_library*
+  get_library(unsigned int n)
+  { return this->library_map_[n]; }
+
+  // Return a Script_info for the given input file.
+  Script_info*
+  get_script_info(unsigned int n)
+  { return this->script_map_[n]; }
+
+  // Initialize the layout of the output file based on the existing
+  // output file.
+  void
+  init_layout(Layout* layout)
+  { this->do_init_layout(layout); }
+
+  // Mark regions of the input file that must be kept unchanged.
+  void
+  reserve_layout(unsigned int input_file_index)
+  { this->do_reserve_layout(input_file_index); }
 
   // Functions and types for the elfcpp::Elf_file interface.  This
   // permit us to use Incremental_binary as the File template parameter for
@@ -1067,14 +1211,43 @@ class Incremental_binary
   view(Location loc)
   { return View(this->view(loc.file_offset, loc.data_size)); }
 
+  // Return the Output_file.
+  Output_file*
+  output_file()
+  { return this->output_; }
+
  protected:
   // Check the .gnu_incremental_inputs section to see whether an incremental
   // build is possible.
   virtual bool
-  do_check_inputs(Incremental_inputs* incremental_inputs) = 0;
+  do_check_inputs(const Command_line& cmdline,
+		  Incremental_inputs* incremental_inputs) = 0;
+
+  // Return TRUE if input file N has changed since the last incremental link.
+  virtual bool
+  do_file_has_changed(unsigned int n) const = 0;
+
+  // Initialize the layout of the output file based on the existing
+  // output file.
+  virtual void
+  do_init_layout(Layout* layout) = 0;
+
+  // Mark regions of the input file that must be kept unchanged.
+  virtual void
+  do_reserve_layout(unsigned int input_file_index) = 0;
 
-  virtual Input_reader*
-  do_get_input_reader(const char* filename) = 0;
+  virtual unsigned int
+  do_input_file_count() const = 0;
+
+  virtual const Input_reader*
+  do_get_input_reader(unsigned int) const = 0;
+
+  // Map from input file index to Input_argument.
+  std::vector<const Input_argument*> input_args_map_;
+  // Map from an input file index to an Incremental_library.
+  std::vector<Incremental_library*> library_map_;
+  // Map from an input file index to a Script_info.
+  std::vector<Script_info*> script_map_;
 
  private:
   // Edited output file object.
@@ -1091,8 +1264,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),
-      has_incremental_info_(false), inputs_reader_(), symtab_reader_(),
-      relocs_reader_(), got_plt_reader_(), current_input_file_(0)
+      section_map_(), has_incremental_info_(false), inputs_reader_(),
+      symtab_reader_(), relocs_reader_(), got_plt_reader_(),
+      input_entry_readers_()
   { this->setup_readers(); }
 
   // Returns TRUE if the file contains incremental info.
@@ -1100,37 +1274,60 @@ class Sized_incremental_binary : public Incremental_binary
   has_incremental_info() const
   { return this->has_incremental_info_; }
 
+  // Return the Output_section for section index SHNDX.
+  Output_section*
+  output_section(unsigned int shndx)
+  { return this->section_map_[shndx]; }
+
   // Readers for the incremental info sections.
 
-  Incremental_inputs_reader<size, big_endian>
+  const Incremental_inputs_reader<size, big_endian>&
   inputs_reader() const
   { return this->inputs_reader_; }
 
-  Incremental_symtab_reader<big_endian>
+  const Incremental_symtab_reader<big_endian>&
   symtab_reader() const
   { return this->symtab_reader_; }
 
-  Incremental_relocs_reader<size, big_endian>
+  const Incremental_relocs_reader<size, big_endian>&
   relocs_reader() const
   { return this->relocs_reader_; }
 
-  Incremental_got_plt_reader<big_endian>
+  const Incremental_got_plt_reader<big_endian>&
   got_plt_reader() const
   { return this->got_plt_reader_; }
 
+  void
+  get_symtab_view(View* symtab_view, unsigned int* sym_count,
+		  elfcpp::Elf_strtab* strtab);
+
  protected:
+  typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
+  typedef typename Inputs_reader::Incremental_input_entry_reader
+      Input_entry_reader;
+
+  virtual bool
+  do_check_inputs(const Command_line& cmdline,
+		  Incremental_inputs* incremental_inputs);
+
+  // Return TRUE if input file N has changed since the last incremental link.
   virtual bool
-  do_check_inputs(Incremental_inputs* incremental_inputs);
+  do_file_has_changed(unsigned int n) const;
 
-  // Wrapper class for a sized Incremental_input_entry_reader.
+  // Initialize the layout of the output file based on the existing
+  // output file.
+  virtual void
+  do_init_layout(Layout* layout);
+
+  // Mark regions of the input file that must be kept unchanged.
+  virtual void
+  do_reserve_layout(unsigned int input_file_index);
+
+  // Proxy class for a sized Incremental_input_entry_reader.
   
   class Sized_input_reader : public Input_reader
   {
    public:
-    typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
-    typedef typename Inputs_reader::Incremental_input_entry_reader
-        Input_entry_reader;
-
     Sized_input_reader(Input_entry_reader r)
       : Input_reader(), reader_(r)
     { }
@@ -1152,11 +1349,31 @@ class Sized_incremental_binary : public Incremental_binary
     do_type() const
     { return this->reader_.type(); }
 
+    unsigned int
+    do_arg_serial() const
+    { return this->reader_.arg_serial(); }
+
+    unsigned int
+    do_get_unused_symbol_count() const
+    { return this->reader_.get_unused_symbol_count(); }
+
+    const char*
+    do_get_unused_symbol(unsigned int n) const
+    { return this->reader_.get_unused_symbol(n); }
+
     Input_entry_reader reader_;
   };
 
-  virtual Input_reader*
-  do_get_input_reader(const char* filename);
+  virtual unsigned int
+  do_input_file_count() const
+  { return this->inputs_reader_.input_file_count(); }
+
+  virtual const Input_reader*
+  do_get_input_reader(unsigned int n) const
+  {
+    gold_assert(n < this->input_entry_readers_.size());
+    return &this->input_entry_readers_[n];
+  }
 
  private:
   bool
@@ -1172,15 +1389,416 @@ class Sized_incremental_binary : public Incremental_binary
   // Output as an ELF file.
   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
 
+  // Map section index to an Output_section in the updated layout.
+  std::vector<Output_section*> section_map_;
+
   // Readers for the incremental info sections.
   bool has_incremental_info_;
   Incremental_inputs_reader<size, big_endian> inputs_reader_;
   Incremental_symtab_reader<big_endian> symtab_reader_;
   Incremental_relocs_reader<size, big_endian> relocs_reader_;
   Incremental_got_plt_reader<big_endian> got_plt_reader_;
+  std::vector<Sized_input_reader> input_entry_readers_;
+};
+
+// An incremental Relobj.  This class represents a relocatable object
+// that has not changed since the last incremental link, and whose contents
+// can be used directly from the base file.
+
+template<int size, bool big_endian>
+class Sized_incr_relobj : public Relobj
+{
+ public:
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+
+  static const Address invalid_address = static_cast<Address>(0) - 1;
+
+  Sized_incr_relobj(const std::string& name,
+		    Sized_incremental_binary<size, big_endian>* ibase,
+		    unsigned int input_file_index)
+    : Relobj(name, NULL), ibase_(ibase),
+      input_file_index_(input_file_index),
+      input_reader_(ibase->inputs_reader().input_file(input_file_index)),
+      symbols_(), section_offsets_()
+  { }
 
-  // Index of the current input file entry.
-  int current_input_file_;
+  // Checks if the offset of input section SHNDX within its output
+  // section is invalid. 
+  bool
+  is_output_section_offset_invalid(unsigned int shndx) const
+  { return this->section_offsets_[shndx] == invalid_address; }
+
+  // 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
+  // output_sections_ field of the parent class Incrobj.
+  uint64_t
+  do_output_section_offset(unsigned int shndx) const
+  {
+    gold_assert(shndx < this->section_offsets_.size());
+    Address off = this->section_offsets_[shndx];
+    if (off == invalid_address)
+      return -1ULL;
+    return off;
+  }
+
+ private:
+  typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
+  typedef typename Inputs_reader::Incremental_input_entry_reader
+      Input_entry_reader;
+
+  // Return TRUE if this is an incremental (unchanged) input file.
+  bool
+  do_is_incremental() const
+  { return true; }
+
+  // Return the last modified time of the file.
+  Timespec
+  do_get_mtime()
+  { return this->input_reader_.get_mtime(); }
+
+  // Read the symbols.
+  void
+  do_read_symbols(Read_symbols_data*);
+
+  // Lay out the input sections.
+  void
+  do_layout(Symbol_table*, Layout*, Read_symbols_data*);
+
+  // Layout sections whose layout was deferred while waiting for
+  // input files from a plugin.
+  void
+  do_layout_deferred_sections(Layout*);
+
+  // Add the symbols to the symbol table.
+  void
+  do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
+
+  Archive::Should_include
+  do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
+                           std::string* why);
+
+  // Iterate over global symbols, calling a visitor class V for each.
+  void
+  do_for_all_global_symbols(Read_symbols_data* sd,
+			    Library_base::Symbol_visitor_base& v);
+
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const;
+
+  // Get the size of a section.
+  uint64_t
+  do_section_size(unsigned int shndx);
+
+  // Get the name of a section.
+  std::string
+  do_section_name(unsigned int shndx);
+
+  // Return a view of the contents of a section.
+  Object::Location
+  do_section_contents(unsigned int shndx);
+
+  // Return section flags.
+  uint64_t
+  do_section_flags(unsigned int shndx);
+
+  // Return section entsize.
+  uint64_t
+  do_section_entsize(unsigned int shndx);
+
+  // Return section address.
+  uint64_t
+  do_section_address(unsigned int shndx);
+
+  // Return section type.
+  unsigned int
+  do_section_type(unsigned int shndx);
+
+  // Return the section link field.
+  unsigned int
+  do_section_link(unsigned int shndx);
+
+  // Return the section link field.
+  unsigned int
+  do_section_info(unsigned int shndx);
+
+  // Return the section alignment.
+  uint64_t
+  do_section_addralign(unsigned int shndx);
+
+  // Return the Xindex structure to use.
+  Xindex*
+  do_initialize_xindex();
+
+  // Get symbol counts.
+  void
+  do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
+
+  // Get global symbols.
+  const Symbols*
+  do_get_global_symbols() const
+  { return &this->symbols_; }
+
+  // Return the number of local symbols.
+  unsigned int
+  do_local_symbol_count() const
+  { return 0; }
+
+  // Read the relocs.
+  void
+  do_read_relocs(Read_relocs_data*);
+
+  // Process the relocs to find list of referenced sections. Used only
+  // during garbage collection.
+  void
+  do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
+
+  // Scan the relocs and adjust the symbol table.
+  void
+  do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
+
+  // Count the local symbols.
+  void
+  do_count_local_symbols(Stringpool_template<char>*,
+			 Stringpool_template<char>*);
+
+  // Finalize the local symbols.
+  unsigned int
+  do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
+
+  // Set the offset where local dynamic symbol information will be stored.
+  unsigned int
+  do_set_local_dynsym_indexes(unsigned int);
+
+  // Set the offset where local dynamic symbol information will be stored.
+  unsigned int
+  do_set_local_dynsym_offset(off_t);
+
+  // Relocate the input sections and write out the local symbols.
+  void
+  do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
+
+  // Set the offset of a section.
+  void
+  do_set_section_offset(unsigned int shndx, uint64_t off);
+
+  // The Incremental_binary base file.
+  Sized_incremental_binary<size, big_endian>* ibase_;
+  // The index of the object in the input file list.
+  unsigned int input_file_index_;
+  // The reader for the input file.
+  Input_entry_reader input_reader_;
+  // The entries in the symbol table for the external symbols.
+  Symbols symbols_;
+  // 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.
+  std::vector<Address> section_offsets_;
+};
+
+// An incremental Dynobj.  This class represents a shared object that has
+// not changed since the last incremental link, and whose contents can be
+// used directly from the base file.
+
+template<int size, bool big_endian>
+class Sized_incr_dynobj : public Dynobj
+{
+ public:
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+
+  static const Address invalid_address = static_cast<Address>(0) - 1;
+
+  Sized_incr_dynobj(const std::string& name,
+		    Sized_incremental_binary<size, big_endian>* ibase,
+		    unsigned int input_file_index)
+    : Dynobj(name, NULL), ibase_(ibase),
+      input_file_index_(input_file_index),
+      input_reader_(ibase->inputs_reader().input_file(input_file_index)),
+      symbols_()
+  { }
+
+ private:
+  typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
+  typedef typename Inputs_reader::Incremental_input_entry_reader
+      Input_entry_reader;
+
+  // Return TRUE if this is an incremental (unchanged) input file.
+  bool
+  do_is_incremental() const
+  { return true; }
+
+  // Return the last modified time of the file.
+  Timespec
+  do_get_mtime()
+  { return this->input_reader_.get_mtime(); }
+
+  // Read the symbols.
+  void
+  do_read_symbols(Read_symbols_data*);
+
+  // Lay out the input sections.
+  void
+  do_layout(Symbol_table*, Layout*, Read_symbols_data*);
+
+  // Add the symbols to the symbol table.
+  void
+  do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
+
+  Archive::Should_include
+  do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
+                           std::string* why);
+
+  // Iterate over global symbols, calling a visitor class V for each.
+  void
+  do_for_all_global_symbols(Read_symbols_data* sd,
+			    Library_base::Symbol_visitor_base& v);
+
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const;
+
+  // Get the size of a section.
+  uint64_t
+  do_section_size(unsigned int shndx);
+
+  // Get the name of a section.
+  std::string
+  do_section_name(unsigned int shndx);
+
+  // Return a view of the contents of a section.
+  Object::Location
+  do_section_contents(unsigned int shndx);
+
+  // Return section flags.
+  uint64_t
+  do_section_flags(unsigned int shndx);
+
+  // Return section entsize.
+  uint64_t
+  do_section_entsize(unsigned int shndx);
+
+  // Return section address.
+  uint64_t
+  do_section_address(unsigned int shndx);
+
+  // Return section type.
+  unsigned int
+  do_section_type(unsigned int shndx);
+
+  // Return the section link field.
+  unsigned int
+  do_section_link(unsigned int shndx);
+
+  // Return the section link field.
+  unsigned int
+  do_section_info(unsigned int shndx);
+
+  // Return the section alignment.
+  uint64_t
+  do_section_addralign(unsigned int shndx);
+
+  // Return the Xindex structure to use.
+  Xindex*
+  do_initialize_xindex();
+
+  // Get symbol counts.
+  void
+  do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
+
+  // Get global symbols.
+  const Symbols*
+  do_get_global_symbols() const
+  { return &this->symbols_; }
+
+  // The Incremental_binary base file.
+  Sized_incremental_binary<size, big_endian>* ibase_;
+  // The index of the object in the input file list.
+  unsigned int input_file_index_;
+  // The reader for the input file.
+  Input_entry_reader input_reader_;
+  // The entries in the symbol table for the external symbols.
+  Symbols symbols_;
+};
+
+// Allocate an incremental object of the appropriate size and endianness.
+Object*
+make_sized_incremental_object(
+    Incremental_binary* base,
+    unsigned int input_file_index,
+    Incremental_input_type input_type,
+    const Incremental_binary::Input_reader* input_reader);
+
+// This class represents an Archive library (or --start-lib/--end-lib group)
+// that has not changed since the last incremental link.  Its contents come
+// from the incremental inputs entry in the base file.
+
+class Incremental_library : public Library_base
+{
+ public:
+  Incremental_library(const char* filename, unsigned int input_file_index,
+		      const Incremental_binary::Input_reader* input_reader)
+    : Library_base(NULL), filename_(filename),
+      input_file_index_(input_file_index), input_reader_(input_reader),
+      unused_symbols_(), is_reported_(false)
+  { }
+
+  // Return the input file index.
+  unsigned int
+  input_file_index() const
+  { return this->input_file_index_; }
+
+  // Return the serial number of the input file.
+  unsigned int
+  arg_serial() const
+  { return this->input_reader_->arg_serial(); }
+
+  // Copy the unused symbols from the incremental input info.
+  // We need to do this because we may be overwriting the incremental
+  // input info in the base file before we write the new incremental
+  // info.
+  void
+  copy_unused_symbols();
+
+  // Return FALSE on the first call to indicate that the library needs
+  // to be recorded; return TRUE subsequently.
+  bool
+  is_reported()
+  {
+    bool was_reported = this->is_reported_;
+    is_reported_ = true;
+    return was_reported;
+  }
+
+ private:
+  typedef std::vector<std::string> Symbol_list;
+
+  // The file name.
+  const std::string&
+  do_filename() const
+  { return this->filename_; }
+
+  // Return the modification time of the archive file.
+  Timespec
+  do_get_mtime()
+  { return this->input_reader_->get_mtime(); }
+
+  // Iterator for unused global symbols in the library.
+  void
+  do_for_all_unused_symbols(Symbol_visitor_base& v) const;
+
+  // The name of the library.
+  std::string filename_;
+  // The input file index of this library.
+  unsigned int input_file_index_;
+  // A reader for the incremental input information.
+  const Incremental_binary::Input_reader* input_reader_;
+  // List of unused symbols defined in this library.
+  Symbol_list unused_symbols_;
+  // TRUE when this library has been reported to the new incremental info.
+  bool is_reported_;
 };
 
 } // End namespace gold.
diff --git a/gold/layout.cc b/gold/layout.cc
index 2f56d27..a5b587d 100644
--- a/gold/layout.cc
+++ b/gold/layout.cc
@@ -55,6 +55,115 @@
 namespace gold
 {
 
+// Class Free_list.
+
+// Initialize the free list.
+
+void
+Free_list::init(off_t len, bool extend)
+{
+  this->list_.push_front(Free_list_node(0, len));
+  this->extend_ = extend;
+  this->length_ = len;
+}
+
+// Remove a chunk from the free list.  Because we start with a single
+// node that covers the entire section, and remove chunks from it one
+// at a time, we do not need to coalesce chunks or handle cases that
+// span more than one free node.  We expect to remove chunks from the
+// free list in order, and we expect to have only a few chunks of free
+// space left (corresponding to files that have changed since the last
+// incremental link), so a simple linear list should provide sufficient
+// performance.
+
+void
+Free_list::remove(off_t start, off_t end)
+{
+  if (start == end)
+    return;
+  gold_assert(start < end);
+
+  for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
+    {
+      // Find a node that wholly contains the indicated region.
+      if (p->start_ <= start && p->end_ >= end)
+	{
+	  // Case 1: the indicated region spans the whole node.
+	  // Add some fuzz to avoid creating tiny free chunks.
+	  if (p->start_ == start && p->end_ <= end + 3)
+	    this->list_.erase(p);
+	  // Case 2: remove a chunk from the start of the node.
+	  else if (p->start_ == start)
+	    p->start_ = end;
+	  // Case 3: remove a chunk from the end of the node.
+	  else if (p->end_ <= end + 3)
+	    p->end_ = start;
+	  // Case 4: remove a chunk from the middle, and split
+	  // the node into two.
+	  else
+	    {
+	      Free_list_node newnode(p->start_, start);
+	      p->start_ = end;
+	      this->list_.insert(p, newnode);
+	    }
+	  return;
+	}
+    }
+  // Did not find a node containing the given chunk.  This could happen
+  // because a small chunk was already removed due to the fuzz.
+  gold_debug(DEBUG_INCREMENTAL,
+	     "Free_list::remove(%d,%d) not found",
+	     static_cast<int>(start), static_cast<int>(end));
+}
+
+// Allocate a chunk of size LEN from the free list.  Returns -1ULL
+// if a sufficiently large chunk of free space is not found.
+// We use a simple first-fit algorithm.
+
+off_t
+Free_list::allocate(off_t len, uint64_t align, off_t minoff)
+{
+  gold_debug(DEBUG_INCREMENTAL,
+  	     "Free_list::allocate(%08lx, %d, %08lx)",
+  	     static_cast<long>(len), static_cast<int>(align),
+  	     static_cast<long>(minoff));
+  if (len == 0)
+    return align_address(minoff, align);
+  for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
+    {
+      off_t start = p->start_ > minoff ? p->start_ : minoff;
+      start = align_address(start, align);
+      off_t end = start + len;
+      if (end <= p->end_)
+	{
+	  if (p->start_ == start && p->end_ <= end + 3)
+	    this->list_.erase(p);
+	  else if (p->start_ == start)
+	    p->start_ = end;
+	  else if (p->end_ <= end + 3)
+	    p->end_ = start;
+	  else
+	    {
+	      Free_list_node newnode(p->start_, start);
+	      p->start_ = end;
+	      this->list_.insert(p, newnode);
+	    }
+	  return start;
+	}
+    }
+  return -1;
+}
+
+// Dump the free list (for debugging).
+void
+Free_list::dump()
+{
+  gold_info("Free list:");
+  for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
+    gold_info("  %08lx %08lx", static_cast<long>(p->start_),
+	      static_cast<long>(p->end_));
+}
+
 // Layout::Relaxation_debug_check methods.
 
 // Check that sections and special data are in reset states.
@@ -150,10 +259,19 @@ Layout_task_runner::run(Workqueue* workqueue, const Task* task)
       this->layout_->print_to_mapfile(this->mapfile_);
     }
 
-  Output_file* of = new Output_file(parameters->options().output_file_name());
-  if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
-    of->set_is_temporary();
-  of->open(file_size);
+  Output_file* of;
+  if (this->layout_->incremental_base() == NULL)
+    {
+      of = new Output_file(parameters->options().output_file_name());
+      if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
+	of->set_is_temporary();
+      of->open(file_size);
+    }
+  else
+    {
+      of = this->layout_->incremental_base()->output_file();
+      of->resize(file_size);
+    }
 
   // Queue up the final set of tasks.
   gold::queue_final_tasks(this->options_, this->input_objects_,
@@ -207,7 +325,9 @@ Layout::Layout(int number_of_input_files, Script_options* script_options)
     record_output_section_data_from_script_(false),
     script_output_section_data_list_(),
     segment_states_(NULL),
-    relaxation_debug_check_(NULL)
+    relaxation_debug_check_(NULL),
+    incremental_base_(NULL),
+    free_list_()
 {
   // Make space for more than enough segments for a typical file.
   // This is just for efficiency--it's OK if we wind up needing more.
@@ -226,6 +346,15 @@ Layout::Layout(int number_of_input_files, Script_options* script_options)
   this->namepool_.set_optimize();
 }
 
+// For incremental links, record the base file to be modified.
+
+void
+Layout::set_incremental_base(Incremental_binary* base)
+{
+  this->incremental_base_ = base;
+  this->free_list_.init(base->output_file()->filesize(), true);
+}
+
 // Hash a key we use to look up an output section mapping.
 
 size_t
@@ -594,6 +723,41 @@ Layout::choose_output_section(const Relobj* relobj, const char* name,
   return this->get_output_section(name, name_key, type, flags, order, is_relro);
 }
 
+// For incremental links, record the initial fixed layout of a section
+// from the base file, and return a pointer to the Output_section.
+
+template<int size, bool big_endian>
+Output_section*
+Layout::init_fixed_output_section(const char* name,
+				  elfcpp::Shdr<size, big_endian>& shdr)
+{
+  unsigned int sh_type = shdr.get_sh_type();
+
+  // We preserve the layout of PROGBITS, NOBITS, and NOTE sections.
+  // All others will be created from scratch and reallocated.
+  if (sh_type != elfcpp::SHT_PROGBITS
+      && sh_type != elfcpp::SHT_NOBITS
+      && sh_type != elfcpp::SHT_NOTE)
+    return NULL;
+
+  typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
+  typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
+  typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
+  typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
+  typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
+      shdr.get_sh_addralign();
+
+  // Make the output section.
+  Stringpool::Key name_key;
+  name = this->namepool_.add(name, true, &name_key);
+  Output_section* os = this->get_output_section(name, name_key, sh_type,
+  						sh_flags, ORDER_INVALID, false);
+  os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
+  if (sh_type != elfcpp::SHT_NOBITS)
+    this->free_list_.remove(sh_offset, sh_offset + sh_size);
+  return os;
+}
+
 // Return the output section to use for input section SHNDX, with name
 // NAME, with header HEADER, from object OBJECT.  RELOC_SHNDX is the
 // index of a relocation section which applies to this section, or 0
@@ -824,7 +988,9 @@ Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
       this->eh_frame_section_ = os;
       this->eh_frame_data_ = new Eh_frame();
 
-      if (parameters->options().eh_frame_hdr())
+      // For incremental linking, we do not optimize .eh_frame sections
+      // or create a .eh_frame_hdr section.
+      if (parameters->options().eh_frame_hdr() && !parameters->incremental())
 	{
 	  Output_section* hdr_os =
 	    this->choose_output_section(NULL, ".eh_frame_hdr",
@@ -856,14 +1022,15 @@ Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
 
   gold_assert(this->eh_frame_section_ == os);
 
-  if (this->eh_frame_data_->add_ehframe_input_section(object,
-						      symbols,
-						      symbols_size,
-						      symbol_names,
-						      symbol_names_size,
-						      shndx,
-						      reloc_shndx,
-						      reloc_type))
+  if (!parameters->incremental()
+      && this->eh_frame_data_->add_ehframe_input_section(object,
+							 symbols,
+							 symbols_size,
+							 symbol_names,
+							 symbol_names_size,
+							 shndx,
+							 reloc_shndx,
+							 reloc_type))
     {
       os->update_flags_for_input_section(shdr.get_sh_flags());
 
@@ -2108,7 +2275,8 @@ Layout::create_note(const char* name, int note_type,
 void
 Layout::create_gold_note()
 {
-  if (parameters->options().relocatable())
+  if (parameters->options().relocatable()
+      || parameters->incremental_update())
     return;
 
   std::string desc = std::string("gold ") + gold::get_version_string();
@@ -2776,6 +2944,9 @@ Layout::set_relocatable_section_offsets(Output_data* file_header,
 off_t
 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
 {
+  off_t startoff = off;
+  off_t maxoff = off;
+
   for (Section_list::iterator p = this->unattached_section_list_.begin();
        p != this->unattached_section_list_.end();
        ++p)
@@ -2807,16 +2978,53 @@ Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
                    || (*p)->type() != elfcpp::SHT_STRTAB))
         continue;
 
-      off = align_address(off, (*p)->addralign());
-      (*p)->set_file_offset(off);
-      (*p)->finalize_data_size();
+      if (!parameters->incremental_update())
+	{
+	  off = align_address(off, (*p)->addralign());
+	  (*p)->set_file_offset(off);
+	  (*p)->finalize_data_size();
+	}
+      else
+	{
+	  // Incremental update: allocate file space from free list.
+	  (*p)->pre_finalize_data_size();
+	  off_t current_size = (*p)->current_data_size();
+	  off = this->allocate(current_size, (*p)->addralign(), startoff);
+	  if (off == -1)
+	    {
+	      if (is_debugging_enabled(DEBUG_INCREMENTAL))
+	        this->free_list_.dump();
+	      gold_assert((*p)->output_section() != NULL);
+	      gold_fatal(_("out of patch space for section %s; "
+			   "relink with --incremental-full"),
+			 (*p)->output_section()->name());
+	    }
+	  (*p)->set_file_offset(off);
+	  (*p)->finalize_data_size();
+	  if ((*p)->data_size() > current_size)
+	    {
+	      gold_assert((*p)->output_section() != NULL);
+	      gold_fatal(_("%s: section changed size; "
+			   "relink with --incremental-full"),
+			 (*p)->output_section()->name());
+	    }
+	  gold_debug(DEBUG_INCREMENTAL,
+		     "set_section_offsets: %08lx %08lx %s",
+		     static_cast<long>(off),
+		     static_cast<long>((*p)->data_size()),
+		     ((*p)->output_section() != NULL
+		      ? (*p)->output_section()->name() : "(special)"));
+	}
+
       off += (*p)->data_size();
+      if (off > maxoff)
+        maxoff = off;
 
       // At this point the name must be set.
       if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
 	this->namepool_.add((*p)->name(), false, NULL);
     }
-  return off;
+  return maxoff;
 }
 
 // Set the section indexes of all the sections not associated with a
@@ -2930,9 +3138,8 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
   else
     gold_unreachable();
 
-  off_t off = *poff;
-  off = align_address(off, align);
-  off_t startoff = off;
+  // Compute file offsets relative to the start of the symtab section.
+  off_t off = 0;
 
   // Save space for the dummy symbol at the start of the section.  We
   // never bother to write this out--it will just be left as zero.
@@ -2965,7 +3172,7 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
     }
 
   unsigned int local_symcount = local_symbol_index;
-  gold_assert(static_cast<off_t>(local_symcount * symsize) == off - startoff);
+  gold_assert(static_cast<off_t>(local_symcount * symsize) == off);
 
   off_t dynoff;
   size_t dyn_global_index;
@@ -2986,6 +3193,7 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
 		  == this->dynsym_section_->data_size() - locsize);
     }
 
+  off_t global_off = off;
   off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
 			 &this->sympool_, &local_symcount);
 
@@ -3000,8 +3208,7 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
 							  false);
       this->symtab_section_ = osymtab;
 
-      Output_section_data* pos = new Output_data_fixed_space(off - startoff,
-							     align,
+      Output_section_data* pos = new Output_data_fixed_space(off, align,
 							     "** symtab");
       osymtab->add_output_section_data(pos);
 
@@ -3021,7 +3228,7 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
 				      elfcpp::SHT_SYMTAB_SHNDX, 0,
 				      ORDER_INVALID, false);
 
-	  size_t symcount = (off - startoff) / symsize;
+	  size_t symcount = off / symsize;
 	  this->symtab_xindex_ = new Output_symtab_xindex(symcount);
 
 	  osymtab_xindex->add_output_section_data(this->symtab_xindex_);
@@ -3047,13 +3254,30 @@ Layout::create_symtab_sections(const Input_objects* input_objects,
       Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
       ostrtab->add_output_section_data(pstr);
 
-      osymtab->set_file_offset(startoff);
+      off_t symtab_off;
+      if (!parameters->incremental_update())
+	symtab_off = align_address(*poff, align);
+      else
+	{
+	  symtab_off = this->allocate(off, align, *poff);
+          if (off == -1)
+	    gold_fatal(_("out of patch space for symbol table; "
+		         "relink with --incremental-full"));
+	  gold_debug(DEBUG_INCREMENTAL,
+		     "create_symtab_sections: %08lx %08lx .symtab",
+		     static_cast<long>(symtab_off),
+		     static_cast<long>(off));
+	}
+
+      symtab->set_file_offset(symtab_off + global_off);
+      osymtab->set_file_offset(symtab_off);
       osymtab->finalize_data_size();
       osymtab->set_link_section(ostrtab);
       osymtab->set_info(local_symcount);
       osymtab->set_entsize(symsize);
 
-      *poff = off;
+      if (symtab_off + off > *poff)
+	*poff = symtab_off + off;
     }
 }
 
@@ -3100,10 +3324,25 @@ Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
 				      &this->unattached_section_list_,
 				      &this->namepool_,
 				      shstrtab_section);
-  off_t off = align_address(*poff, oshdrs->addralign());
+  off_t off;
+  if (!parameters->incremental_update())
+    off = align_address(*poff, oshdrs->addralign());
+  else
+    {
+      oshdrs->pre_finalize_data_size();
+      off = this->allocate(oshdrs->data_size(), oshdrs->addralign(), *poff);
+      if (off == -1)
+	  gold_fatal(_("out of patch space for section header table; "
+		       "relink with --incremental-full"));
+      gold_debug(DEBUG_INCREMENTAL,
+		 "create_shdrs: %08lx %08lx (section header table)",
+		 static_cast<long>(off),
+		 static_cast<long>(off + oshdrs->data_size()));
+    }
   oshdrs->set_address_and_file_offset(0, off);
   off += oshdrs->data_size();
-  *poff = off;
+  if (off > *poff)
+    *poff = off;
   this->section_headers_ = oshdrs;
 }
 
@@ -3617,6 +3856,7 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
        ++p)
     {
       if (!(*p)->is_needed()
+	  && !(*p)->is_incremental()
 	  && (*p)->input_file()->options().as_needed())
 	{
 	  // This dynamic object was linked with --as-needed, but it
@@ -4388,6 +4628,38 @@ Close_task_runner::run(Workqueue*, const Task*)
 #ifdef HAVE_TARGET_32_LITTLE
 template
 Output_section*
+Layout::init_fixed_output_section<32, false>(
+    const char* name,
+    elfcpp::Shdr<32, false>& shdr);
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+Output_section*
+Layout::init_fixed_output_section<32, true>(
+    const char* name,
+    elfcpp::Shdr<32, true>& shdr);
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+Output_section*
+Layout::init_fixed_output_section<64, false>(
+    const char* name,
+    elfcpp::Shdr<64, false>& shdr);
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+Output_section*
+Layout::init_fixed_output_section<64, true>(
+    const char* name,
+    elfcpp::Shdr<64, true>& shdr);
+#endif
+
+#ifdef HAVE_TARGET_32_LITTLE
+template
+Output_section*
 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
 			  const char* name,
 			  const elfcpp::Shdr<32, false>& shdr,
diff --git a/gold/layout.h b/gold/layout.h
index 6e817ca..693f3d6 100644
--- a/gold/layout.h
+++ b/gold/layout.h
@@ -41,6 +41,7 @@ namespace gold
 
 class General_options;
 class Incremental_inputs;
+class Incremental_binary;
 class Input_objects;
 class Mapfile;
 class Symbol_table;
@@ -64,6 +65,45 @@ struct Timespec;
 extern bool
 is_compressed_debug_section(const char* secname);
 
+// Maintain a list of free space within a section, segment, or file.
+// Used for incremental update links.
+
+class Free_list
+{
+ public:
+  Free_list()
+    : list_(), extend_(false), length_(0)
+  { }
+
+  void
+  init(off_t len, bool extend);
+
+  void
+  remove(off_t start, off_t end);
+
+  off_t
+  allocate(off_t len, uint64_t align, off_t minoff);
+
+  void
+  dump();
+
+ private:
+  struct Free_list_node
+  {
+    Free_list_node(off_t start, off_t end)
+      : start_(start), end_(end)
+    { }
+    off_t start_;
+    off_t end_;
+  };
+  typedef std::list<Free_list_node>::iterator Iterator;
+  std::list<Free_list_node> list_;
+  // Whether we can extend past the original length.
+  bool extend_;
+  // The total length of the section, segment, or file.
+  off_t length_;
+};
+
 // This task function handles mapping the input sections to output
 // sections and laying them out in memory.
 
@@ -401,6 +441,20 @@ class Layout
     delete this->segment_states_;
   }
 
+  // For incremental links, record the base file to be modified.
+  void
+  set_incremental_base(Incremental_binary* base);
+
+  Incremental_binary*
+  incremental_base()
+  { return this->incremental_base_; }
+
+  // For incremental links, record the initial fixed layout of a section
+  // from the base file, and return a pointer to the Output_section.
+  template<int size, bool big_endian>
+  Output_section*
+  init_fixed_output_section(const char*, elfcpp::Shdr<size, big_endian>&);
+
   // Given an input section SHNDX, named NAME, with data in SHDR, from
   // the object file OBJECT, return the output section where this
   // input section should go.  RELOC_SHNDX is the index of a
@@ -414,6 +468,12 @@ class Layout
 	 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
 	 unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
 
+  // For incremental updates, allocate a block of memory from the
+  // free list.  Find a block starting at or after MINOFF.
+  off_t
+  allocate(off_t len, uint64_t align, off_t minoff)
+  { return this->free_list_.allocate(len, align, minoff); }
+
   unsigned int
   find_section_order_index(const std::string&);
 
@@ -1150,6 +1210,10 @@ class Layout
   Unordered_map<std::string, unsigned int> input_section_position_;
   // Vector of glob only patterns in the section_ordering file.
   std::vector<std::string> input_section_glob_;
+  // For incremental links, the base file to be modified.
+  Incremental_binary* incremental_base_;
+  // For incremental links, a list of free space within the file.
+  Free_list free_list_;
 };
 
 // This task handles writing out data in output sections which is not
diff --git a/gold/object.cc b/gold/object.cc
index e5eb27c..b351ace 100644
--- a/gold/object.cc
+++ b/gold/object.cc
@@ -1265,9 +1265,12 @@ Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
 
 	  // Add the section to the incremental inputs layout.
 	  Incremental_inputs* incremental_inputs = layout->incremental_inputs();
-	  if (incremental_inputs != NULL)
-	    incremental_inputs->report_input_section(this, i,
-						     discard ? NULL : name,
+	  if (incremental_inputs != NULL
+	      && !discard
+	      && (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
+	          || shdr.get_sh_type() == elfcpp::SHT_NOBITS
+	          || shdr.get_sh_type() == elfcpp::SHT_NOTE))
+	    incremental_inputs->report_input_section(this, i, name,
 						     shdr.get_sh_size());
 
           if (discard)
@@ -1716,6 +1719,26 @@ Sized_relobj<size, big_endian>::do_for_all_global_symbols(
     }
 }
 
+// Iterate over local symbols, calling a visitor class V for each GOT offset
+// associated with a local symbol.
+
+template<int size, bool big_endian>
+void
+Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
+    Got_offset_list::Visitor& v) const
+{
+  unsigned int nsyms = this->local_symbol_count();
+  for (unsigned int i = 0; i < nsyms; i++)
+    {
+      Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i);
+      if (p != this->local_got_offsets_.end())
+	{
+	  const Got_offset_list* got_offsets = p->second;
+	  got_offsets->for_all_got_offsets(v);
+	}
+    }
+}
+
 // Return whether the local symbol SYMNDX has a PLT offset.
 
 template<int size, bool big_endian>
@@ -2199,7 +2222,8 @@ Sized_relobj<size, big_endian>::write_local_symbols(
     const Stringpool* sympool,
     const Stringpool* dynpool,
     Output_symtab_xindex* symtab_xindex,
-    Output_symtab_xindex* dynsym_xindex)
+    Output_symtab_xindex* dynsym_xindex,
+    off_t symtab_off)
 {
   const bool strip_all = parameters->options().strip_all();
   if (strip_all)
@@ -2244,7 +2268,8 @@ Sized_relobj<size, big_endian>::write_local_symbols(
   off_t output_size = this->output_local_symbol_count_ * sym_size;
   unsigned char* oview = NULL;
   if (output_size > 0)
-    oview = of->get_output_view(this->local_symbol_offset_, output_size);
+    oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
+				output_size);
 
   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
   unsigned char* dyn_oview = NULL;
@@ -2324,7 +2349,8 @@ Sized_relobj<size, big_endian>::write_local_symbols(
   if (output_size > 0)
     {
       gold_assert(ov - oview == output_size);
-      of->write_output_view(this->local_symbol_offset_, output_size, oview);
+      of->write_output_view(symtab_off + this->local_symbol_offset_,
+			    output_size, oview);
     }
 
   if (dyn_output_size > 0)
diff --git a/gold/object.h b/gold/object.h
index 5daed87..c0ee7eb 100644
--- a/gold/object.h
+++ b/gold/object.h
@@ -203,6 +203,117 @@ class Xindex
   Symtab_xindex symtab_xindex_;
 };
 
+// A GOT offset list.  A symbol may have more than one GOT offset
+// (e.g., when mixing modules compiled with two different TLS models),
+// but will usually have at most one.  GOT_TYPE identifies the type of
+// GOT entry; its values are specific to each target.
+
+class Got_offset_list
+{
+ public:
+  Got_offset_list()
+    : got_type_(-1U), got_offset_(0), got_next_(NULL)
+  { }
+
+  Got_offset_list(unsigned int got_type, unsigned int got_offset)
+    : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
+  { }
+
+  ~Got_offset_list()
+  { 
+    if (this->got_next_ != NULL)
+      {
+        delete this->got_next_;
+        this->got_next_ = NULL;
+      }
+  }
+
+  // Initialize the fields to their default values.
+  void
+  init()
+  {
+    this->got_type_ = -1U;
+    this->got_offset_ = 0;
+    this->got_next_ = NULL;
+  }
+
+  // Set the offset for the GOT entry of type GOT_TYPE.
+  void
+  set_offset(unsigned int got_type, unsigned int got_offset)
+  {
+    if (this->got_type_ == -1U)
+      {
+        this->got_type_ = got_type;
+        this->got_offset_ = got_offset;
+      }
+    else
+      {
+        for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
+          {
+            if (g->got_type_ == got_type)
+              {
+                g->got_offset_ = got_offset;
+                return;
+              }
+          }
+        Got_offset_list* g = new Got_offset_list(got_type, got_offset);
+        g->got_next_ = this->got_next_;
+        this->got_next_ = g;
+      }
+  }
+
+  // Return the offset for a GOT entry of type GOT_TYPE.
+  unsigned int
+  get_offset(unsigned int got_type) const
+  {
+    for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
+      {
+        if (g->got_type_ == got_type)
+          return g->got_offset_;
+      }
+    return -1U;
+  }
+
+  // Return a pointer to the list, or NULL if the list is empty.
+  const Got_offset_list*
+  get_list() const
+  {
+    if (this->got_type_ == -1U)
+      return NULL;
+    return this;
+  }
+
+  // Abstract visitor class for iterating over GOT offsets.
+  class Visitor
+  {
+   public:
+    Visitor()
+    { }
+
+    virtual
+    ~Visitor()
+    { }
+
+    virtual void
+    visit(unsigned int, unsigned int) = 0;
+  };
+
+  // Loop over all GOT offset entries, calling a visitor class V for each.
+  void
+  for_all_got_offsets(Visitor& v) const
+  {
+    if (this->got_type_ == -1U)
+      return;
+    for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
+      v.visit(g->got_type_, g->got_offset_);
+  }
+
+ private:
+  unsigned int got_type_;
+  unsigned int got_offset_;
+  Got_offset_list* got_next_;
+};
+
 // Object is an abstract base class which represents either a 32-bit
 // or a 64-bit input object.  This can be a regular object file
 // (ET_REL) or a shared object (ET_DYN).
@@ -221,10 +332,16 @@ class Object
     : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
       is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
       has_no_split_stack_(false), no_export_(false), xindex_(NULL)
-  { input_file->file().add_object(); }
+  {
+    if (input_file != NULL)
+      input_file->file().add_object();
+  }
 
   virtual ~Object()
-  { this->input_file_->file().remove_object(); }
+  { 
+    if (this->input_file_ != NULL)
+      this->input_file_->file().remove_object();
+  }
 
   // Return the name of the object as we would report it to the tuser.
   const std::string&
@@ -274,42 +391,71 @@ class Object
   // Get the file.  We pass on const-ness.
   Input_file*
   input_file()
-  { return this->input_file_; }
+  {
+    gold_assert(this->input_file_ != NULL);
+    return this->input_file_;
+  }
 
   const Input_file*
   input_file() const
-  { return this->input_file_; }
+  {
+    gold_assert(this->input_file_ != NULL);
+    return this->input_file_;
+  }
 
   // Lock the underlying file.
   void
   lock(const Task* t)
-  { this->input_file()->file().lock(t); }
+  { 
+    if (this->input_file_ != NULL)
+      this->input_file_->file().lock(t);
+  }
 
   // Unlock the underlying file.
   void
   unlock(const Task* t)
-  { this->input_file()->file().unlock(t); }
+  {
+    if (this->input_file_ != NULL)
+      this->input_file()->file().unlock(t);
+  }
 
   // Return whether the underlying file is locked.
   bool
   is_locked() const
-  { return this->input_file()->file().is_locked(); }
+  { return this->input_file_ != NULL && this->input_file_->file().is_locked(); }
 
   // Return the token, so that the task can be queued.
   Task_token*
   token()
-  { return this->input_file()->file().token(); }
+  { 
+    if (this->input_file_ == NULL)
+      return NULL;
+    return this->input_file()->file().token();
+  }
 
   // Release the underlying file.
   void
   release()
-  { this->input_file_->file().release(); }
+  {
+    if (this->input_file_ != NULL)
+      this->input_file()->file().release();
+  }
 
   // Return whether we should just read symbols from this file.
   bool
   just_symbols() const
   { return this->input_file()->just_symbols(); }
 
+  // Return whether this is an incremental object.
+  bool
+  is_incremental() const
+  { return this->do_is_incremental(); }
+
+  // Return the last modified time of the file.
+  Timespec
+  get_mtime()
+  { return this->do_get_mtime(); }
+
   // Get the number of sections.
   unsigned int
   shnum() const
@@ -428,6 +574,12 @@ class Object
 			 Library_base::Symbol_visitor_base& v)
   { return this->do_for_all_global_symbols(sd, v); }
 
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  for_all_local_got_entries(Got_offset_list::Visitor& v) const
+  { this->do_for_all_local_got_entries(v); }
+
   // Functions and types for the elfcpp::Elf_file interface.  This
   // permit us to use Object as the File template parameter for
   // elfcpp::Elf_file.
@@ -504,7 +656,10 @@ class Object
   // Stop caching views in the underlying file.
   void
   clear_view_cache_marks()
-  { this->input_file()->file().clear_view_cache_marks(); }
+  {
+    if (this->input_file_ != NULL)
+      this->input_file_->file().clear_view_cache_marks();
+  }
 
   // Get the number of global symbols defined by this object, and the
   // number of the symbols whose final definition came from this
@@ -561,6 +716,20 @@ class Object
   do_pluginobj()
   { return NULL; }
 
+  // Return TRUE if this is an incremental (unchanged) input file.
+  // We return FALSE by default; the incremental object classes
+  // override this method.
+  virtual bool
+  do_is_incremental() const
+  { return false; }
+
+  // Return the last modified time of the file.  This method may be
+  // overridden for subclasses that don't use an actual file (e.g.,
+  // Incremental objects).
+  virtual Timespec
+  do_get_mtime()
+  { return this->input_file()->file().get_mtime(); }
+
   // Read the symbols--implemented by child class.
   virtual void
   do_read_symbols(Read_symbols_data*) = 0;
@@ -583,6 +752,11 @@ class Object
   do_for_all_global_symbols(Read_symbols_data* sd,
 			    Library_base::Symbol_visitor_base& v) = 0;
 
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  virtual void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const = 0;
+
   // Return the location of the contents of a section.  Implemented by
   // child class.
   virtual Location
@@ -1443,103 +1617,6 @@ class Symbol_value
   } u_;
 };
 
-// A GOT offset list.  A symbol may have more than one GOT offset
-// (e.g., when mixing modules compiled with two different TLS models),
-// but will usually have at most one.  GOT_TYPE identifies the type of
-// GOT entry; its values are specific to each target.
-
-class Got_offset_list
-{
- public:
-  Got_offset_list()
-    : got_type_(-1U), got_offset_(0), got_next_(NULL)
-  { }
-
-  Got_offset_list(unsigned int got_type, unsigned int got_offset)
-    : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
-  { }
-
-  ~Got_offset_list()
-  { 
-    if (this->got_next_ != NULL)
-      {
-        delete this->got_next_;
-        this->got_next_ = NULL;
-      }
-  }
-
-  // Initialize the fields to their default values.
-  void
-  init()
-  {
-    this->got_type_ = -1U;
-    this->got_offset_ = 0;
-    this->got_next_ = NULL;
-  }
-
-  // Set the offset for the GOT entry of type GOT_TYPE.
-  void
-  set_offset(unsigned int got_type, unsigned int got_offset)
-  {
-    if (this->got_type_ == -1U)
-      {
-        this->got_type_ = got_type;
-        this->got_offset_ = got_offset;
-      }
-    else
-      {
-        for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
-          {
-            if (g->got_type_ == got_type)
-              {
-                g->got_offset_ = got_offset;
-                return;
-              }
-          }
-        Got_offset_list* g = new Got_offset_list(got_type, got_offset);
-        g->got_next_ = this->got_next_;
-        this->got_next_ = g;
-      }
-  }
-
-  // Return the offset for a GOT entry of type GOT_TYPE.
-  unsigned int
-  get_offset(unsigned int got_type) const
-  {
-    for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
-      {
-        if (g->got_type_ == got_type)
-          return g->got_offset_;
-      }
-    return -1U;
-  }
-
-  // Return a pointer to the list, or NULL if the list is empty.
-  const Got_offset_list*
-  get_list() const
-  {
-    if (this->got_type_ == -1U)
-      return NULL;
-    return this;
-  }
-
-  // Loop over all GOT offset entries, applying the function F to each.
-  template<typename F>
-  void
-  for_all_got_offsets(F f) const
-  {
-    if (this->got_type_ == -1U)
-      return;
-    for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
-      f(g->got_type_, g->got_offset_);
-  }
-
- private:
-  unsigned int got_type_;
-  unsigned int got_offset_;
-  Got_offset_list* got_next_;
-};
-
 // This type is used to modify relocations for -fsplit-stack.  It is
 // indexed by relocation index, and means that the relocation at that
 // index should use the symbol from the vector, rather than the one
@@ -1726,17 +1803,6 @@ class Sized_relobj : public Relobj
       }
   }
 
-  // Return the GOT offset list for the local symbol SYMNDX.
-  const Got_offset_list*
-  local_got_offset_list(unsigned int symndx) const
-  {
-    Local_got_offsets::const_iterator p =
-        this->local_got_offsets_.find(symndx);
-    if (p == this->local_got_offsets_.end())
-      return NULL;
-    return p->second;
-  }
-
   // Return whether the local symbol SYMNDX has a PLT offset.
   bool
   local_has_plt_offset(unsigned int symndx) const;
@@ -1826,6 +1892,11 @@ class Sized_relobj : public Relobj
   do_for_all_global_symbols(Read_symbols_data* sd,
 			    Library_base::Symbol_visitor_base& v);
 
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const;
+
   // Read the relocs.
   void
   do_read_relocs(Read_relocs_data*);
@@ -2179,7 +2250,8 @@ class Sized_relobj : public Relobj
 		      const Stringpool_template<char>*,
 		      const Stringpool_template<char>*,
 		      Output_symtab_xindex*,
-		      Output_symtab_xindex*);
+		      Output_symtab_xindex*,
+		      off_t);
 
   // Clear the local symbol information.
   void
@@ -2278,9 +2350,9 @@ class Sized_relobj : public Relobj
   Symbols symbols_;
   // Number of symbols defined in object file itself.
   size_t defined_count_;
-  // File offset for local symbols.
+  // File offset for local symbols (relative to start of symbol table).
   off_t local_symbol_offset_;
-  // File offset for local dynamic symbols.
+  // File offset for local dynamic symbols (absolute).
   off_t local_dynsym_offset_;
   // Values of local symbols.
   Local_values local_values_;
diff --git a/gold/options.cc b/gold/options.cc
index 88c2533..ecfa550 100644
--- a/gold/options.cc
+++ b/gold/options.cc
@@ -1215,8 +1215,9 @@ Search_directory::add_sysroot(const char* sysroot,
 // Add a file to the list.
 
 Input_argument&
-Input_arguments::add_file(const Input_file_argument& file)
+Input_arguments::add_file(Input_file_argument& file)
 {
+  file.set_arg_serial(++this->file_count_);
   if (this->in_group_)
     {
       gold_assert(!this->input_argument_list_.empty());
diff --git a/gold/options.h b/gold/options.h
index 8e5c6bb..c716272 100644
--- a/gold/options.h
+++ b/gold/options.h
@@ -57,6 +57,7 @@ class Input_file_lib;
 class Position_dependent_options;
 class Target;
 class Plugin_manager;
+class Script_info;
 
 // Incremental build action for a specific file, as selected by the user.
 
@@ -1511,7 +1512,7 @@ class Input_file_argument
   //         command line, such as --whole-archive.
   Input_file_argument()
     : name_(), type_(INPUT_FILE_TYPE_FILE), extra_search_path_(""),
-      just_symbols_(false), options_()
+      just_symbols_(false), options_(), arg_serial_(0)
   { }
 
   Input_file_argument(const char* name, Input_file_type type,
@@ -1519,7 +1520,7 @@ class Input_file_argument
                       bool just_symbols,
                       const Position_dependent_options& options)
     : name_(name), type_(type), extra_search_path_(extra_search_path),
-      just_symbols_(just_symbols), options_(options)
+      just_symbols_(just_symbols), options_(options), arg_serial_(0)
   { }
 
   // You can also pass in a General_options instance instead of a
@@ -1531,7 +1532,7 @@ class Input_file_argument
                       bool just_symbols,
                       const General_options& options)
     : name_(name), type_(type), extra_search_path_(extra_search_path),
-      just_symbols_(just_symbols), options_(options)
+      just_symbols_(just_symbols), options_(options), arg_serial_(0)
   { }
 
   const char*
@@ -1573,6 +1574,16 @@ class Input_file_argument
 	    || !this->extra_search_path_.empty());
   }
 
+  // Set the serial number for this argument.
+  void
+  set_arg_serial(unsigned int arg_serial)
+  { this->arg_serial_ = arg_serial; }
+
+  // Get the serial number.
+  unsigned int
+  arg_serial() const
+  { return this->arg_serial_; }
+
  private:
   // We use std::string, not const char*, here for convenience when
   // using script files, so that we do not have to preserve the string
@@ -1582,6 +1593,8 @@ class Input_file_argument
   std::string extra_search_path_;
   bool just_symbols_;
   Position_dependent_options options_;
+  // A unique index for this file argument in the argument list.
+  unsigned int arg_serial_;
 };
 
 // A file or library, or a group, from the command line.
@@ -1764,12 +1777,12 @@ class Input_arguments
   typedef Input_argument_list::const_iterator const_iterator;
 
   Input_arguments()
-    : input_argument_list_(), in_group_(false), in_lib_(false)
+    : input_argument_list_(), in_group_(false), in_lib_(false), file_count_(0)
   { }
 
   // Add a file.
   Input_argument&
-  add_file(const Input_file_argument& arg);
+  add_file(Input_file_argument& arg);
 
   // Start a group (the --start-group option).
   void
@@ -1821,6 +1834,7 @@ class Input_arguments
   Input_argument_list input_argument_list_;
   bool in_group_;
   bool in_lib_;
+  unsigned int file_count_;
 };
 
 
diff --git a/gold/output.cc b/gold/output.cc
index ffc5d69..303b0e2 100644
--- a/gold/output.cc
+++ b/gold/output.cc
@@ -280,6 +280,7 @@ Output_segment_headers::Output_segment_headers(
     const Layout::Segment_list& segment_list)
   : segment_list_(segment_list)
 {
+  this->set_current_data_size_for_child(this->do_size());
 }
 
 void
@@ -1794,6 +1795,21 @@ Output_symtab_xindex::endian_do_write(unsigned char* const oview)
 
 // Output_section::Input_section methods.
 
+// Return the current data size.  For an input section we store the size here.
+// For an Output_section_data, we have to ask it for the size.
+
+off_t
+Output_section::Input_section::current_data_size() const
+{
+  if (this->is_input_section())
+    return this->u1_.data_size;
+  else
+    {
+      this->u2_.posd->pre_finalize_data_size();
+      return this->u2_.posd->current_data_size();
+    }
+}
+
 // Return the data size.  For an input section we store the size here.
 // For an Output_section_data, we have to ask it for the size.
 
@@ -2003,9 +2019,11 @@ Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
     section_offsets_need_adjustment_(false),
     is_noload_(false),
     always_keeps_input_sections_(false),
+    has_fixed_layout_(false),
     tls_offset_(0),
     checkpoint_(NULL),
-    lookup_maps_(new Output_section_lookup_maps)
+    lookup_maps_(new Output_section_lookup_maps),
+    free_list_()
 {
   // An unallocated section has no address.  Forcing this means that
   // we don't need special treatment for symbols defined in debug
@@ -2086,9 +2104,11 @@ Output_section::add_input_section(Layout* layout,
   // a Output_data_merge.  We don't try to handle relocations for such
   // a section.  We don't try to handle empty merge sections--they
   // mess up the mappings, and are useless anyhow.
+  // FIXME: Need to handle merge sections during incremental update.
   if ((sh_flags & elfcpp::SHF_MERGE) != 0
       && reloc_shndx == 0
-      && shdr.get_sh_size() > 0)
+      && shdr.get_sh_size() > 0
+      && !parameters->incremental_update())
     {
       // Keep information about merged input sections for rebuilding fast
       // lookup maps if we have sections-script or we do relaxation.
@@ -2105,9 +2125,30 @@ Output_section::add_input_section(Layout* layout,
 	}
     }
 
-  off_t offset_in_section = this->current_data_size_for_child();
-  off_t aligned_offset_in_section = align_address(offset_in_section,
-                                                  addralign);
+  section_size_type input_section_size = shdr.get_sh_size();
+  section_size_type uncompressed_size;
+  if (object->section_is_compressed(shndx, &uncompressed_size))
+    input_section_size = uncompressed_size;
+
+  off_t offset_in_section;
+  off_t aligned_offset_in_section;
+  if (this->has_fixed_layout())
+    {
+      // For incremental updates, find a chunk of unused space in the section.
+      offset_in_section = this->free_list_.allocate(input_section_size,
+						    addralign, 0);
+      if (offset_in_section == -1)
+        gold_fatal(_("out of patch space; relink with --incremental-full"));
+      aligned_offset_in_section = offset_in_section;
+    }
+  else
+    {
+      offset_in_section = this->current_data_size_for_child();
+      aligned_offset_in_section = align_address(offset_in_section,
+						addralign);
+      this->set_current_data_size_for_child(aligned_offset_in_section
+					    + input_section_size);
+    }
 
   // Determine if we want to delay code-fill generation until the output
   // section is written.  When the target is relaxing, we want to delay fill
@@ -2142,14 +2183,6 @@ Output_section::add_input_section(Layout* layout,
         }
     }
 
-  section_size_type input_section_size = shdr.get_sh_size();
-  section_size_type uncompressed_size;
-  if (object->section_is_compressed(shndx, &uncompressed_size))
-    input_section_size = uncompressed_size;
-
-  this->set_current_data_size_for_child(aligned_offset_in_section
-					+ input_section_size);
-
   // We need to keep track of this section if we are already keeping
   // track of sections, or if we are relaxing.  Also, if this is a
   // section which requires sorting, or which may require sorting in
@@ -2176,6 +2209,14 @@ Output_section::add_input_section(Layout* layout,
               this->set_input_section_order_specified();
             }
         }
+      if (this->has_fixed_layout())
+	{
+	  // For incremental updates, finalize the address and offset now.
+	  uint64_t addr = this->address();
+	  isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
+					    aligned_offset_in_section,
+					    this->offset());
+	}
       this->input_sections_.push_back(isecn);
     }
 
@@ -2192,11 +2233,27 @@ Output_section::add_output_section_data(Output_section_data* posd)
 
   if (posd->is_data_size_valid())
     {
-      off_t offset_in_section = this->current_data_size_for_child();
-      off_t aligned_offset_in_section = align_address(offset_in_section,
-						      posd->addralign());
-      this->set_current_data_size_for_child(aligned_offset_in_section
-					    + posd->data_size());
+      off_t offset_in_section;
+      if (this->has_fixed_layout())
+	{
+	  // For incremental updates, find a chunk of unused space.
+	  offset_in_section = this->free_list_.allocate(posd->data_size(),
+							posd->addralign(), 0);
+	  if (offset_in_section == -1)
+	    gold_fatal(_("out of patch space; relink with --incremental-full"));
+	  // Finalize the address and offset now.
+	  uint64_t addr = this->address();
+	  posd->set_address_and_file_offset(addr + offset_in_section,
+					    offset_in_section);
+	}
+      else
+	{
+	  offset_in_section = this->current_data_size_for_child();
+	  off_t aligned_offset_in_section = align_address(offset_in_section,
+							  posd->addralign());
+	  this->set_current_data_size_for_child(aligned_offset_in_section
+						+ posd->data_size());
+	}
     }
 }
 
@@ -2689,6 +2746,30 @@ Output_section::find_starting_output_address(const Relobj* object,
   return false;
 }
 
+// Update the data size of an Output_section.
+
+void
+Output_section::update_data_size()
+{
+  if (this->input_sections_.empty())
+      return;
+
+  if (this->must_sort_attached_input_sections()
+      || this->input_section_order_specified())
+    this->sort_attached_input_sections();
+
+  off_t off = this->first_input_offset_;
+  for (Input_section_list::iterator p = this->input_sections_.begin();
+       p != this->input_sections_.end();
+       ++p)
+    {
+      off = align_address(off, p->addralign());
+      off += p->current_data_size();
+    }
+
+  this->set_current_data_size_for_child(off);
+}
+
 // Set the data size of an Output_section.  This is where we handle
 // setting the addresses of any Output_section_data objects.
 
@@ -3459,6 +3540,30 @@ Output_section::print_merge_stats()
     p->print_merge_stats(this->name_);
 }
 
+// Set a fixed layout for the section.  Used for incremental update links.
+
+void
+Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
+				 off_t sh_size, uint64_t sh_addralign)
+{
+  this->addralign_ = sh_addralign;
+  this->set_current_data_size(sh_size);
+  if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
+    this->set_address(sh_addr);
+  this->set_file_offset(sh_offset);
+  this->finalize_data_size();
+  this->free_list_.init(sh_size, false);
+  this->has_fixed_layout_ = true;
+}
+
+// Reserve space within the fixed layout for the section.  Used for
+// incremental update links.
+void
+Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
+{
+  this->free_list_.remove(sh_offset, sh_offset + sh_size);
+}
+
 // Output segment methods.
 
 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
@@ -3660,7 +3765,7 @@ Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
 // *POFF and *PSHNDX.
 
 uint64_t
-Output_segment::set_section_addresses(const Layout* layout, bool reset,
+Output_segment::set_section_addresses(Layout* layout, bool reset,
                                       uint64_t addr,
 				      unsigned int increase_relro,
 				      off_t* poff,
@@ -3770,13 +3875,16 @@ Output_segment::set_section_addresses(const Layout* layout, bool reset,
 // structures.
 
 uint64_t
-Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
+Output_segment::set_section_list_addresses(Layout* layout, bool reset,
                                            Output_data_list* pdl,
 					   uint64_t addr, off_t* poff,
 					   unsigned int* pshndx,
                                            bool* in_tls)
 {
   off_t startoff = *poff;
+  // For incremental updates, we may allocate non-fixed sections from
+  // free space in the file.  This keeps track of the high-water mark.
+  off_t maxoff = startoff;
 
   off_t off = startoff;
   for (Output_data_list::iterator p = pdl->begin();
@@ -3786,8 +3894,8 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
       if (reset)
 	(*p)->reset_address_and_file_offset();
 
-      // When using a linker script the section will most likely
-      // already have an address.
+      // When doing an incremental update or when using a linker script,
+      // the section will most likely already have an address.
       if (!(*p)->is_address_valid())
 	{
           uint64_t align = (*p)->addralign();
@@ -3825,9 +3933,43 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
                 }
             }
 
-	  off = align_address(off, align);
-	  (*p)->set_address_and_file_offset(addr + (off - startoff), off);
+	  // FIXME: Need to handle TLS and .bss with incremental update.
+	  if (!parameters->incremental_update()
+	      || (*p)->is_section_flag_set(elfcpp::SHF_TLS)
+	      || (*p)->is_section_type(elfcpp::SHT_NOBITS))
+	    {
+	      off = align_address(off, align);
+	      (*p)->set_address_and_file_offset(addr + (off - startoff), off);
+	    }
+	  else
+	    {
+	      // Incremental update: allocate file space from free list.
+	      (*p)->pre_finalize_data_size();
+	      off_t current_size = (*p)->current_data_size();
+	      off = layout->allocate(current_size, align, startoff);
+	      if (off == -1)
+	        {
+		  gold_assert((*p)->output_section() != NULL);
+		  gold_fatal(_("out of patch space for section %s; "
+			       "relink with --incremental-full"),
+			     (*p)->output_section()->name());
+	        }
+	      (*p)->set_address_and_file_offset(addr + (off - startoff), off);
+	      if ((*p)->data_size() > current_size)
+		{
+		  gold_assert((*p)->output_section() != NULL);
+		  gold_fatal(_("%s: section changed size; "
+			       "relink with --incremental-full"),
+			     (*p)->output_section()->name());
+		}
+	    }
 	}
+      else if (parameters->incremental_update())
+        {
+          // For incremental updates, use the fixed offset for the
+          // high-water mark computation.
+          off = (*p)->offset();
+        }
       else
 	{
 	  // The script may have inserted a skip forward, but it
@@ -3861,6 +4003,13 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
 	  (*p)->finalize_data_size();
 	}
 
+      gold_debug(DEBUG_INCREMENTAL,
+		 "set_section_list_addresses: %08lx %08lx %s",
+		 static_cast<long>(off),
+		 static_cast<long>((*p)->data_size()),
+		 ((*p)->output_section() != NULL
+		  ? (*p)->output_section()->name() : "(special)"));
+
       // We want to ignore the size of a SHF_TLS or SHT_NOBITS
       // section.  Such a section does not affect the size of a
       // PT_LOAD segment.
@@ -3868,6 +4017,9 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
 	  || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
 	off += (*p)->data_size();
 
+      if (off > maxoff)
+        maxoff = off;
+
       if ((*p)->is_section())
 	{
 	  (*p)->set_out_shndx(*pshndx);
@@ -3875,8 +4027,8 @@ Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
 	}
     }
 
-  *poff = off;
-  return addr + (off - startoff);
+  *poff = maxoff;
+  return addr + (maxoff - startoff);
 }
 
 // For a non-PT_LOAD segment, set the offset from the sections, if
diff --git a/gold/output.h b/gold/output.h
index 40d8d3f..4f49e4b 100644
--- a/gold/output.h
+++ b/gold/output.h
@@ -81,6 +81,11 @@ class Output_data
     return this->data_size_;
   }
 
+  // Get the current data size.
+  off_t
+  current_data_size() const
+  { return this->current_data_size_for_child(); }
+
   // Return true if data size is fixed.
   bool
   is_data_size_fixed() const
@@ -193,6 +198,17 @@ class Output_data
     this->is_offset_valid_ = true;
   }
 
+  // Update the data size without finalizing it.
+  void
+  pre_finalize_data_size()
+  {
+    if (!this->is_data_size_valid_)
+      {
+	// Tell the child class to update the data size.
+	this->update_data_size();
+      }
+  }
+
   // Finalize the data size.
   void
   finalize_data_size()
@@ -320,6 +336,17 @@ class Output_data
   do_set_out_shndx(unsigned int)
   { gold_unreachable(); }
 
+  // This is a hook for derived classes to set the preliminary data size.
+  // This is called by pre_finalize_data_size, normally called during
+  // Layout::finalize, before the section address is set, and is used
+  // during an incremental update, when we need to know the size of a
+  // section before allocating space in the output file.  For classes
+  // where the current data size is up to date, this default version of
+  // the method can be inherited.
+  virtual void
+  update_data_size()
+  { }
+
   // This is a hook for derived classes to set the data size.  This is
   // called by finalize_data_size, normally called during
   // Layout::finalize, when the section address is set.
@@ -462,6 +489,11 @@ class Output_section_headers : public Output_data
   do_print_to_mapfile(Mapfile* mapfile) const
   { mapfile->print_output_data(this, _("** section headers")); }
 
+  // Update the data size.
+  void
+  update_data_size()
+  { this->set_data_size(this->do_size()); }
+
   // Set final data size.
   void
   set_final_data_size()
@@ -732,11 +764,6 @@ class Output_section_data_build : public Output_section_data
     : Output_section_data(addralign)
   { }
 
-  // Get the current data size.
-  off_t
-  current_data_size() const
-  { return this->current_data_size_for_child(); }
-
   // Set the current data size.
   void
   set_current_data_size(off_t data_size)
@@ -918,6 +945,12 @@ class Output_data_strtab : public Output_section_data
   { }
 
  protected:
+  // This is called to update the section size prior to assigning
+  // the address and file offset.
+  void
+  update_data_size()
+  { this->set_final_data_size(); }
+
   // This is called to set the address and file offset.  Here we make
   // sure that the Stringpool is finalized.
   void
@@ -3032,6 +3065,10 @@ class Output_section : public Output_data
 	}
     }
  
+    // Return the current required size, without finalization.
+    off_t
+    current_data_size() const;
+
     // Return the required size.
     off_t
     data_size() const;
@@ -3255,11 +3292,6 @@ class Output_section : public Output_data
   set_current_data_size(off_t size)
   { this->set_current_data_size_for_child(size); }
 
-  // Get the current size of the output section.
-  off_t
-  current_data_size() const
-  { return this->current_data_size_for_child(); }
-
   // End of linker script support.
 
   // Save states before doing section layout.
@@ -3314,6 +3346,21 @@ class Output_section : public Output_data
   void
   print_merge_stats();
 
+  // Set a fixed layout for the section.  Used for incremental update links.
+  void
+  set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
+		   uint64_t sh_addralign);
+
+  // Return TRUE if the section has a fixed layout.
+  bool
+  has_fixed_layout() const
+  { return this->has_fixed_layout_; }
+
+  // Reserve space within the fixed layout for the section.  Used for
+  // incremental update links.
+  void
+  reserve(uint64_t sh_offset, uint64_t sh_size);
+
  protected:
   // Return the output section--i.e., the object itself.
   Output_section*
@@ -3340,6 +3387,13 @@ class Output_section : public Output_data
     this->out_shndx_ = shndx;
   }
 
+  // Update the data size of the Output_section.  For a typical
+  // Output_section, there is nothing to do, but if there are any
+  // Output_section_data objects we need to do a trial layout
+  // here.
+  virtual void
+  update_data_size();
+
   // Set the final data size of the Output_section.  For a typical
   // Output_section, there is nothing to do, but if there are any
   // Output_section_data objects we need to set their final addresses
@@ -3758,6 +3812,8 @@ class Output_section : public Output_data
   bool is_noload_ : 1;
   // Whether this always keeps input section.
   bool always_keeps_input_sections_ : 1;
+  // Whether this section has a fixed layout, for incremental update links.
+  bool has_fixed_layout_ : 1;
   // For SHT_TLS sections, the offset of this section relative to the base
   // of the TLS segment.
   uint64_t tls_offset_;
@@ -3765,6 +3821,9 @@ class Output_section : public Output_data
   Checkpoint_output_section* checkpoint_;
   // Fast lookup maps for merged and relaxed input sections.
   Output_section_lookup_maps* lookup_maps_;
+  // List of available regions within the section, for incremental
+  // update links.
+  Free_list free_list_;
 };
 
 // An output segment.  PT_LOAD segments are built from collections of
@@ -3907,7 +3966,7 @@ class Output_segment
   // address of the immediately following segment.  Update *POFF and
   // *PSHNDX.  This should only be called for a PT_LOAD segment.
   uint64_t
-  set_section_addresses(const Layout*, bool reset, uint64_t addr,
+  set_section_addresses(Layout*, bool reset, uint64_t addr,
 			unsigned int increase_relro, off_t* poff,
 			unsigned int* pshndx);
 
@@ -3967,7 +4026,7 @@ class Output_segment
 
   // Set the section addresses in an Output_data_list.
   uint64_t
-  set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
+  set_section_list_addresses(Layout*, bool reset, Output_data_list*,
                              uint64_t addr, off_t* poff, unsigned int* pshndx,
                              bool* in_tls);
 
diff --git a/gold/plugin.cc b/gold/plugin.cc
index 8c22494..aef544e 100644
--- a/gold/plugin.cc
+++ b/gold/plugin.cc
@@ -752,6 +752,16 @@ Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
     }
 }
 
+// Iterate over local symbols, calling a visitor class V for each GOT offset
+// associated with a local symbol.
+template<int size, bool big_endian>
+void
+Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
+    Got_offset_list::Visitor&) const
+{
+  gold_unreachable();
+}
+
 // Get the size of a section.  Not used for plugin objects.
 
 template<int size, bool big_endian>
diff --git a/gold/plugin.h b/gold/plugin.h
index 6fb322f..571ad6f 100644
--- a/gold/plugin.h
+++ b/gold/plugin.h
@@ -384,6 +384,11 @@ class Sized_pluginobj : public Pluginobj
   do_for_all_global_symbols(Read_symbols_data* sd,
 			    Library_base::Symbol_visitor_base& v);
 
+  // Iterate over local symbols, calling a visitor class V for each GOT offset
+  // associated with a local symbol.
+  void
+  do_for_all_local_got_entries(Got_offset_list::Visitor& v) const;
+
   // Get the size of a section.
   uint64_t
   do_section_size(unsigned int shndx);
diff --git a/gold/readsyms.cc b/gold/readsyms.cc
index b6b0c26..903091f 100644
--- a/gold/readsyms.cc
+++ b/gold/readsyms.cc
@@ -397,6 +397,8 @@ Read_symbols::do_read_symbols(Workqueue* workqueue)
         {
           this->member_->sd_ = sd;
           this->member_->obj_ = obj;
+          this->member_->arg_serial_ =
+              this->input_argument_->file().arg_serial();
           return true;
         }
 
@@ -590,8 +592,12 @@ Add_symbols::run(Workqueue*)
       Incremental_inputs* incremental_inputs =
           this->layout_->incremental_inputs();
       if (incremental_inputs != NULL)
-	incremental_inputs->report_object(this->object_, NULL,
-					  this->input_argument_->script_info());
+	{
+	  unsigned int arg_serial = this->input_argument_->file().arg_serial();
+	  Script_info* script_info = this->input_argument_->script_info();
+	  incremental_inputs->report_object(this->object_, arg_serial, NULL,
+					    script_info);
+	}
       this->object_->layout(this->symtab_, this->layout_, this->sd_);
       this->object_->add_symbols(this->symtab_, this->sd_, this->layout_);
       delete this->sd_;
@@ -600,6 +606,181 @@ Add_symbols::run(Workqueue*)
     }
 }
 
+// Class Read_member.
+
+Read_member::~Read_member()
+{
+  if (this->this_blocker_ != NULL)
+    delete this->this_blocker_;
+  // next_blocker_ is deleted by the task associated with the next
+  // input file.
+}
+
+// Return whether a Read_member task is runnable.
+
+Task_token*
+Read_member::is_runnable()
+{
+  return NULL;
+}
+
+void
+Read_member::locks(Task_locker* tl)
+{
+  tl->add(this, this->next_blocker_);
+}
+
+// Run a Read_member task.
+
+void
+Read_member::run(Workqueue*)
+{
+  // This task doesn't need to do anything for now.  The Read_symbols task
+  // that is queued for the archive library will cause the archive to be
+  // processed from scratch.
+}
+
+// Class Add_base_symbols.
+
+Add_base_symbols::~Add_base_symbols()
+{
+  if (this->this_blocker_ != NULL)
+    delete this->this_blocker_;
+  // next_blocker_ is deleted by the task associated with the next
+  // input file.
+}
+
+// Return whether a Add_base_symbols task is runnable.
+
+Task_token*
+Add_base_symbols::is_runnable()
+{
+  if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
+    return this->this_blocker_;
+  return NULL;
+}
+
+void
+Add_base_symbols::locks(Task_locker* tl)
+{
+  tl->add(this, this->next_blocker_);
+}
+
+// Run an Add_base_symbols task.
+
+void
+Add_base_symbols::run(Workqueue*)
+{
+  if (!this->input_objects_->add_object(this->object_))
+    gold_unreachable();
+
+  Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
+  gold_assert(incremental_inputs != NULL);
+  unsigned int arg_serial = this->input_reader_->arg_serial();
+  Script_info* script_info =
+      this->ibase_->get_script_info(this->input_file_index_);
+  if (this->library_ != NULL && !this->library_->is_reported())
+    {
+      unsigned int lib_serial = this->library_->arg_serial();
+      unsigned int lib_index = this->library_->input_file_index();
+      Script_info* script_info = this->ibase_->get_script_info(lib_index);
+      incremental_inputs->report_archive_begin(this->library_, lib_serial,
+					       script_info);
+    }
+  incremental_inputs->report_object(this->object_, arg_serial, this->library_,
+				    script_info);
+
+  this->object_->layout(this->symtab_, this->layout_, NULL);
+  this->object_->add_symbols(this->symtab_, NULL, this->layout_);
+}
+
+// Class Check_script.
+
+Check_script::~Check_script()
+{
+  if (this->this_blocker_ != NULL)
+    delete this->this_blocker_;
+  // next_blocker_ is deleted by the task associated with the next
+  // input file.
+}
+
+// Return whether a Check_script task is runnable.
+
+Task_token*
+Check_script::is_runnable()
+{
+  if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
+    return this->this_blocker_;
+  return NULL;
+}
+
+void
+Check_script::locks(Task_locker* tl)
+{
+  tl->add(this, this->next_blocker_);
+}
+
+// Run a Check_script task.
+
+void
+Check_script::run(Workqueue*)
+{
+  Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
+  gold_assert(incremental_inputs != NULL);
+  unsigned int arg_serial = this->input_reader_->arg_serial();
+  Script_info* script_info =
+      this->ibase_->get_script_info(this->input_file_index_);
+  Timespec mtime = this->input_reader_->get_mtime();
+  incremental_inputs->report_script(script_info, arg_serial, mtime);
+}
+
+// Class Check_library.
+
+Check_library::~Check_library()
+{
+  if (this->this_blocker_ != NULL)
+    delete this->this_blocker_;
+  // next_blocker_ is deleted by the task associated with the next
+  // input file.
+}
+
+// Return whether a Check_library task is runnable.
+
+Task_token*
+Check_library::is_runnable()
+{
+  if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
+    return this->this_blocker_;
+  return NULL;
+}
+
+void
+Check_library::locks(Task_locker* tl)
+{
+  tl->add(this, this->next_blocker_);
+}
+
+// Run a Check_library task.
+
+void
+Check_library::run(Workqueue*)
+{
+  Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
+  gold_assert(incremental_inputs != NULL);
+  Incremental_library* lib = this->ibase_->get_library(this->input_file_index_);
+  gold_assert(lib != NULL);
+  lib->copy_unused_symbols();
+  // FIXME: Check that unused symbols remain unused.
+  if (!lib->is_reported())
+    {
+      unsigned int lib_serial = lib->arg_serial();
+      unsigned int lib_index = lib->input_file_index();
+      Script_info* script_info = this->ibase_->get_script_info(lib_index);
+      incremental_inputs->report_archive_begin(lib, lib_serial, script_info);
+    }
+  incremental_inputs->report_archive_end(lib);
+}
+
 // Class Start_group.
 
 Start_group::~Start_group()
diff --git a/gold/readsyms.h b/gold/readsyms.h
index bc4f38f..7dae248 100644
--- a/gold/readsyms.h
+++ b/gold/readsyms.h
@@ -27,6 +27,7 @@
 
 #include "workqueue.h"
 #include "object.h"
+#include "incremental.h"
 
 namespace gold
 {
@@ -179,6 +180,205 @@ private:
   Task_token* next_blocker_;
 };
 
+// This Task is responsible for reading the symbols from an archive
+// member that has changed since the last incremental link.
+
+class Read_member : public Task
+{
+ public:
+  // INPUT is the file to read.  INPUT_GROUP is not NULL if we are in
+  // the middle of an input group.  THIS_BLOCKER is used to prevent
+  // the associated Add_symbols task from running before the previous
+  // one has completed; it will be NULL for the first task.
+  // NEXT_BLOCKER is used to block the next input file from adding
+  // symbols.
+  Read_member(Input_objects* input_objects, Symbol_table* symtab,
+	      Layout* layout, Mapfile* mapfile,
+	      const Incremental_binary::Input_reader* input_reader,
+              Task_token* this_blocker, Task_token* next_blocker)
+    : input_objects_(input_objects), symtab_(symtab), layout_(layout),
+      mapfile_(mapfile), input_reader_(input_reader),
+      this_blocker_(this_blocker), next_blocker_(next_blocker)
+  { }
+
+  ~Read_member();
+
+  // The standard Task methods.
+
+  Task_token*
+  is_runnable();
+
+  void
+  locks(Task_locker*);
+
+  void
+  run(Workqueue*);
+
+  std::string
+  get_name() const
+  {
+    return (std::string("Read_member ") + this->input_reader_->filename());
+  }
+
+ private:
+  Input_objects* input_objects_;
+  Symbol_table* symtab_;
+  Layout* layout_;
+  Mapfile* mapfile_;
+  const Incremental_binary::Input_reader* input_reader_;
+  Task_token* this_blocker_;
+  Task_token* next_blocker_;
+};
+
+// This Task is responsible for reading the symbols from an unchanged
+// input file as part of an incremental link.  Rather than reading the
+// input file itself, we read the stored information in the base file.
+
+class Add_base_symbols : public Task
+{
+ public:
+  // INPUT is the file to read.  INPUT_GROUP is not NULL if we are in
+  // the middle of an input group.  THIS_BLOCKER is used to prevent
+  // the associated Add_symbols task from running before the previous
+  // one has completed; it will be NULL for the first task.
+  // NEXT_BLOCKER is used to block the next input file from adding
+  // symbols.
+  Add_base_symbols(Input_objects* input_objects, Object* object,
+		   Symbol_table* symtab, Layout* layout,
+		   Incremental_binary* ibase, unsigned int input_file_index,
+		   Incremental_library* lib, Mapfile* mapfile,
+		   const Incremental_binary::Input_reader* input_reader,
+		   Task_token* this_blocker, Task_token* next_blocker)
+    : input_objects_(input_objects), object_(object), symtab_(symtab),
+      layout_(layout), ibase_(ibase), input_file_index_(input_file_index),
+      library_(lib), mapfile_(mapfile), input_reader_(input_reader),
+      this_blocker_(this_blocker), next_blocker_(next_blocker)
+  { }
+
+  ~Add_base_symbols();
+
+  // The standard Task methods.
+
+  Task_token*
+  is_runnable();
+
+  void
+  locks(Task_locker*);
+
+  void
+  run(Workqueue*);
+
+  std::string
+  get_name() const
+  {
+    return (std::string("Add_base_symbols ") + this->input_reader_->filename());
+  }
+
+ private:
+  Input_objects* input_objects_;
+  Object* object_;
+  Symbol_table* symtab_;
+  Layout* layout_;
+  Incremental_binary* ibase_;
+  unsigned int input_file_index_;
+  Incremental_library* library_;
+  Mapfile* mapfile_;
+  const Incremental_binary::Input_reader* input_reader_;
+  Task_token* this_blocker_;
+  Task_token* next_blocker_;
+};
+
+// This Task is responsible for processing an input script file that has
+// not changed since the last incremental link.
+
+class Check_script : public Task
+{
+ public:
+  Check_script(Layout* layout, Incremental_binary* ibase,
+	       unsigned int input_file_index,
+	       const Incremental_binary::Input_reader* input_reader,
+	       Task_token* this_blocker, Task_token* next_blocker)
+    : layout_(layout), ibase_(ibase), input_file_index_(input_file_index),
+      input_reader_(input_reader), this_blocker_(this_blocker),
+      next_blocker_(next_blocker)
+  {
+    this->filename_ = std::string(this->input_reader_->filename());
+  }
+
+  ~Check_script();
+
+  // The standard Task methods.
+
+  Task_token*
+  is_runnable();
+
+  void
+  locks(Task_locker*);
+
+  void
+  run(Workqueue*);
+
+  std::string
+  get_name() const
+  {
+    return (std::string("Check_script ") + this->input_reader_->filename());
+  }
+
+ private:
+  std::string filename_;
+  Layout* layout_;
+  Incremental_binary* ibase_;
+  unsigned int input_file_index_;
+  const Incremental_binary::Input_reader* input_reader_;
+  Task_token* this_blocker_;
+  Task_token* next_blocker_;
+};
+
+// This Task is responsible for processing an archive library that has
+// not changed since the last incremental link.
+
+class Check_library : public Task
+{
+ public:
+  Check_library(Symbol_table* symtab, Layout* layout,
+		Incremental_binary* ibase,
+		unsigned int input_file_index,
+		const Incremental_binary::Input_reader* input_reader,
+		Task_token* this_blocker, Task_token* next_blocker)
+    : layout_(layout), symtab_(symtab), ibase_(ibase),
+      input_file_index_(input_file_index), input_reader_(input_reader),
+      this_blocker_(this_blocker), next_blocker_(next_blocker)
+  { }
+
+  ~Check_library();
+
+  // The standard Task methods.
+
+  Task_token*
+  is_runnable();
+
+  void
+  locks(Task_locker*);
+
+  void
+  run(Workqueue*);
+
+  std::string
+  get_name() const
+  {
+    return (std::string("Check_library ") + this->input_reader_->filename());
+  }
+
+ private:
+  Layout* layout_;
+  Symbol_table* symtab_;
+  Incremental_binary* ibase_;
+  unsigned int input_file_index_;
+  const Incremental_binary::Input_reader* input_reader_;
+  Task_token* this_blocker_;
+  Task_token* next_blocker_;
+};
+
 // This class is used to track the archives in a group.
 
 class Input_group
diff --git a/gold/reloc.cc b/gold/reloc.cc
index 9ffb693..9644110 100644
--- a/gold/reloc.cc
+++ b/gold/reloc.cc
@@ -56,7 +56,9 @@ Read_relocs::is_runnable()
 void
 Read_relocs::locks(Task_locker* tl)
 {
-  tl->add(this, this->object_->token());
+  Task_token* token = this->object_->token();
+  if (token != NULL)
+    tl->add(this, token);
 }
 
 // Read the relocations and then start a Scan_relocs_task.
@@ -172,7 +174,9 @@ Scan_relocs::is_runnable()
 void
 Scan_relocs::locks(Task_locker* tl)
 {
-  tl->add(this, this->object_->token());
+  Task_token* token = this->object_->token();
+  if (token != NULL)
+    tl->add(this, token);
   tl->add(this, this->next_blocker_);
 }
 
@@ -222,7 +226,9 @@ Relocate_task::locks(Task_locker* tl)
   if (this->input_sections_blocker_ != NULL)
     tl->add(this, this->input_sections_blocker_);
   tl->add(this, this->final_blocker_);
-  tl->add(this, this->object_->token());
+  Task_token* token = this->object_->token();
+  if (token != NULL)
+    tl->add(this, token);
 }
 
 // Run the task.
@@ -684,7 +690,8 @@ Sized_relobj<size, big_endian>::do_relocate(const Symbol_table* symtab,
 
   // Write out the local symbols.
   this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
-			    layout->symtab_xindex(), layout->dynsym_xindex());
+			    layout->symtab_xindex(), layout->dynsym_xindex(),
+			    layout->symtab_section()->offset());
 
   // We should no longer need the local symbol values.
   this->clear_local_symbols();
diff --git a/gold/script.cc b/gold/script.cc
index dbf0b16..da6af5b 100644
--- a/gold/script.cc
+++ b/gold/script.cc
@@ -1422,8 +1422,10 @@ read_input_script(Workqueue* workqueue, Symbol_table* symtab, Layout* layout,
     {
       const std::string& filename = input_file->filename();
       Timespec mtime = input_file->file().get_mtime();
-      script_info = new Script_info();
-      layout->incremental_inputs()->report_script(filename, script_info, mtime);
+      unsigned int arg_serial = input_argument->file().arg_serial();
+      script_info = new Script_info(filename);
+      layout->incremental_inputs()->report_script(script_info, arg_serial,
+						  mtime);
     }
 
   Parser_closure closure(input_file->filename().c_str(),
diff --git a/gold/script.h b/gold/script.h
index 2d9355a..5ef723e 100644
--- a/gold/script.h
+++ b/gold/script.h
@@ -540,31 +540,6 @@ class Script_options
   Script_sections script_sections_;
 };
 
-// Information about a script input that will persist during the whole linker
-// run. Needed only during an incremental build to retrieve the input files
-// added by this script.
-
-class Script_info
-{
- public:
-  Script_info()
-    : incremental_script_entry_(NULL)
-  { }
-
-  // Store a pointer to the incremental information for this script.
-  void
-  set_incremental_info(Incremental_script_entry* entry)
-  { this->incremental_script_entry_ = entry; }
-
-  // Return the pointer to the incremental information for this script.
-  Incremental_script_entry*
-  incremental_info() const
-  { return this->incremental_script_entry_; }
-
- private:
-  Incremental_script_entry* incremental_script_entry_;
-};
-
 // FILE was found as an argument on the command line, but was not
 // recognized as an ELF file.  Try to read it as a script.  Return
 // true if the file was handled.  This has to handle /usr/lib/libc.so
diff --git a/gold/symtab.cc b/gold/symtab.cc
index d8cda2d..7af2013 100644
--- a/gold/symtab.cc
+++ b/gold/symtab.cc
@@ -40,6 +40,7 @@
 #include "symtab.h"
 #include "script.h"
 #include "plugin.h"
+#include "incremental.h"
 
 namespace gold
 {
@@ -1484,6 +1485,37 @@ Symbol_table::add_from_dynobj(
   this->record_weak_aliases(&object_symbols);
 }
 
+// Add a symbol from a incremental object file.
+
+template<int size, bool big_endian>
+Symbol*
+Symbol_table::add_from_incrobj(
+    Object* obj,
+    const char* name,
+    const char* ver,
+    elfcpp::Sym<size, big_endian>* sym)
+{
+  unsigned int st_shndx = sym->get_st_shndx();
+  bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
+
+  Stringpool::Key ver_key = 0;
+  bool is_default_version = false;
+  bool is_forced_local = false;
+
+  Stringpool::Key name_key;
+  name = this->namepool_.add(name, true, &name_key);
+
+  Sized_symbol<size>* res;
+  res = this->add_from_object(obj, name, name_key, ver, ver_key,
+		              is_default_version, *sym, st_shndx,
+			      is_ordinary, st_shndx);
+
+  if (is_forced_local)
+    this->force_local(res);
+
+  return res;
+}
+
 // This is used to sort weak aliases.  We sort them first by section
 // index, then by offset, then by weak ahead of strong.
 
@@ -3323,6 +3355,46 @@ Symbol_table::add_from_dynobj<64, true>(
     size_t* defined);
 #endif
 
+#ifdef HAVE_TARGET_32_LITTLE
+template
+Symbol*
+Symbol_table::add_from_incrobj(
+    Object* obj,
+    const char* name,
+    const char* ver,
+    elfcpp::Sym<32, false>* sym);
+#endif
+
+#ifdef HAVE_TARGET_32_BIG
+template
+Symbol*
+Symbol_table::add_from_incrobj(
+    Object* obj,
+    const char* name,
+    const char* ver,
+    elfcpp::Sym<32, true>* sym);
+#endif
+
+#ifdef HAVE_TARGET_64_LITTLE
+template
+Symbol*
+Symbol_table::add_from_incrobj(
+    Object* obj,
+    const char* name,
+    const char* ver,
+    elfcpp::Sym<64, false>* sym);
+#endif
+
+#ifdef HAVE_TARGET_64_BIG
+template
+Symbol*
+Symbol_table::add_from_incrobj(
+    Object* obj,
+    const char* name,
+    const char* ver,
+    elfcpp::Sym<64, true>* sym);
+#endif
+
 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
 template
 void
diff --git a/gold/symtab.h b/gold/symtab.h
index e355b2a..c9ef1c8 100644
--- a/gold/symtab.h
+++ b/gold/symtab.h
@@ -48,6 +48,8 @@ class Sized_pluginobj;
 class Dynobj;
 template<int size, bool big_endian>
 class Sized_dynobj;
+template<int size, bool big_endian>
+class Sized_incrobj;
 class Versions;
 class Version_script_info;
 class Input_objects;
@@ -1340,6 +1342,13 @@ class Symbol_table
 		  typename Sized_relobj<size, big_endian>::Symbols*,
 		  size_t* defined);
 
+  // Add one external symbol from the incremental object OBJ to the symbol
+  // table.  Returns a pointer to the resolved symbol in the symbol table.
+  template<int size, bool big_endian>
+  Symbol*
+  add_from_incrobj(Object* obj, const char* name,
+		   const char* ver, elfcpp::Sym<size, big_endian>* sym);
+
   // Define a special symbol based on an Output_data.  It is a
   // multiple definition error if this symbol is already defined.
   Symbol*
@@ -1470,6 +1479,11 @@ class Symbol_table
   finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
 	   Stringpool* pool, unsigned int* plocal_symcount);
 
+  // Set the final file offset of the symbol table.
+  void
+  set_file_offset(off_t off)
+  { this->offset_ = off; }
+
   // Status code of Symbol_table::compute_final_value.
   enum Compute_final_value_status
   {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]