// descriptors.cc -- manage file descriptors for gold
-// Copyright 2008, 2009 Free Software Foundation, Inc.
+// Copyright (C) 2008-2014 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
#include "gold.h"
#include <cerrno>
+#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include "options.h"
#include "gold-threads.h"
#include "descriptors.h"
+#include "binary-io.h"
+
+// O_CLOEXEC is only available on newer systems.
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
// Very old systems may not define FD_CLOEXEC.
#ifndef FD_CLOEXEC
#define FD_CLOEXEC 1
#endif
-// O_CLOEXEC is only available on newer systems.
-#ifndef O_CLOEXEC
-#define O_CLOEXEC 0
+static inline void
+set_close_on_exec(int fd ATTRIBUTE_UNUSED)
+{
+// Mingw does not define F_SETFD.
+#ifdef F_SETFD
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
+}
namespace gold
{
// adjusted downward if we run out of file descriptors.
Descriptors::Descriptors()
- : lock_(NULL), open_descriptors_(), stack_top_(-1), current_(0),
- limit_(8192 - 16)
+ : lock_(NULL), initialize_lock_(&this->lock_), open_descriptors_(),
+ stack_top_(-1), current_(0), limit_(8192 - 16)
{
this->open_descriptors_.reserve(128);
}
// 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);
- }
+ bool lock_initialized = this->initialize_lock_.initialize();
+
+ gold_assert(lock_initialized || descriptor < 0);
if (descriptor >= 0)
{
// require callers to pass it.
flags |= O_CLOEXEC;
+ // Always open the file as a binary file.
+ flags |= O_BINARY;
+
int new_descriptor = ::open(name, flags, mode);
if (new_descriptor < 0
&& errno != ENFILE
{
Hold_lock hl(*this->lock_);
- gold_error(_("file %s was removed during the link"),
- this->open_descriptors_[descriptor].name);
+ gold_error(_("file %s was removed during the link"), name);
}
errno = ENOENT;
if (O_CLOEXEC == 0
&& parameters->options_valid()
&& parameters->options().has_plugins())
- fcntl(new_descriptor, F_SETFD, FD_CLOEXEC);
+ set_close_on_exec(new_descriptor);
{
Hold_optional_lock hl(this->lock_);
return false;
}
+// Close all the descriptors open for reading.
+
+void
+Descriptors::close_all()
+{
+ Hold_optional_lock hl(this->lock_);
+
+ for (size_t i = 0; i < this->open_descriptors_.size(); i++)
+ {
+ Open_descriptor* pod = &this->open_descriptors_[i];
+ if (pod->name != NULL && !pod->inuse && !pod->is_write)
+ {
+ if (::close(i) < 0)
+ gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
+ pod->name = NULL;
+ pod->stack_next = -1;
+ pod->is_on_stack = false;
+ }
+ }
+ this->stack_top_ = -1;
+}
+
// The single global variable which manages descriptors.
Descriptors descriptors;