* config/tc-sparc.c (md_apply_fix): Handle BFD_RELOC_8.
[deliverable/binutils-gdb.git] / gold / dirsearch.cc
CommitLineData
bae7f79e
ILT
1// dirsearch.cc -- directory searching for gold
2
1706a06f 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6cb15b7f
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
bae7f79e
ILT
23#include "gold.h"
24
25#include <cerrno>
75f2446e 26#include <cstring>
bae7f79e
ILT
27#include <sys/types.h>
28#include <dirent.h>
29
2285a610 30#include "debug.h"
bae7f79e 31#include "gold-threads.h"
17a1d0a9
ILT
32#include "options.h"
33#include "workqueue.h"
bae7f79e
ILT
34#include "dirsearch.h"
35
36namespace
37{
38
39// Read all the files in a directory.
40
41class Dir_cache
42{
43 public:
44 Dir_cache(const char* dirname)
45 : dirname_(dirname), files_()
46 { }
47
48 // Read the files in the directory.
49 void read_files();
50
51 // Return whether a file (a base name) is present in the directory.
52 bool find(const std::string&) const;
53
54 private:
55 // We can not copy this class.
56 Dir_cache(const Dir_cache&);
57 Dir_cache& operator=(const Dir_cache&);
58
59 const char* dirname_;
60 Unordered_set<std::string> files_;
61};
62
63void
64Dir_cache::read_files()
65{
66 DIR* d = opendir(this->dirname_);
67 if (d == NULL)
68 {
ae3a6d4f
ILT
69 // We ignore directories which do not exist or are actually file
70 // names.
71 if (errno != ENOENT && errno != ENOTDIR)
75f2446e
ILT
72 gold::gold_error(_("%s: can not read directory: %s"),
73 this->dirname_, strerror(errno));
74 return;
bae7f79e
ILT
75 }
76
77 dirent* de;
78 while ((de = readdir(d)) != NULL)
79 this->files_.insert(std::string(de->d_name));
80
81 if (closedir(d) != 0)
75f2446e
ILT
82 gold::gold_warning("%s: closedir failed: %s", this->dirname_,
83 strerror(errno));
bae7f79e
ILT
84}
85
86bool
87Dir_cache::find(const std::string& basename) const
88{
89 return this->files_.find(basename) != this->files_.end();
90}
91
92// A mapping from directory names to caches. A lock permits
93// concurrent update. There is no lock for read operations--some
94// other mechanism must be used to prevent reads from conflicting with
95// writes.
96
97class Dir_caches
98{
99 public:
100 Dir_caches()
101 : lock_(), caches_()
102 { }
103
104 ~Dir_caches();
105
106 // Add a cache for a directory.
107 void add(const char*);
108
109 // Look up a directory in the cache. This much be locked against
110 // calls to Add.
111 Dir_cache* lookup(const char*) const;
112
113 private:
114 // We can not copy this class.
115 Dir_caches(const Dir_caches&);
116 Dir_caches& operator=(const Dir_caches&);
117
118 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
119
120 gold::Lock lock_;
121 Cache_hash caches_;
122};
123
124Dir_caches::~Dir_caches()
125{
126 for (Cache_hash::iterator p = this->caches_.begin();
127 p != this->caches_.end();
128 ++p)
129 delete p->second;
130}
131
132void
133Dir_caches::add(const char* dirname)
134{
135 {
136 gold::Hold_lock hl(this->lock_);
137 if (this->lookup(dirname) != NULL)
138 return;
139 }
140
141 Dir_cache* cache = new Dir_cache(dirname);
142
143 cache->read_files();
144
145 {
146 gold::Hold_lock hl(this->lock_);
147
148 std::pair<const char*, Dir_cache*> v(dirname, cache);
149 std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
a3ad94ed 150 gold_assert(p.second);
bae7f79e
ILT
151 }
152}
153
154Dir_cache*
155Dir_caches::lookup(const char* dirname) const
156{
157 Cache_hash::const_iterator p = this->caches_.find(dirname);
158 if (p == this->caches_.end())
159 return NULL;
160 return p->second;
161}
162
163// The caches.
164
c7912668 165Dir_caches* caches;
bae7f79e
ILT
166
167// A Task to read the directory.
168
169class Dir_cache_task : public gold::Task
170{
171 public:
172 Dir_cache_task(const char* dir, gold::Task_token& token)
173 : dir_(dir), token_(token)
174 { }
175
17a1d0a9
ILT
176 gold::Task_token*
177 is_runnable();
bae7f79e 178
17a1d0a9
ILT
179 void
180 locks(gold::Task_locker*);
bae7f79e 181
c7912668
ILT
182 void
183 run(gold::Workqueue*);
184
185 std::string
186 get_name() const
187 { return std::string("Dir_cache_task ") + this->dir_; }
bae7f79e
ILT
188
189 private:
190 const char* dir_;
191 gold::Task_token& token_;
192};
193
194// We can always run the task to read the directory.
195
17a1d0a9
ILT
196gold::Task_token*
197Dir_cache_task::is_runnable()
bae7f79e 198{
17a1d0a9 199 return NULL;
bae7f79e
ILT
200}
201
202// Return the locks to hold. We use a blocker lock to prevent file
203// lookups from starting until the directory contents have been read.
204
17a1d0a9
ILT
205void
206Dir_cache_task::locks(gold::Task_locker* tl)
bae7f79e 207{
17a1d0a9 208 tl->add(this, &this->token_);
bae7f79e
ILT
209}
210
211// Run the task--read the directory contents.
212
213void
214Dir_cache_task::run(gold::Workqueue*)
215{
c7912668 216 caches->add(this->dir_);
bae7f79e
ILT
217}
218
219}
220
221namespace gold
222{
223
15f8229b
ILT
224// Initialize.
225
bae7f79e 226void
ad2d6943
ILT
227Dirsearch::initialize(Workqueue* workqueue,
228 const General_options::Dir_list* directories)
bae7f79e 229{
c7912668
ILT
230 gold_assert(caches == NULL);
231 caches = new Dir_caches;
ad2d6943 232 this->directories_ = directories;
fa17a3f4 233 this->token_.add_blockers(directories->size());
ad2d6943
ILT
234 for (General_options::Dir_list::const_iterator p = directories->begin();
235 p != directories->end();
bae7f79e 236 ++p)
fa17a3f4 237 workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_));
bae7f79e
ILT
238}
239
15f8229b
ILT
240// Search for a file. NOTE: we only log failed file-lookup attempts
241// here. Successfully lookups will eventually get logged in
242// File_read::open.
2285a610 243
bae7f79e 244std::string
1706a06f
ILT
245Dirsearch::find(const std::vector<std::string>& names,
246 bool* is_in_sysroot, int* pindex,
247 std::string *found_name) const
bae7f79e 248{
a3ad94ed 249 gold_assert(!this->token_.is_blocked());
15f8229b 250 gold_assert(*pindex >= 0);
bae7f79e 251
15f8229b
ILT
252 for (unsigned int i = static_cast<unsigned int>(*pindex);
253 i < this->directories_->size();
254 ++i)
bae7f79e 255 {
15f8229b 256 const Search_directory* p = &this->directories_->at(i);
c7912668 257 Dir_cache* pdc = caches->lookup(p->name().c_str());
a3ad94ed 258 gold_assert(pdc != NULL);
1706a06f
ILT
259 for (std::vector<std::string>::const_iterator n = names.begin();
260 n != names.end();
261 ++n)
ad2d6943 262 {
1706a06f
ILT
263 if (pdc->find(*n))
264 {
265 *is_in_sysroot = p->is_in_sysroot();
15f8229b 266 *pindex = i;
1706a06f
ILT
267 *found_name = *n;
268 return p->name() + '/' + *n;
269 }
270 else
271 gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed",
272 p->name().c_str(), (*n).c_str());
ad2d6943 273 }
bae7f79e
ILT
274 }
275
15f8229b 276 *pindex = -2;
bae7f79e
ILT
277 return std::string();
278}
279
280} // End namespace gold.
This page took 0.239818 seconds and 4 git commands to generate.