// inremental.cc -- incremental linking support for gold
-// Copyright 2009, 2010 Free Software Foundation, Inc.
+// Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
// Written by Mikolaj Zalewski <mikolajz@google.com>.
// This file is part of gold.
#include "gold.h"
+#include <set>
#include <cstdarg>
#include "libiberty.h"
va_end(args);
}
+// Return TRUE if a section of type SH_TYPE can be updated in place
+// during an incremental update. We can update sections of type PROGBITS,
+// NOBITS, INIT_ARRAY, FINI_ARRAY, PREINIT_ARRAY, and NOTE. All others
+// will be regenerated.
+
+bool
+can_incremental_update(unsigned int sh_type)
+{
+ return (sh_type == elfcpp::SHT_PROGBITS
+ || sh_type == elfcpp::SHT_NOBITS
+ || sh_type == elfcpp::SHT_INIT_ARRAY
+ || sh_type == elfcpp::SHT_FINI_ARRAY
+ || sh_type == elfcpp::SHT_PREINIT_ARRAY
+ || sh_type == elfcpp::SHT_NOTE);
+}
+
// Find the .gnu_incremental_inputs section and related sections.
template<int size, bool big_endian>
break;
case INCREMENTAL_INPUT_SCRIPT:
{
- Script_info* script = new Script_info(input_file.filename());
+ Script_info* script = new Script_info(input_file.filename(), i);
this->script_map_[i] = script;
unsigned int object_count = input_file.get_object_count();
for (unsigned int j = 0; j < object_count; j++)
if (incremental_inputs->command_line() != inputs.command_line())
{
+ gold_debug(DEBUG_INCREMENTAL,
+ "old command line: %s",
+ inputs.command_line());
+ gold_debug(DEBUG_INCREMENTAL,
+ "new command line: %s",
+ incremental_inputs->command_line().c_str());
explain_no_incremental(_("command line changed"));
return false;
}
{
Input_entry_reader input_file = this->inputs_reader_.input_file(n);
Incremental_disposition disp = INCREMENTAL_CHECK;
+
+ // For files named in scripts, find the file that was actually named
+ // on the command line, so that we can get the incremental disposition
+ // flag.
+ Script_info* script = this->get_script_info(n);
+ if (script != NULL)
+ n = script->input_file_index();
+
const Input_argument* input_argument = this->get_input_argument(n);
if (input_argument != NULL)
disp = input_argument->file().options().incremental_disposition();
+ // For files at the beginning of the command line (i.e., those added
+ // implicitly by gcc), check whether the --incremental-startup-unchanged
+ // option was used.
+ if (disp == INCREMENTAL_STARTUP)
+ disp = parameters->options().incremental_startup_disposition();
+
if (disp != INCREMENTAL_CHECK)
return disp == INCREMENTAL_CHANGED;
Sized_incremental_binary<size, big_endian>::do_reserve_layout(
unsigned int input_file_index)
{
+ const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+
Input_entry_reader input_file =
this->inputs_reader_.input_file(input_file_index);
if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
- return;
+ {
+ // Reserve the BSS space used for COPY relocations.
+ unsigned int nsyms = input_file.get_global_symbol_count();
+ Incremental_binary::View symtab_view(NULL);
+ unsigned int symtab_count;
+ elfcpp::Elf_strtab strtab(NULL, 0);
+ this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
+ for (unsigned int i = 0; i < nsyms; ++i)
+ {
+ bool is_def;
+ bool is_copy;
+ unsigned int output_symndx =
+ input_file.get_output_symbol_index(i, &is_def, &is_copy);
+ if (is_copy)
+ {
+ const unsigned char* sym_p = (symtab_view.data()
+ + output_symndx * sym_size);
+ elfcpp::Sym<size, big_endian> gsym(sym_p);
+ unsigned int shndx = gsym.get_st_shndx();
+ if (shndx < 1 || shndx >= this->section_map_.size())
+ continue;
+ Output_section* os = this->section_map_[shndx];
+ off_t offset = gsym.get_st_value() - os->address();
+ os->reserve(offset, gsym.get_st_size());
+ gold_debug(DEBUG_INCREMENTAL,
+ "Reserve for COPY reloc: %s, off %d, size %d",
+ os->name(),
+ static_cast<int>(offset),
+ static_cast<int>(gsym.get_st_size()));
+ }
+ }
+ return;
+ }
unsigned int shnum = input_file.get_input_section_count();
for (unsigned int i = 0; i < shnum; i++)
// FIXME: This should really be a fatal error (corrupt input).
gold_assert(symndx >= first_global && symndx < symtab_count);
Symbol* sym = this->global_symbol(symndx - first_global);
- gold_debug(DEBUG_INCREMENTAL,
- "GOT entry %d, type %02x: %s",
- i, got_type, sym->name());
- target->reserve_global_got_entry(i, sym, got_type);
+ // Add the GOT entry only if the symbol is still referenced.
+ if (sym != NULL && sym->in_reg())
+ {
+ gold_debug(DEBUG_INCREMENTAL,
+ "GOT entry %d, type %02x: %s",
+ i, got_type, sym->name());
+ target->reserve_global_got_entry(i, sym, got_type);
+ }
}
}
// FIXME: This should really be a fatal error (corrupt input).
gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
Symbol* sym = this->global_symbol(plt_desc - first_global);
- gold_debug(DEBUG_INCREMENTAL,
- "PLT entry %d: %s",
- i, sym->name());
- target->register_global_plt_entry(i, sym);
+ // Add the PLT entry only if the symbol is still referenced.
+ if (sym != NULL && sym->in_reg())
+ {
+ gold_debug(DEBUG_INCREMENTAL,
+ "PLT entry %d: %s",
+ i, sym->name());
+ target->register_global_plt_entry(symtab, layout, i, sym);
+ }
+ }
+}
+
+// Emit COPY relocations from the existing output file.
+
+template<int size, bool big_endian>
+void
+Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
+ Symbol_table* symtab)
+{
+ Sized_target<size, big_endian>* target =
+ parameters->sized_target<size, big_endian>();
+
+ for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
+ p != this->copy_relocs_.end();
+ ++p)
+ {
+ if (!(*p).symbol->is_copied_from_dynobj())
+ target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
+ (*p).offset);
}
}
|| strcmp(argv[i], "--incremental-changed") == 0
|| strcmp(argv[i], "--incremental-unchanged") == 0
|| strcmp(argv[i], "--incremental-unknown") == 0
+ || strcmp(argv[i], "--incremental-startup-unchanged") == 0
+ || is_prefix_of("--incremental-base=", argv[i])
+ || is_prefix_of("--incremental-patch=", argv[i])
|| is_prefix_of("--debug=", argv[i]))
continue;
+ if (strcmp(argv[i], "--incremental-base") == 0
+ || strcmp(argv[i], "--incremental-patch") == 0
+ || strcmp(argv[i], "--debug") == 0)
+ {
+ // When these options are used without the '=', skip the
+ // following parameter as well.
+ ++i;
+ continue;
+ }
args.append(" '");
// Now append argv[i], but with all single-quotes escaped
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, arg_serial, mtime);
- if (obj->is_in_system_directory())
- obj_entry->set_is_in_system_directory();
- this->inputs_.push_back(obj_entry);
- if (arch != NULL)
+ Incremental_input_entry* input_entry;
+
+ this->current_object_ = obj;
+
+ if (!obj->is_dynamic())
+ {
+ this->current_object_entry_ =
+ new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
+ input_entry = this->current_object_entry_;
+ if (arch != NULL)
+ {
+ Incremental_archive_entry* arch_entry = arch->incremental_info();
+ gold_assert(arch_entry != NULL);
+ arch_entry->add_object(this->current_object_entry_);
+ }
+ }
+ else
{
- Incremental_archive_entry* arch_entry = arch->incremental_info();
- gold_assert(arch_entry != NULL);
- arch_entry->add_object(obj_entry);
+ this->current_object_entry_ = NULL;
+ Stringpool::Key soname_key;
+ Dynobj* dynobj = obj->dynobj();
+ gold_assert(dynobj != NULL);
+ this->strtab_->add(dynobj->soname(), false, &soname_key);
+ input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
+ arg_serial, mtime);
}
+ if (obj->is_in_system_directory())
+ input_entry->set_is_in_system_directory();
+
+ if (obj->as_needed())
+ input_entry->set_as_needed();
+
+ this->inputs_.push_back(input_entry);
+
if (script_info != NULL)
{
Incremental_script_entry* script_entry = script_info->incremental_info();
gold_assert(script_entry != NULL);
- script_entry->add_object(obj_entry);
+ script_entry->add_object(input_entry);
}
-
- this->current_object_ = obj;
- this->current_object_entry_ = obj_entry;
}
-// Record the input object file OBJ. If ARCH is not NULL, attach
-// the object file to the archive. This is called by the
-// Add_symbols task after finding out the type of the file.
+// Record an input section SHNDX from object file OBJ.
void
Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
Stringpool::Key key = 0;
if (name != NULL)
- this->strtab_->add(name, true, &key);
+ this->strtab_->add(name, true, &key);
gold_assert(obj == this->current_object_);
+ gold_assert(this->current_object_entry_ != NULL);
this->current_object_entry_->add_input_section(shndx, key, sh_size);
}
+// Record a kept COMDAT group belonging to object file OBJ.
+
+void
+Incremental_inputs::report_comdat_group(Object* obj, const char* name)
+{
+ Stringpool::Key key = 0;
+
+ if (name != NULL)
+ this->strtab_->add(name, true, &key);
+ gold_assert(obj == this->current_object_);
+ gold_assert(this->current_object_entry_ != NULL);
+ this->current_object_entry_->add_comdat_group(key);
+}
+
// Record that the input argument INPUT is a script SCRIPT. This is
// called by read_script after parsing the script and reading the list
// of inputs added by this script.
gold_assert(entry != NULL);
(*p)->set_info_offset(info_offset);
// Input section count, global symbol count, local symbol offset,
- // local symbol count, first dynamic reloc, dynamic reloc count.
- info_offset += 24;
+ // local symbol count, first dynamic reloc, dynamic reloc count,
+ // comdat group count.
+ info_offset += 28;
// Each input section.
info_offset += (entry->get_input_section_count()
* (8 + 2 * sizeof_addr));
// Each global symbol.
const Object::Symbols* syms = entry->object()->get_global_symbols();
info_offset += syms->size() * 20;
+ // Each comdat group.
+ info_offset += entry->get_comdat_group_count() * 4;
}
break;
case INCREMENTAL_INPUT_SHARED_LIBRARY:
{
- Incremental_object_entry* entry = (*p)->object_entry();
+ Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
gold_assert(entry != NULL);
(*p)->set_info_offset(info_offset);
- // Global symbol count.
- info_offset += 4;
+ // Global symbol count, soname index.
+ info_offset += 8;
// Each global symbol.
const Object::Symbols* syms = entry->object()->get_global_symbols();
gold_assert(syms != NULL);
unsigned int flags = (*p)->type();
if ((*p)->is_in_system_directory())
flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
+ if ((*p)->as_needed())
+ flags |= INCREMENTAL_INPUT_AS_NEEDED;
Swap32::writeval(pov, filename_offset);
Swap32::writeval(pov + 4, (*p)->get_info_offset());
Swap64::writeval(pov + 8, mtime.seconds);
unsigned int nlocals = relobj->output_local_symbol_count();
unsigned int first_dynrel = relobj->first_dyn_reloc();
unsigned int ndynrel = relobj->dyn_reloc_count();
+ unsigned int ncomdat = entry->get_comdat_group_count();
Swap32::writeval(pov, nsections);
Swap32::writeval(pov + 4, nsyms);
Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
Swap32::writeval(pov + 12, nlocals);
Swap32::writeval(pov + 16, first_dynrel);
Swap32::writeval(pov + 20, ndynrel);
- pov += 24;
+ Swap32::writeval(pov + 24, ncomdat);
+ pov += 28;
// Build a temporary array to map input section indexes
// from the original object file index to the index in the
if (sym->is_forwarder())
sym = this->symtab_->resolve_forwards(sym);
unsigned int shndx = 0;
- if (sym->source() == Symbol::FROM_OBJECT
- && sym->object() == obj
- && sym->is_defined())
+ if (sym->source() != Symbol::FROM_OBJECT)
+ {
+ // The symbol was defined by the linker (e.g., common).
+ // We mark these symbols with a special SHNDX of -1,
+ // but exclude linker-predefined symbols and symbols
+ // copied from shared objects.
+ if (!sym->is_predefined()
+ && !sym->is_copied_from_dynobj())
+ shndx = -1U;
+ }
+ else if (sym->object() == obj && sym->is_defined())
{
bool is_ordinary;
unsigned int orig_shndx = sym->shndx(&is_ordinary);
if (is_ordinary)
shndx = index_map[orig_shndx];
+ else
+ shndx = 1;
}
unsigned int symtab_index = sym->symtab_index();
unsigned int chain = 0;
pov += 20;
}
+ // For each kept COMDAT group, write the group signature.
+ for (unsigned int i = 0; i < ncomdat; i++)
+ {
+ Stringpool::Key key = entry->get_comdat_signature_key(i);
+ off_t name_offset = 0;
+ if (key != 0)
+ name_offset = strtab->get_offset_from_key(key);
+ Swap32::writeval(pov, name_offset);
+ pov += 4;
+ }
+
delete[] index_map;
}
break;
{
gold_assert(static_cast<unsigned int>(pov - oview)
== (*p)->get_info_offset());
- Incremental_object_entry* entry = (*p)->object_entry();
+ Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
gold_assert(entry != NULL);
- const Object* obj = entry->object();
+ Object* obj = entry->object();
+ Dynobj* dynobj = obj->dynobj();
+ gold_assert(dynobj != NULL);
const Object::Symbols* syms = obj->get_global_symbols();
+ // Write the soname string table index.
+ section_offset_type soname_offset =
+ strtab->get_offset_from_key(entry->get_soname_key());
+ Swap32::writeval(pov, soname_offset);
+ pov += 4;
+
// Skip the global symbol count for now.
unsigned char* orig_pov = pov;
pov += 4;
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);
+ unsigned int flags = 0;
+ // If the symbol has hidden or internal visibility, we
+ // mark it as defined in the shared object so we don't
+ // try to resolve it during an incremental update.
+ if (sym->visibility() == elfcpp::STV_HIDDEN
+ || sym->visibility() == elfcpp::STV_INTERNAL)
+ flags = INCREMENTAL_SHLIB_SYM_DEF;
+ else if (sym->source() == Symbol::FROM_OBJECT
+ && sym->object() == obj
+ && sym->is_defined())
+ flags = INCREMENTAL_SHLIB_SYM_DEF;
+ else if (sym->is_copied_from_dynobj()
+ && this->symtab_->get_copy_source(sym) == dynobj)
+ flags = INCREMENTAL_SHLIB_SYM_COPY;
+ flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
+ Swap32::writeval(pov, sym->symtab_index() | flags);
pov += 4;
++nsyms_out;
}
input_reader_(ibase->inputs_reader().input_file(input_file_index)),
local_symbol_count_(0), output_local_dynsym_count_(0),
local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
- symbols_(), incr_reloc_offset_(-1U), incr_reloc_count_(0),
- incr_reloc_output_index_(0), incr_relocs_(NULL), local_symbols_()
+ symbols_(), defined_count_(0), incr_reloc_offset_(-1U),
+ incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
+ local_symbols_()
{
if (this->input_reader_.is_in_system_directory())
this->set_is_in_system_directory();
out_sections[i] = os;
this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
}
+
+ // Process the COMDAT groups.
+ unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
+ for (unsigned int i = 0; i < ncomdat; i++)
+ {
+ const char* signature = this->input_reader_.get_comdat_group_signature(i);
+ if (signature == NULL || signature[0] == '\0')
+ this->error(_("COMDAT group has no signature"));
+ bool keep = layout->find_or_add_kept_section(signature, this, i, true,
+ true, NULL);
+ if (keep)
+ incremental_inputs->report_comdat_group(this, signature);
+ else
+ this->error(_("COMDAT group %s included twice in incremental link"),
+ signature);
+ }
}
// Layout sections whose layout was deferred while waiting for
st_bind = elfcpp::STB_GLOBAL;
unsigned int input_shndx = info.shndx();
- if (input_shndx == 0)
+ if (input_shndx == 0 || input_shndx == -1U)
{
shndx = elfcpp::SHN_UNDEF;
v = 0;
osym.put_st_other(gsym.get_st_other());
osym.put_st_shndx(shndx);
- this->symbols_[i] =
- symtab->add_from_incrobj(this, name, NULL, &sym);
- this->ibase_->add_global_symbol(output_symndx - first_global,
- this->symbols_[i]);
+ Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
+
+ if (shndx != elfcpp::SHN_UNDEF)
+ ++this->defined_count_;
+
+ // If this is a linker-defined symbol that hasn't yet been defined,
+ // define it now.
+ if (input_shndx == -1U && !res->is_defined())
+ {
+ shndx = gsym.get_st_shndx();
+ v = gsym.get_st_value();
+ Elf_size_type symsize = gsym.get_st_size();
+ if (shndx == elfcpp::SHN_ABS)
+ {
+ symtab->define_as_constant(name, NULL,
+ Symbol_table::INCREMENTAL_BASE,
+ v, symsize, st_type, st_bind,
+ gsym.get_st_visibility(), 0,
+ false, false);
+ }
+ else
+ {
+ Output_section* os = this->ibase_->output_section(shndx);
+ gold_assert(os != NULL && os->has_fixed_layout());
+ v -= os->address();
+ if (symsize > 0)
+ os->reserve(v, symsize);
+ symtab->define_in_output_data(name, NULL,
+ Symbol_table::INCREMENTAL_BASE,
+ os, v, symsize, st_type, st_bind,
+ gsym.get_st_visibility(), 0,
+ false, false);
+ }
+ }
+
+ this->symbols_[i] = res;
+ this->ibase_->add_global_symbol(output_symndx - first_global, res);
}
}
template<int size, bool big_endian>
void
Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
- const Symbol_table*, size_t*, size_t*) const
-{
- gold_unreachable();
+ const Symbol_table*,
+ size_t* defined,
+ size_t* used) const
+{
+ *defined = this->defined_count_;
+ size_t count = 0;
+ for (typename Symbols::const_iterator p = this->symbols_.begin();
+ p != this->symbols_.end();
+ ++p)
+ if (*p != NULL
+ && (*p)->source() == Symbol::FROM_OBJECT
+ && (*p)->object() == this
+ && (*p)->is_defined())
+ ++count;
+ *used = count;
}
// Read the relocs.
: Dynobj(name, NULL), ibase_(ibase),
input_file_index_(input_file_index),
input_reader_(ibase->inputs_reader().input_file(input_file_index)),
- symbols_()
+ symbols_(), defined_count_(0)
{
if (this->input_reader_.is_in_system_directory())
this->set_is_in_system_directory();
+ if (this->input_reader_.as_needed())
+ this->set_as_needed();
+ this->set_soname_string(this->input_reader_.get_soname());
this->set_shnum(0);
}
unsigned int isym_count = isymtab.symbol_count();
unsigned int first_global = symtab_count - isym_count;
+ // We keep a set of symbols that we have generated COPY relocations
+ // for, indexed by the symbol value. We do not need more than one
+ // COPY relocation per address.
+ typedef typename std::set<Address> Copied_symbols;
+ Copied_symbols copied_symbols;
+
const unsigned char* sym_p;
for (unsigned int i = 0; i < nsyms; ++i)
{
bool is_def;
+ bool is_copy;
unsigned int output_symndx =
- this->input_reader_.get_output_symbol_index(i, &is_def);
+ this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
sym_p = symtab_view.data() + 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;
+ Address v;
unsigned int shndx;
elfcpp::STB st_bind = gsym.get_st_bind();
elfcpp::STT st_type = gsym.get_st_type();
// is meaningless, as long as it's not SHN_UNDEF.
shndx = 1;
v = gsym.get_st_value();
+ ++this->defined_count_;
}
osym.put_st_name(0);
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);
+ Sized_symbol<size>* res =
+ symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
+ this->symbols_[i] = res;
this->ibase_->add_global_symbol(output_symndx - first_global,
this->symbols_[i]);
+
+ if (is_copy)
+ {
+ std::pair<typename Copied_symbols::iterator, bool> ins =
+ copied_symbols.insert(v);
+ if (ins.second)
+ {
+ unsigned int shndx = gsym.get_st_shndx();
+ Output_section* os = this->ibase_->output_section(shndx);
+ off_t offset = v - os->address();
+ this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
+ }
+ }
}
}
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();
+ const Symbol_table*,
+ size_t* defined,
+ size_t* used) const
+{
+ *defined = this->defined_count_;
+ size_t count = 0;
+ for (typename Symbols::const_iterator p = this->symbols_.begin();
+ p != this->symbols_.end();
+ ++p)
+ if (*p != NULL
+ && (*p)->source() == Symbol::FROM_OBJECT
+ && (*p)->object() == this
+ && (*p)->is_defined()
+ && (*p)->dynsym_index() != -1U)
+ ++count;
+ *used = count;
}
// Allocate an incremental object of the appropriate size and endianness.