binutils-gdb/gold/descriptors.cc
Cary Coutant 89fc34211b Add plugin functionality for link-time optimization (LTO).
include/:
	* plugin-api.h: New file.

gold/:
	* configure.ac (plugins): Add --enable-plugins option.
	* configure: Regenerate.
	* config.in: Regenerate.
	* Makefile.am (LIBDL): New variable.
	(CCFILES): Add plugin.cc.
	(HFILES): Add plugin.h.
	(ldadd_var): Add LIBDL.
	* Makefile.in: Regenerate.

	* archive.cc: Include "plugin.h".
	(Archive::setup): Don't preread archive symbols when using a plugin.
	(Archive::get_file_and_offset): Add memsize parameter.  Change callers.
	(Archive::get_elf_object_for_member): Call plugin hooks for claiming
	files.
	(Archive::include_member): Add symbols from plugin objects.
	* archive.h (Archive::get_file_and_offset): Add memsize parameter.
	* descriptors.cc (Descriptors::open): Check for file descriptors
	abandoned by plugins.
	(Descriptors::claim_for_plugin): New function.
	* descriptors.h (Descriptors::claim_for_plugin): New function.
	(Open_descriptor::is_claimed): New field.
	(claim_descriptor_for_plugin): New function.
	* fileread.cc (File_read::claim_for_plugin): New function.
	* fileread.h (File_read::claim_for_plugin): New function.
	(File_read::descriptor): New function.
	* gold.cc: Include "plugin.h".
	(queue_initial_tasks): Add task to call plugin hooks for generating
	new object files.
	* main.cc: Include "plugin.h".
	(main): Load plugin libraries.
	* object.h (Pluginobj): Declare.
	(Object::pluginobj): New function.
	(Object::do_pluginobj): New function.
	(Object::set_target): New function.
	* options.cc: Include "plugin.h".
	(General_options::parse_plugin): New function.
	(General_options::General_options): Initialize plugins_ field.
	(General_options::add_plugin): New function.
	* options.h (Plugin_manager): Declare.
	(General_options): Add --plugin option.
	(General_options::has_plugins): New function.
	(General_options::plugins): New function.
	(General_options::add_plugin): New function.
	(General_options::plugins_): New field.
	* plugin.cc: New file.
	* plugin.h: New file.
	* readsyms.cc: Include "plugin.h".
	(Read_symbols::do_read_symbols): Check for archive before checking
	for ELF file.  Call plugin hooks to claim files.
	* resolve.cc (Symbol_table::resolve): Record when symbol is referenced
	from a real object file; force override when processing replacement
	files.
	* symtab.cc (Symbol::init_fields): Initialize in_real_elf_ field.
	(Symbol::init_base_object): Likewise.
	(Symbol::init_base_output_data): Likewise.
	(Symbol::init_base_output_segment): Likewise.
	(Symbol::init_base_constant): Likewise.
	(Symbol::init_base_undefined): Likewise.
	(Symbol::output_section): Assert that object is not a plugin.
	(Symbol_table::add_from_pluginobj): New function.
	(Symbol_table::sized_finalize_symbol): Treat symbols from plugins as
	undefined.
	(Symbol_table::sized_write_globals): Likewise.
	(Symbol_table::add_from_pluginobj): Instantiate template.
	* symtab.h (Sized_pluginobj): Declare.
	(Symbol::in_real_elf): New function.
	(Symbol::set_in_real_elf): New function.
	(Symbol::in_real_elf_): New field.
	(Symbol_table::add_from_pluginobj): New function.

	* testsuite/Makefile.am (AM_CFLAGS): New variable.
	(LIBDL): New variable.
	(LDADD): Add LIBDL.
	(check_PROGRAMS): Add plugin_test_1 and plugin_test_2.
	(check_SCRIPTS): Add plugin_test_1.sh and plugin_test_2.sh.
	(check_DATA): Add plugin_test_1.err and plugin_test_2.err.
	(MOSTLYCLEANFILES): Likewise.
	* testsuite/Makefile.in: Regenerate.
	* testsuite/plugin_test.c: New file.
	* testsuite/plugin_test_1.sh: New file.
	* testsuite/plugin_test_2.sh: New file.
2008-09-19 22:54:57 +00:00

232 lines
6.1 KiB
C++

// descriptors.cc -- manage file descriptors for gold
// Copyright 2008 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "gold.h"
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include "parameters.h"
#include "gold-threads.h"
#include "descriptors.h"
namespace gold
{
// Class Descriptors.
// The default for limit_ is meant to simply be large. It gets
// adjusted downward if we run out of file descriptors.
Descriptors::Descriptors()
: lock_(NULL), open_descriptors_(), stack_top_(-1), current_(0),
limit_(8192 - 16)
{
this->open_descriptors_.reserve(128);
}
// Open a file.
int
Descriptors::open(int descriptor, const char* name, int flags, int mode)
{
// We don't initialize this until we are called, because we can't
// initialize a Lock until we have parsed the options to find out
// whether we are running with threads. We can be called before
// options are valid when reading a linker script.
if (this->lock_ == NULL)
{
if (parameters->options_valid())
this->lock_ = new Lock();
else
gold_assert(descriptor < 0);
}
if (descriptor >= 0)
{
Hold_lock hl(*this->lock_);
gold_assert(static_cast<size_t>(descriptor)
< this->open_descriptors_.size());
Open_descriptor* pod = &this->open_descriptors_[descriptor];
if (pod->name == name
|| (pod->name != NULL && strcmp(pod->name, name) == 0))
{
gold_assert(!pod->inuse);
pod->inuse = true;
return descriptor;
}
}
while (true)
{
int new_descriptor = ::open(name, flags, mode);
if (new_descriptor < 0
&& errno != ENFILE
&& errno != EMFILE)
{
if (descriptor >= 0 && errno == ENOENT)
{
{
Hold_lock hl(*this->lock_);
gold_error(_("file %s was removed during the link"),
this->open_descriptors_[descriptor].name);
}
errno = ENOENT;
}
return new_descriptor;
}
if (new_descriptor >= 0)
{
Hold_optional_lock hl(this->lock_);
if (static_cast<size_t>(new_descriptor)
>= this->open_descriptors_.size())
this->open_descriptors_.resize(new_descriptor + 64);
Open_descriptor* pod = &this->open_descriptors_[new_descriptor];
pod->name = name;
pod->stack_next = -1;
pod->inuse = true;
pod->is_write = (flags & O_ACCMODE) != O_RDONLY;
if (!pod->is_claimed)
++this->current_;
pod->is_claimed = false;
if (this->current_ >= this->limit_)
this->close_some_descriptor();
return new_descriptor;
}
// We ran out of file descriptors.
{
Hold_optional_lock hl(this->lock_);
this->limit_ = this->current_ - 16;
if (this->limit_ < 8)
this->limit_ = 8;
if (!this->close_some_descriptor())
gold_fatal(_("out of file descriptors and couldn't close any"));
}
}
}
// Release a descriptor.
void
Descriptors::release(int descriptor, bool permanent)
{
Hold_optional_lock hl(this->lock_);
gold_assert(descriptor >= 0
&& (static_cast<size_t>(descriptor)
< this->open_descriptors_.size()));
Open_descriptor* pod = &this->open_descriptors_[descriptor];
if (permanent
|| (this->current_ > this->limit_ && !pod->is_write))
{
if (::close(descriptor) < 0)
gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
pod->name = NULL;
--this->current_;
}
else
{
pod->inuse = false;
if (!pod->is_write)
{
pod->stack_next = this->stack_top_;
this->stack_top_ = descriptor;
}
}
}
// Claim the file descriptor DESCRIPTOR for a plugin. This effectively
// removes the descriptor from the pool of linker-managed descriptors,
// as the plugin will assume responsibility for closing it.
// The IS_CLAIMED flag allows us to recognize when a file descriptor
// has been reused after being closed by the plugin.
void
Descriptors::claim_for_plugin(int descriptor)
{
Hold_lock hl(*this->lock_);
gold_assert(descriptor >= 0
&& (static_cast<size_t>(descriptor)
< this->open_descriptors_.size()));
Open_descriptor* pod = &this->open_descriptors_[descriptor];
pod->is_claimed = true;
}
// Close some descriptor. The lock is held when this is called. We
// close the descriptor on the top of the free stack. Note that this
// is the opposite of an LRU algorithm--we close the most recently
// used descriptor. That is because the linker tends to cycle through
// all the files; after we release a file, we are unlikely to need it
// again until we have looked at all the other files. Return true if
// we closed a descriptor.
bool
Descriptors::close_some_descriptor()
{
int last = -1;
int i = this->stack_top_;
while (i >= 0)
{
gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size());
Open_descriptor* pod = &this->open_descriptors_[i];
if (!pod->inuse && !pod->is_write)
{
if (::close(i) < 0)
gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
--this->current_;
pod->name = NULL;
if (last < 0)
this->stack_top_ = pod->stack_next;
else
this->open_descriptors_[last].stack_next = pod->stack_next;
return true;
}
last = i;
i = pod->stack_next;
}
// We couldn't find any descriptors to close. This is weird but not
// necessarily an error.
return false;
}
// The single global variable which manages descriptors.
Descriptors descriptors;
} // End namespace gold.