1// descriptors.cc -- manage file descriptors for gold
2
3// Copyright (C) 2008-2022 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
25#include <cerrno>
26#include <cstdio>
27#include <cstring>
28#include <fcntl.h>
29#include <string>
30#include <unistd.h>
31
32#include "debug.h"
33#include "parameters.h"
34#include "options.h"
35#include "gold-threads.h"
36#include "descriptors.h"
37#include "binary-io.h"
38
39// O_CLOEXEC is only available on newer systems.
40#ifndef O_CLOEXEC
41#define O_CLOEXEC 0
42#endif
43
44// Very old systems may not define FD_CLOEXEC.
45#ifndef FD_CLOEXEC
46#define FD_CLOEXEC 1
47#endif
48
49static inline void
50set_close_on_exec(int fd ATTRIBUTE_UNUSED)
51{
52// Mingw does not define F_SETFD.
53#ifdef F_SETFD
54  fcntl(fd, F_SETFD, FD_CLOEXEC);
55#endif
56}
57
58namespace gold
59{
60
61// Class Descriptors.
62
63// The default for limit_ is meant to simply be large.  It gets
64// adjusted downward if we run out of file descriptors.
65
66Descriptors::Descriptors()
67  : lock_(NULL), initialize_lock_(&this->lock_), open_descriptors_(),
68    stack_top_(-1), current_(0), limit_(8192 - 16)
69{
70  this->open_descriptors_.reserve(128);
71}
72
73// Open a file.
74
75int
76Descriptors::open(int descriptor, const char* name, int flags, int mode)
77{
78  // We don't initialize this until we are called, because we can't
79  // initialize a Lock until we have parsed the options to find out
80  // whether we are running with threads.  We can be called before
81  // options are valid when reading a linker script.
82  bool lock_initialized = this->initialize_lock_.initialize();
83
84  gold_assert(lock_initialized || descriptor < 0);
85
86  if (is_debugging_enabled(DEBUG_FILES))
87    this->limit_ = 8;
88
89  if (descriptor >= 0)
90    {
91      Hold_lock hl(*this->lock_);
92
93      gold_assert(static_cast<size_t>(descriptor)
94		  < this->open_descriptors_.size());
95      Open_descriptor* pod = &this->open_descriptors_[descriptor];
96      if (pod->name == name
97	  || (pod->name != NULL && strcmp(pod->name, name) == 0))
98	{
99	  gold_assert(!pod->inuse);
100	  pod->inuse = true;
101	  if (descriptor == this->stack_top_)
102	    {
103	      this->stack_top_ = pod->stack_next;
104	      pod->stack_next = -1;
105	      pod->is_on_stack = false;
106	    }
107	  gold_debug(DEBUG_FILES, "Reused existing descriptor %d for \"%s\"",
108		     descriptor, name);
109	  return descriptor;
110	}
111    }
112
113  while (true)
114    {
115      // We always want to set the close-on-exec flag; we don't
116      // require callers to pass it.
117      flags |= O_CLOEXEC;
118
119      // Always open the file as a binary file.
120      flags |= O_BINARY;
121
122      int new_descriptor = ::open(name, flags, mode);
123      if (new_descriptor < 0
124	  && errno != ENFILE
125	  && errno != EMFILE)
126	{
127	  if (descriptor >= 0 && errno == ENOENT)
128	    {
129	      {
130		Hold_lock hl(*this->lock_);
131
132		gold_error(_("file %s was removed during the link"), name);
133	      }
134
135	      errno = ENOENT;
136	    }
137
138	  gold_debug(DEBUG_FILES, "Opened new descriptor %d for \"%s\"",
139		     new_descriptor, name);
140	  return new_descriptor;
141	}
142
143      if (new_descriptor >= 0)
144	{
145	  // If we have any plugins, we really do need to set the
146	  // close-on-exec flag, even if O_CLOEXEC is not defined.
147	  // FIXME: In some cases O_CLOEXEC may be defined in the
148	  // header file but not supported by the kernel.
149	  // Unfortunately there doesn't seem to be any obvious way to
150	  // detect that, as unknown flags passed to open are ignored.
151	  if (O_CLOEXEC == 0
152	      && parameters->options_valid()
153	      && parameters->options().has_plugins())
154	    set_close_on_exec(new_descriptor);
155
156	  {
157	    Hold_optional_lock hl(this->lock_);
158
159	    if (static_cast<size_t>(new_descriptor)
160		>= this->open_descriptors_.size())
161	      this->open_descriptors_.resize(new_descriptor + 64);
162
163	    Open_descriptor* pod = &this->open_descriptors_[new_descriptor];
164	    pod->name = name;
165	    pod->stack_next = -1;
166	    pod->inuse = true;
167	    pod->is_write = (flags & O_ACCMODE) != O_RDONLY;
168	    pod->is_on_stack = false;
169
170	    ++this->current_;
171	    if (this->current_ >= this->limit_)
172	      this->close_some_descriptor();
173
174	    gold_debug(DEBUG_FILES, "Opened new descriptor %d for \"%s\"",
175		       new_descriptor, name);
176	    return new_descriptor;
177	  }
178	}
179
180      // We ran out of file descriptors.
181      {
182	Hold_optional_lock hl(this->lock_);
183
184	this->limit_ = this->current_ - 16;
185	if (this->limit_ < 8)
186	  this->limit_ = 8;
187	if (!this->close_some_descriptor())
188	  gold_fatal(_("out of file descriptors and couldn't close any"));
189      }
190    }
191}
192
193// Release a descriptor.
194
195void
196Descriptors::release(int descriptor, bool permanent)
197{
198  Hold_optional_lock hl(this->lock_);
199
200  gold_assert(descriptor >= 0
201	      && (static_cast<size_t>(descriptor)
202		  < this->open_descriptors_.size()));
203  Open_descriptor* pod = &this->open_descriptors_[descriptor];
204
205  if (permanent
206      || (this->current_ > this->limit_ && !pod->is_write))
207    {
208      if (::close(descriptor) < 0)
209	gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
210      pod->name = NULL;
211      --this->current_;
212    }
213  else
214    {
215      pod->inuse = false;
216      if (!pod->is_write && !pod->is_on_stack)
217	{
218	  pod->stack_next = this->stack_top_;
219	  this->stack_top_ = descriptor;
220	  pod->is_on_stack = true;
221	}
222    }
223
224  gold_debug(DEBUG_FILES, "Released descriptor %d for \"%s\"",
225	     descriptor, pod->name);
226}
227
228// Close some descriptor.  The lock is held when this is called.  We
229// close the descriptor on the top of the free stack.  Note that this
230// is the opposite of an LRU algorithm--we close the most recently
231// used descriptor.  That is because the linker tends to cycle through
232// all the files; after we release a file, we are unlikely to need it
233// again until we have looked at all the other files.  Return true if
234// we closed a descriptor.
235
236bool
237Descriptors::close_some_descriptor()
238{
239  int last = -1;
240  int i = this->stack_top_;
241  while (i >= 0)
242    {
243      gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size());
244      Open_descriptor* pod = &this->open_descriptors_[i];
245      if (!pod->inuse && !pod->is_write)
246	{
247	  if (::close(i) < 0)
248	    gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
249	  --this->current_;
250	  gold_debug(DEBUG_FILES, "Closed descriptor %d for \"%s\"",
251		     i, pod->name);
252	  pod->name = NULL;
253	  if (last < 0)
254	    this->stack_top_ = pod->stack_next;
255	  else
256	    this->open_descriptors_[last].stack_next = pod->stack_next;
257	  pod->stack_next = -1;
258	  pod->is_on_stack = false;
259	  return true;
260	}
261      last = i;
262      i = pod->stack_next;
263    }
264
265  // We couldn't find any descriptors to close.  This is weird but not
266  // necessarily an error.
267  return false;
268}
269
270// Close all the descriptors open for reading.
271
272void
273Descriptors::close_all()
274{
275  Hold_optional_lock hl(this->lock_);
276
277  for (size_t i = 0; i < this->open_descriptors_.size(); i++)
278    {
279      Open_descriptor* pod = &this->open_descriptors_[i];
280      if (pod->name != NULL && !pod->inuse && !pod->is_write)
281	{
282	  if (::close(i) < 0)
283	    gold_warning(_("while closing %s: %s"), pod->name, strerror(errno));
284	  gold_debug(DEBUG_FILES, "Closed descriptor %d for \"%s\" (close_all)",
285		     static_cast<int>(i), pod->name);
286	  pod->name = NULL;
287	  pod->stack_next = -1;
288	  pod->is_on_stack = false;
289	}
290    }
291  this->stack_top_ = -1;
292}
293
294// The single global variable which manages descriptors.
295
296Descriptors descriptors;
297
298} // End namespace gold.
299