Commit | Line | Data |
---|---|---|
2a00e4fb ILT |
1 | // descriptors.cc -- manage file descriptors for gold |
2 | ||
0f7c0701 | 3 | // Copyright 2008, 2009 Free Software Foundation, Inc. |
2a00e4fb ILT |
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 <cstring> | |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
29 | ||
30 | #include "parameters.h" | |
e85b18e1 | 31 | #include "options.h" |
2a00e4fb ILT |
32 | #include "gold-threads.h" |
33 | #include "descriptors.h" | |
34 | ||
e85b18e1 ILT |
35 | // Very old systems may not define FD_CLOEXEC. |
36 | #ifndef FD_CLOEXEC | |
37 | #define FD_CLOEXEC 1 | |
38 | #endif | |
39 | ||
40 | // O_CLOEXEC is only available on newer systems. | |
41 | #ifndef O_CLOEXEC | |
42 | #define O_CLOEXEC 0 | |
43 | #endif | |
44 | ||
2a00e4fb ILT |
45 | namespace gold |
46 | { | |
47 | ||
48 | // Class Descriptors. | |
49 | ||
50 | // The default for limit_ is meant to simply be large. It gets | |
51 | // adjusted downward if we run out of file descriptors. | |
52 | ||
53 | Descriptors::Descriptors() | |
54 | : lock_(NULL), open_descriptors_(), stack_top_(-1), current_(0), | |
55 | limit_(8192 - 16) | |
56 | { | |
57 | this->open_descriptors_.reserve(128); | |
58 | } | |
59 | ||
60 | // Open a file. | |
61 | ||
62 | int | |
63 | Descriptors::open(int descriptor, const char* name, int flags, int mode) | |
64 | { | |
65 | // We don't initialize this until we are called, because we can't | |
66 | // initialize a Lock until we have parsed the options to find out | |
67 | // whether we are running with threads. We can be called before | |
68 | // options are valid when reading a linker script. | |
69 | if (this->lock_ == NULL) | |
70 | { | |
71 | if (parameters->options_valid()) | |
72 | this->lock_ = new Lock(); | |
73 | else | |
74 | gold_assert(descriptor < 0); | |
75 | } | |
76 | ||
77 | if (descriptor >= 0) | |
78 | { | |
79 | Hold_lock hl(*this->lock_); | |
80 | ||
81 | gold_assert(static_cast<size_t>(descriptor) | |
82 | < this->open_descriptors_.size()); | |
83 | Open_descriptor* pod = &this->open_descriptors_[descriptor]; | |
84 | if (pod->name == name | |
85 | || (pod->name != NULL && strcmp(pod->name, name) == 0)) | |
86 | { | |
87 | gold_assert(!pod->inuse); | |
88 | pod->inuse = true; | |
61edd21f ILT |
89 | if (descriptor == this->stack_top_) |
90 | { | |
91 | this->stack_top_ = pod->stack_next; | |
92 | pod->stack_next = -1; | |
93 | pod->is_on_stack = false; | |
94 | } | |
2a00e4fb ILT |
95 | return descriptor; |
96 | } | |
97 | } | |
98 | ||
99 | while (true) | |
100 | { | |
e85b18e1 ILT |
101 | // We always want to set the close-on-exec flag; we don't |
102 | // require callers to pass it. | |
103 | flags |= O_CLOEXEC; | |
104 | ||
2a00e4fb ILT |
105 | int new_descriptor = ::open(name, flags, mode); |
106 | if (new_descriptor < 0 | |
107 | && errno != ENFILE | |
108 | && errno != EMFILE) | |
109 | { | |
110 | if (descriptor >= 0 && errno == ENOENT) | |
111 | { | |
112 | { | |
113 | Hold_lock hl(*this->lock_); | |
114 | ||
115 | gold_error(_("file %s was removed during the link"), | |
116 | this->open_descriptors_[descriptor].name); | |
117 | } | |
118 | ||
119 | errno = ENOENT; | |
120 | } | |
121 | ||
122 | return new_descriptor; | |
123 | } | |
124 | ||
125 | if (new_descriptor >= 0) | |
126 | { | |
e85b18e1 ILT |
127 | // If we have any plugins, we really do need to set the |
128 | // close-on-exec flag, even if O_CLOEXEC is not defined. | |
129 | // FIXME: In some cases O_CLOEXEC may be defined in the | |
130 | // header file but not supported by the kernel. | |
131 | // Unfortunately there doesn't seem to be any obvious way to | |
132 | // detect that, as unknown flags passed to open are ignored. | |
133 | if (O_CLOEXEC == 0 && parameters->options().has_plugins()) | |
134 | fcntl(new_descriptor, F_SETFD, FD_CLOEXEC); | |
135 | ||
136 | { | |
137 | Hold_optional_lock hl(this->lock_); | |
138 | ||
139 | if (static_cast<size_t>(new_descriptor) | |
140 | >= this->open_descriptors_.size()) | |
141 | this->open_descriptors_.resize(new_descriptor + 64); | |
142 | ||
143 | Open_descriptor* pod = &this->open_descriptors_[new_descriptor]; | |
144 | pod->name = name; | |
145 | pod->stack_next = -1; | |
146 | pod->inuse = true; | |
147 | pod->is_write = (flags & O_ACCMODE) != O_RDONLY; | |
148 | pod->is_on_stack = false; | |
149 | ||
150 | ++this->current_; | |
151 | if (this->current_ >= this->limit_) | |
152 | this->close_some_descriptor(); | |
153 | ||
154 | return new_descriptor; | |
155 | } | |
2a00e4fb ILT |
156 | } |
157 | ||
158 | // We ran out of file descriptors. | |
159 | { | |
160 | Hold_optional_lock hl(this->lock_); | |
161 | ||
162 | this->limit_ = this->current_ - 16; | |
163 | if (this->limit_ < 8) | |
164 | this->limit_ = 8; | |
165 | if (!this->close_some_descriptor()) | |
166 | gold_fatal(_("out of file descriptors and couldn't close any")); | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | // Release a descriptor. | |
172 | ||
173 | void | |
174 | Descriptors::release(int descriptor, bool permanent) | |
175 | { | |
176 | Hold_optional_lock hl(this->lock_); | |
177 | ||
178 | gold_assert(descriptor >= 0 | |
179 | && (static_cast<size_t>(descriptor) | |
180 | < this->open_descriptors_.size())); | |
181 | Open_descriptor* pod = &this->open_descriptors_[descriptor]; | |
182 | ||
183 | if (permanent | |
184 | || (this->current_ > this->limit_ && !pod->is_write)) | |
185 | { | |
186 | if (::close(descriptor) < 0) | |
187 | gold_warning(_("while closing %s: %s"), pod->name, strerror(errno)); | |
188 | pod->name = NULL; | |
189 | --this->current_; | |
190 | } | |
191 | else | |
192 | { | |
193 | pod->inuse = false; | |
61edd21f | 194 | if (!pod->is_write && !pod->is_on_stack) |
2a00e4fb ILT |
195 | { |
196 | pod->stack_next = this->stack_top_; | |
197 | this->stack_top_ = descriptor; | |
61edd21f | 198 | pod->is_on_stack = true; |
2a00e4fb ILT |
199 | } |
200 | } | |
201 | } | |
202 | ||
203 | // Close some descriptor. The lock is held when this is called. We | |
204 | // close the descriptor on the top of the free stack. Note that this | |
205 | // is the opposite of an LRU algorithm--we close the most recently | |
206 | // used descriptor. That is because the linker tends to cycle through | |
207 | // all the files; after we release a file, we are unlikely to need it | |
208 | // again until we have looked at all the other files. Return true if | |
209 | // we closed a descriptor. | |
210 | ||
211 | bool | |
212 | Descriptors::close_some_descriptor() | |
213 | { | |
214 | int last = -1; | |
215 | int i = this->stack_top_; | |
216 | while (i >= 0) | |
217 | { | |
218 | gold_assert(static_cast<size_t>(i) < this->open_descriptors_.size()); | |
219 | Open_descriptor* pod = &this->open_descriptors_[i]; | |
220 | if (!pod->inuse && !pod->is_write) | |
221 | { | |
222 | if (::close(i) < 0) | |
223 | gold_warning(_("while closing %s: %s"), pod->name, strerror(errno)); | |
224 | --this->current_; | |
225 | pod->name = NULL; | |
226 | if (last < 0) | |
227 | this->stack_top_ = pod->stack_next; | |
228 | else | |
229 | this->open_descriptors_[last].stack_next = pod->stack_next; | |
61edd21f ILT |
230 | pod->stack_next = -1; |
231 | pod->is_on_stack = false; | |
2a00e4fb ILT |
232 | return true; |
233 | } | |
234 | last = i; | |
235 | i = pod->stack_next; | |
236 | } | |
237 | ||
238 | // We couldn't find any descriptors to close. This is weird but not | |
239 | // necessarily an error. | |
240 | return false; | |
241 | } | |
242 | ||
243 | // The single global variable which manages descriptors. | |
244 | ||
245 | Descriptors descriptors; | |
246 | ||
247 | } // End namespace gold. |