1*fae548d3Szrj // fileread.cc -- read files for gold
2*fae548d3Szrj 
3*fae548d3Szrj // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*fae548d3Szrj 
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj 
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj 
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj 
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj 
23*fae548d3Szrj #include "gold.h"
24*fae548d3Szrj 
25*fae548d3Szrj #include <cstring>
26*fae548d3Szrj #include <cerrno>
27*fae548d3Szrj #include <climits>
28*fae548d3Szrj #include <fcntl.h>
29*fae548d3Szrj #include <unistd.h>
30*fae548d3Szrj 
31*fae548d3Szrj #ifdef HAVE_SYS_MMAN_H
32*fae548d3Szrj #include <sys/mman.h>
33*fae548d3Szrj #endif
34*fae548d3Szrj 
35*fae548d3Szrj #ifdef HAVE_READV
36*fae548d3Szrj #include <sys/uio.h>
37*fae548d3Szrj #endif
38*fae548d3Szrj 
39*fae548d3Szrj #include <sys/stat.h>
40*fae548d3Szrj #include "filenames.h"
41*fae548d3Szrj 
42*fae548d3Szrj #include "debug.h"
43*fae548d3Szrj #include "parameters.h"
44*fae548d3Szrj #include "options.h"
45*fae548d3Szrj #include "dirsearch.h"
46*fae548d3Szrj #include "target.h"
47*fae548d3Szrj #include "binary.h"
48*fae548d3Szrj #include "descriptors.h"
49*fae548d3Szrj #include "gold-threads.h"
50*fae548d3Szrj #include "fileread.h"
51*fae548d3Szrj 
52*fae548d3Szrj // For systems without mmap support.
53*fae548d3Szrj #ifndef HAVE_MMAP
54*fae548d3Szrj # define mmap gold_mmap
55*fae548d3Szrj # define munmap gold_munmap
56*fae548d3Szrj # ifndef MAP_FAILED
57*fae548d3Szrj #  define MAP_FAILED (reinterpret_cast<void*>(-1))
58*fae548d3Szrj # endif
59*fae548d3Szrj # ifndef PROT_READ
60*fae548d3Szrj #  define PROT_READ 0
61*fae548d3Szrj # endif
62*fae548d3Szrj # ifndef MAP_PRIVATE
63*fae548d3Szrj #  define MAP_PRIVATE 0
64*fae548d3Szrj # endif
65*fae548d3Szrj 
66*fae548d3Szrj # ifndef ENOSYS
67*fae548d3Szrj #  define ENOSYS EINVAL
68*fae548d3Szrj # endif
69*fae548d3Szrj 
70*fae548d3Szrj static void *
gold_mmap(void *,size_t,int,int,int,off_t)71*fae548d3Szrj gold_mmap(void *, size_t, int, int, int, off_t)
72*fae548d3Szrj {
73*fae548d3Szrj   errno = ENOSYS;
74*fae548d3Szrj   return MAP_FAILED;
75*fae548d3Szrj }
76*fae548d3Szrj 
77*fae548d3Szrj static int
gold_munmap(void *,size_t)78*fae548d3Szrj gold_munmap(void *, size_t)
79*fae548d3Szrj {
80*fae548d3Szrj   errno = ENOSYS;
81*fae548d3Szrj   return -1;
82*fae548d3Szrj }
83*fae548d3Szrj 
84*fae548d3Szrj #endif
85*fae548d3Szrj 
86*fae548d3Szrj #ifndef HAVE_READV
87*fae548d3Szrj struct iovec { void* iov_base; size_t iov_len; };
88*fae548d3Szrj ssize_t
readv(int,const iovec *,int)89*fae548d3Szrj readv(int, const iovec*, int)
90*fae548d3Szrj {
91*fae548d3Szrj   gold_unreachable();
92*fae548d3Szrj }
93*fae548d3Szrj #endif
94*fae548d3Szrj 
95*fae548d3Szrj namespace gold
96*fae548d3Szrj {
97*fae548d3Szrj 
98*fae548d3Szrj // Get the last modified time of an unopened file.
99*fae548d3Szrj 
100*fae548d3Szrj bool
get_mtime(const char * filename,Timespec * mtime)101*fae548d3Szrj get_mtime(const char* filename, Timespec* mtime)
102*fae548d3Szrj {
103*fae548d3Szrj   struct stat file_stat;
104*fae548d3Szrj 
105*fae548d3Szrj   if (stat(filename, &file_stat) < 0)
106*fae548d3Szrj     return false;
107*fae548d3Szrj #ifdef HAVE_STAT_ST_MTIM
108*fae548d3Szrj   mtime->seconds = file_stat.st_mtim.tv_sec;
109*fae548d3Szrj   mtime->nanoseconds = file_stat.st_mtim.tv_nsec;
110*fae548d3Szrj #else
111*fae548d3Szrj   mtime->seconds = file_stat.st_mtime;
112*fae548d3Szrj   mtime->nanoseconds = 0;
113*fae548d3Szrj #endif
114*fae548d3Szrj   return true;
115*fae548d3Szrj }
116*fae548d3Szrj 
117*fae548d3Szrj // Class File_read.
118*fae548d3Szrj 
119*fae548d3Szrj // A lock for the File_read static variables.
120*fae548d3Szrj static Lock* file_counts_lock = NULL;
121*fae548d3Szrj static Initialize_lock file_counts_initialize_lock(&file_counts_lock);
122*fae548d3Szrj 
123*fae548d3Szrj // The File_read static variables.
124*fae548d3Szrj unsigned long long File_read::total_mapped_bytes;
125*fae548d3Szrj unsigned long long File_read::current_mapped_bytes;
126*fae548d3Szrj unsigned long long File_read::maximum_mapped_bytes;
127*fae548d3Szrj 
128*fae548d3Szrj // Class File_read::View.
129*fae548d3Szrj 
~View()130*fae548d3Szrj File_read::View::~View()
131*fae548d3Szrj {
132*fae548d3Szrj   gold_assert(!this->is_locked());
133*fae548d3Szrj   switch (this->data_ownership_)
134*fae548d3Szrj     {
135*fae548d3Szrj     case DATA_ALLOCATED_ARRAY:
136*fae548d3Szrj       free(const_cast<unsigned char*>(this->data_));
137*fae548d3Szrj       break;
138*fae548d3Szrj     case DATA_MMAPPED:
139*fae548d3Szrj       if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
140*fae548d3Szrj 	gold_warning(_("munmap failed: %s"), strerror(errno));
141*fae548d3Szrj       if (!parameters->options_valid() || parameters->options().stats())
142*fae548d3Szrj 	{
143*fae548d3Szrj 	  file_counts_initialize_lock.initialize();
144*fae548d3Szrj 	  Hold_optional_lock hl(file_counts_lock);
145*fae548d3Szrj 	  File_read::current_mapped_bytes -= this->size_;
146*fae548d3Szrj 	}
147*fae548d3Szrj       break;
148*fae548d3Szrj     case DATA_NOT_OWNED:
149*fae548d3Szrj       break;
150*fae548d3Szrj     default:
151*fae548d3Szrj       gold_unreachable();
152*fae548d3Szrj     }
153*fae548d3Szrj }
154*fae548d3Szrj 
155*fae548d3Szrj void
lock()156*fae548d3Szrj File_read::View::lock()
157*fae548d3Szrj {
158*fae548d3Szrj   ++this->lock_count_;
159*fae548d3Szrj }
160*fae548d3Szrj 
161*fae548d3Szrj void
unlock()162*fae548d3Szrj File_read::View::unlock()
163*fae548d3Szrj {
164*fae548d3Szrj   gold_assert(this->lock_count_ > 0);
165*fae548d3Szrj   --this->lock_count_;
166*fae548d3Szrj }
167*fae548d3Szrj 
168*fae548d3Szrj bool
is_locked()169*fae548d3Szrj File_read::View::is_locked()
170*fae548d3Szrj {
171*fae548d3Szrj   return this->lock_count_ > 0;
172*fae548d3Szrj }
173*fae548d3Szrj 
174*fae548d3Szrj // Class File_read.
175*fae548d3Szrj 
~File_read()176*fae548d3Szrj File_read::~File_read()
177*fae548d3Szrj {
178*fae548d3Szrj   gold_assert(this->token_.is_writable());
179*fae548d3Szrj   if (this->is_descriptor_opened_)
180*fae548d3Szrj     {
181*fae548d3Szrj       release_descriptor(this->descriptor_, true);
182*fae548d3Szrj       this->descriptor_ = -1;
183*fae548d3Szrj       this->is_descriptor_opened_ = false;
184*fae548d3Szrj     }
185*fae548d3Szrj   this->name_.clear();
186*fae548d3Szrj   this->clear_views(CLEAR_VIEWS_ALL);
187*fae548d3Szrj }
188*fae548d3Szrj 
189*fae548d3Szrj // Open the file.
190*fae548d3Szrj 
191*fae548d3Szrj bool
open(const Task * task,const std::string & name)192*fae548d3Szrj File_read::open(const Task* task, const std::string& name)
193*fae548d3Szrj {
194*fae548d3Szrj   gold_assert(this->token_.is_writable()
195*fae548d3Szrj 	      && this->descriptor_ < 0
196*fae548d3Szrj 	      && !this->is_descriptor_opened_
197*fae548d3Szrj 	      && this->name_.empty());
198*fae548d3Szrj   this->name_ = name;
199*fae548d3Szrj 
200*fae548d3Szrj   this->descriptor_ = open_descriptor(-1, this->name_.c_str(),
201*fae548d3Szrj 				      O_RDONLY);
202*fae548d3Szrj 
203*fae548d3Szrj   if (this->descriptor_ >= 0)
204*fae548d3Szrj     {
205*fae548d3Szrj       this->is_descriptor_opened_ = true;
206*fae548d3Szrj       struct stat s;
207*fae548d3Szrj       if (::fstat(this->descriptor_, &s) < 0)
208*fae548d3Szrj 	gold_error(_("%s: fstat failed: %s"),
209*fae548d3Szrj 		   this->name_.c_str(), strerror(errno));
210*fae548d3Szrj       this->size_ = s.st_size;
211*fae548d3Szrj       gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
212*fae548d3Szrj 		 this->name_.c_str());
213*fae548d3Szrj       this->token_.add_writer(task);
214*fae548d3Szrj     }
215*fae548d3Szrj 
216*fae548d3Szrj   return this->descriptor_ >= 0;
217*fae548d3Szrj }
218*fae548d3Szrj 
219*fae548d3Szrj // Open the file with the contents in memory.
220*fae548d3Szrj 
221*fae548d3Szrj bool
open(const Task * task,const std::string & name,const unsigned char * contents,off_t size)222*fae548d3Szrj File_read::open(const Task* task, const std::string& name,
223*fae548d3Szrj 		const unsigned char* contents, off_t size)
224*fae548d3Szrj {
225*fae548d3Szrj   gold_assert(this->token_.is_writable()
226*fae548d3Szrj 	      && this->descriptor_ < 0
227*fae548d3Szrj 	      && !this->is_descriptor_opened_
228*fae548d3Szrj 	      && this->name_.empty());
229*fae548d3Szrj   this->name_ = name;
230*fae548d3Szrj   this->whole_file_view_ = new View(0, size, contents, 0, false,
231*fae548d3Szrj 				    View::DATA_NOT_OWNED);
232*fae548d3Szrj   this->add_view(this->whole_file_view_);
233*fae548d3Szrj   this->size_ = size;
234*fae548d3Szrj   this->token_.add_writer(task);
235*fae548d3Szrj   return true;
236*fae548d3Szrj }
237*fae548d3Szrj 
238*fae548d3Szrj // Reopen a descriptor if necessary.
239*fae548d3Szrj 
240*fae548d3Szrj void
reopen_descriptor()241*fae548d3Szrj File_read::reopen_descriptor()
242*fae548d3Szrj {
243*fae548d3Szrj   if (!this->is_descriptor_opened_)
244*fae548d3Szrj     {
245*fae548d3Szrj       this->descriptor_ = open_descriptor(this->descriptor_,
246*fae548d3Szrj 					  this->name_.c_str(),
247*fae548d3Szrj 					  O_RDONLY);
248*fae548d3Szrj       if (this->descriptor_ < 0)
249*fae548d3Szrj 	gold_fatal(_("could not reopen file %s"), this->name_.c_str());
250*fae548d3Szrj       this->is_descriptor_opened_ = true;
251*fae548d3Szrj     }
252*fae548d3Szrj }
253*fae548d3Szrj 
254*fae548d3Szrj // Release the file.  This is called when we are done with the file in
255*fae548d3Szrj // a Task.
256*fae548d3Szrj 
257*fae548d3Szrj void
release()258*fae548d3Szrj File_read::release()
259*fae548d3Szrj {
260*fae548d3Szrj   gold_assert(this->is_locked());
261*fae548d3Szrj 
262*fae548d3Szrj   if (!parameters->options_valid() || parameters->options().stats())
263*fae548d3Szrj     {
264*fae548d3Szrj       file_counts_initialize_lock.initialize();
265*fae548d3Szrj       Hold_optional_lock hl(file_counts_lock);
266*fae548d3Szrj       File_read::total_mapped_bytes += this->mapped_bytes_;
267*fae548d3Szrj       File_read::current_mapped_bytes += this->mapped_bytes_;
268*fae548d3Szrj       if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
269*fae548d3Szrj 	File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
270*fae548d3Szrj     }
271*fae548d3Szrj 
272*fae548d3Szrj   this->mapped_bytes_ = 0;
273*fae548d3Szrj 
274*fae548d3Szrj   // Only clear views if there is only one attached object.  Otherwise
275*fae548d3Szrj   // we waste time trying to clear cached archive views.  Similarly
276*fae548d3Szrj   // for releasing the descriptor.
277*fae548d3Szrj   if (this->object_count_ <= 1)
278*fae548d3Szrj     {
279*fae548d3Szrj       this->clear_views(CLEAR_VIEWS_NORMAL);
280*fae548d3Szrj       if (this->is_descriptor_opened_)
281*fae548d3Szrj 	{
282*fae548d3Szrj 	  release_descriptor(this->descriptor_, false);
283*fae548d3Szrj 	  this->is_descriptor_opened_ = false;
284*fae548d3Szrj 	}
285*fae548d3Szrj     }
286*fae548d3Szrj 
287*fae548d3Szrj   this->released_ = true;
288*fae548d3Szrj }
289*fae548d3Szrj 
290*fae548d3Szrj // Lock the file.
291*fae548d3Szrj 
292*fae548d3Szrj void
lock(const Task * task)293*fae548d3Szrj File_read::lock(const Task* task)
294*fae548d3Szrj {
295*fae548d3Szrj   gold_assert(this->released_);
296*fae548d3Szrj   gold_debug(DEBUG_FILES, "Locking file \"%s\"", this->name_.c_str());
297*fae548d3Szrj   this->token_.add_writer(task);
298*fae548d3Szrj   this->released_ = false;
299*fae548d3Szrj }
300*fae548d3Szrj 
301*fae548d3Szrj // Unlock the file.
302*fae548d3Szrj 
303*fae548d3Szrj void
unlock(const Task * task)304*fae548d3Szrj File_read::unlock(const Task* task)
305*fae548d3Szrj {
306*fae548d3Szrj   gold_debug(DEBUG_FILES, "Unlocking file \"%s\"", this->name_.c_str());
307*fae548d3Szrj   this->release();
308*fae548d3Szrj   this->token_.remove_writer(task);
309*fae548d3Szrj }
310*fae548d3Szrj 
311*fae548d3Szrj // Return whether the file is locked.
312*fae548d3Szrj 
313*fae548d3Szrj bool
is_locked() const314*fae548d3Szrj File_read::is_locked() const
315*fae548d3Szrj {
316*fae548d3Szrj   if (!this->token_.is_writable())
317*fae548d3Szrj     return true;
318*fae548d3Szrj   // The file is not locked, so it should have been released.
319*fae548d3Szrj   gold_assert(this->released_);
320*fae548d3Szrj   return false;
321*fae548d3Szrj }
322*fae548d3Szrj 
323*fae548d3Szrj // See if we have a view which covers the file starting at START for
324*fae548d3Szrj // SIZE bytes.  Return a pointer to the View if found, NULL if not.
325*fae548d3Szrj // If BYTESHIFT is not -1U, the returned View must have the specified
326*fae548d3Szrj // byte shift; otherwise, it may have any byte shift.  If VSHIFTED is
327*fae548d3Szrj // not NULL, this sets *VSHIFTED to a view which would have worked if
328*fae548d3Szrj // not for the requested BYTESHIFT.
329*fae548d3Szrj 
330*fae548d3Szrj inline File_read::View*
find_view(off_t start,section_size_type size,unsigned int byteshift,File_read::View ** vshifted) const331*fae548d3Szrj File_read::find_view(off_t start, section_size_type size,
332*fae548d3Szrj 		     unsigned int byteshift, File_read::View** vshifted) const
333*fae548d3Szrj {
334*fae548d3Szrj   gold_assert(start <= this->size_
335*fae548d3Szrj 	      && (static_cast<unsigned long long>(size)
336*fae548d3Szrj 		  <= static_cast<unsigned long long>(this->size_ - start)));
337*fae548d3Szrj 
338*fae548d3Szrj   if (vshifted != NULL)
339*fae548d3Szrj     *vshifted = NULL;
340*fae548d3Szrj 
341*fae548d3Szrj   // If we have the whole file mmapped, and the alignment is right,
342*fae548d3Szrj   // we can return it.
343*fae548d3Szrj   if (this->whole_file_view_)
344*fae548d3Szrj     if (byteshift == -1U || byteshift == 0)
345*fae548d3Szrj       return this->whole_file_view_;
346*fae548d3Szrj 
347*fae548d3Szrj   off_t page = File_read::page_offset(start);
348*fae548d3Szrj 
349*fae548d3Szrj   unsigned int bszero = 0;
350*fae548d3Szrj   Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1,
351*fae548d3Szrj 								    bszero));
352*fae548d3Szrj 
353*fae548d3Szrj   while (p != this->views_.end() && p->first.first <= page)
354*fae548d3Szrj     {
355*fae548d3Szrj       if (p->second->start() <= start
356*fae548d3Szrj 	  && (p->second->start() + static_cast<off_t>(p->second->size())
357*fae548d3Szrj 	      >= start + static_cast<off_t>(size)))
358*fae548d3Szrj 	{
359*fae548d3Szrj 	  if (byteshift == -1U || byteshift == p->second->byteshift())
360*fae548d3Szrj 	    {
361*fae548d3Szrj 	      p->second->set_accessed();
362*fae548d3Szrj 	      return p->second;
363*fae548d3Szrj 	    }
364*fae548d3Szrj 
365*fae548d3Szrj 	  if (vshifted != NULL && *vshifted == NULL)
366*fae548d3Szrj 	    *vshifted = p->second;
367*fae548d3Szrj 	}
368*fae548d3Szrj 
369*fae548d3Szrj       ++p;
370*fae548d3Szrj     }
371*fae548d3Szrj 
372*fae548d3Szrj   return NULL;
373*fae548d3Szrj }
374*fae548d3Szrj 
375*fae548d3Szrj // Read SIZE bytes from the file starting at offset START.  Read into
376*fae548d3Szrj // the buffer at P.
377*fae548d3Szrj 
378*fae548d3Szrj void
do_read(off_t start,section_size_type size,void * p)379*fae548d3Szrj File_read::do_read(off_t start, section_size_type size, void* p)
380*fae548d3Szrj {
381*fae548d3Szrj   ssize_t bytes;
382*fae548d3Szrj   if (this->whole_file_view_ != NULL)
383*fae548d3Szrj     {
384*fae548d3Szrj       bytes = this->size_ - start;
385*fae548d3Szrj       if (static_cast<section_size_type>(bytes) >= size)
386*fae548d3Szrj 	{
387*fae548d3Szrj 	  memcpy(p, this->whole_file_view_->data() + start, size);
388*fae548d3Szrj 	  return;
389*fae548d3Szrj 	}
390*fae548d3Szrj     }
391*fae548d3Szrj   else
392*fae548d3Szrj     {
393*fae548d3Szrj       this->reopen_descriptor();
394*fae548d3Szrj 
395*fae548d3Szrj       char *read_ptr = static_cast<char *>(p);
396*fae548d3Szrj       off_t read_pos = start;
397*fae548d3Szrj       size_t to_read = size;
398*fae548d3Szrj       do
399*fae548d3Szrj 	{
400*fae548d3Szrj 	  bytes = ::pread(this->descriptor_, read_ptr, to_read, read_pos);
401*fae548d3Szrj 	  if (bytes < 0)
402*fae548d3Szrj 	    gold_fatal(_("%s: pread failed: %s"),
403*fae548d3Szrj 		       this->filename().c_str(), strerror(errno));
404*fae548d3Szrj 
405*fae548d3Szrj 	  read_pos += bytes;
406*fae548d3Szrj 	  read_ptr += bytes;
407*fae548d3Szrj 	  to_read -= bytes;
408*fae548d3Szrj 	  if (to_read == 0)
409*fae548d3Szrj 	    return;
410*fae548d3Szrj 	}
411*fae548d3Szrj       while (bytes > 0);
412*fae548d3Szrj 
413*fae548d3Szrj       bytes = size - to_read;
414*fae548d3Szrj     }
415*fae548d3Szrj 
416*fae548d3Szrj   gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
417*fae548d3Szrj 	     this->filename().c_str(),
418*fae548d3Szrj 	     static_cast<long long>(bytes),
419*fae548d3Szrj 	     static_cast<long long>(size),
420*fae548d3Szrj 	     static_cast<long long>(start));
421*fae548d3Szrj }
422*fae548d3Szrj 
423*fae548d3Szrj // Read data from the file.
424*fae548d3Szrj 
425*fae548d3Szrj void
read(off_t start,section_size_type size,void * p)426*fae548d3Szrj File_read::read(off_t start, section_size_type size, void* p)
427*fae548d3Szrj {
428*fae548d3Szrj   const File_read::View* pv = this->find_view(start, size, -1U, NULL);
429*fae548d3Szrj   if (pv != NULL)
430*fae548d3Szrj     {
431*fae548d3Szrj       memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
432*fae548d3Szrj       return;
433*fae548d3Szrj     }
434*fae548d3Szrj 
435*fae548d3Szrj   this->do_read(start, size, p);
436*fae548d3Szrj }
437*fae548d3Szrj 
438*fae548d3Szrj // Add a new view.  There may already be an existing view at this
439*fae548d3Szrj // offset.  If there is, the new view will be larger, and should
440*fae548d3Szrj // replace the old view.
441*fae548d3Szrj 
442*fae548d3Szrj void
add_view(File_read::View * v)443*fae548d3Szrj File_read::add_view(File_read::View* v)
444*fae548d3Szrj {
445*fae548d3Szrj   std::pair<Views::iterator, bool> ins =
446*fae548d3Szrj     this->views_.insert(std::make_pair(std::make_pair(v->start(),
447*fae548d3Szrj 						      v->byteshift()),
448*fae548d3Szrj 				       v));
449*fae548d3Szrj   if (ins.second)
450*fae548d3Szrj     return;
451*fae548d3Szrj 
452*fae548d3Szrj   // There was an existing view at this offset.  It must not be large
453*fae548d3Szrj   // enough.  We can't delete it here, since something might be using
454*fae548d3Szrj   // it; we put it on a list to be deleted when the file is unlocked.
455*fae548d3Szrj   File_read::View* vold = ins.first->second;
456*fae548d3Szrj   gold_assert(vold->size() < v->size());
457*fae548d3Szrj   if (vold->should_cache())
458*fae548d3Szrj     {
459*fae548d3Szrj       v->set_cache();
460*fae548d3Szrj       vold->clear_cache();
461*fae548d3Szrj     }
462*fae548d3Szrj   this->saved_views_.push_back(vold);
463*fae548d3Szrj 
464*fae548d3Szrj   ins.first->second = v;
465*fae548d3Szrj }
466*fae548d3Szrj 
467*fae548d3Szrj // Make a new view with a specified byteshift, reading the data from
468*fae548d3Szrj // the file.
469*fae548d3Szrj 
470*fae548d3Szrj File_read::View*
make_view(off_t start,section_size_type size,unsigned int byteshift,bool cache)471*fae548d3Szrj File_read::make_view(off_t start, section_size_type size,
472*fae548d3Szrj 		     unsigned int byteshift, bool cache)
473*fae548d3Szrj {
474*fae548d3Szrj   gold_assert(size > 0);
475*fae548d3Szrj   gold_assert(start <= this->size_
476*fae548d3Szrj 	      && (static_cast<unsigned long long>(size)
477*fae548d3Szrj 		  <= static_cast<unsigned long long>(this->size_ - start)));
478*fae548d3Szrj 
479*fae548d3Szrj   off_t poff = File_read::page_offset(start);
480*fae548d3Szrj 
481*fae548d3Szrj   section_size_type psize = File_read::pages(size + (start - poff));
482*fae548d3Szrj 
483*fae548d3Szrj   if (poff + static_cast<off_t>(psize) >= this->size_)
484*fae548d3Szrj     {
485*fae548d3Szrj       psize = this->size_ - poff;
486*fae548d3Szrj       gold_assert(psize >= size);
487*fae548d3Szrj     }
488*fae548d3Szrj 
489*fae548d3Szrj   void* p;
490*fae548d3Szrj   View::Data_ownership ownership;
491*fae548d3Szrj   if (byteshift != 0)
492*fae548d3Szrj     {
493*fae548d3Szrj       p = malloc(psize + byteshift);
494*fae548d3Szrj       if (p == NULL)
495*fae548d3Szrj 	gold_nomem();
496*fae548d3Szrj       memset(p, 0, byteshift);
497*fae548d3Szrj       this->do_read(poff, psize, static_cast<unsigned char*>(p) + byteshift);
498*fae548d3Szrj       ownership = View::DATA_ALLOCATED_ARRAY;
499*fae548d3Szrj     }
500*fae548d3Szrj   else
501*fae548d3Szrj     {
502*fae548d3Szrj       this->reopen_descriptor();
503*fae548d3Szrj       p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE, this->descriptor_, poff);
504*fae548d3Szrj       if (p != MAP_FAILED)
505*fae548d3Szrj 	{
506*fae548d3Szrj 	  ownership = View::DATA_MMAPPED;
507*fae548d3Szrj 	  this->mapped_bytes_ += psize;
508*fae548d3Szrj 	}
509*fae548d3Szrj       else
510*fae548d3Szrj 	{
511*fae548d3Szrj 	  p = malloc(psize);
512*fae548d3Szrj 	  if (p == NULL)
513*fae548d3Szrj 	    gold_nomem();
514*fae548d3Szrj 	  this->do_read(poff, psize, p);
515*fae548d3Szrj 	  ownership = View::DATA_ALLOCATED_ARRAY;
516*fae548d3Szrj 	}
517*fae548d3Szrj     }
518*fae548d3Szrj 
519*fae548d3Szrj   const unsigned char* pbytes = static_cast<const unsigned char*>(p);
520*fae548d3Szrj   File_read::View* v = new File_read::View(poff, psize, pbytes, byteshift,
521*fae548d3Szrj 					   cache, ownership);
522*fae548d3Szrj 
523*fae548d3Szrj   this->add_view(v);
524*fae548d3Szrj 
525*fae548d3Szrj   return v;
526*fae548d3Szrj }
527*fae548d3Szrj 
528*fae548d3Szrj // Find a View or make a new one, shifted as required by the file
529*fae548d3Szrj // offset OFFSET and ALIGNED.
530*fae548d3Szrj 
531*fae548d3Szrj File_read::View*
find_or_make_view(off_t offset,off_t start,section_size_type size,bool aligned,bool cache)532*fae548d3Szrj File_read::find_or_make_view(off_t offset, off_t start,
533*fae548d3Szrj 			     section_size_type size, bool aligned, bool cache)
534*fae548d3Szrj {
535*fae548d3Szrj   // Check that start and end of the view are within the file.
536*fae548d3Szrj   if (start > this->size_
537*fae548d3Szrj       || (static_cast<unsigned long long>(size)
538*fae548d3Szrj 	  > static_cast<unsigned long long>(this->size_ - start)))
539*fae548d3Szrj     gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
540*fae548d3Szrj 		 "size of file; the file may be corrupt"),
541*fae548d3Szrj 		   this->filename().c_str(),
542*fae548d3Szrj 		   static_cast<long long>(size),
543*fae548d3Szrj 		   static_cast<long long>(start));
544*fae548d3Szrj 
545*fae548d3Szrj   unsigned int byteshift;
546*fae548d3Szrj   if (offset == 0)
547*fae548d3Szrj     byteshift = 0;
548*fae548d3Szrj   else
549*fae548d3Szrj     {
550*fae548d3Szrj       unsigned int target_size = (!parameters->target_valid()
551*fae548d3Szrj 				  ? 64
552*fae548d3Szrj 				  : parameters->target().get_size());
553*fae548d3Szrj       byteshift = offset & ((target_size / 8) - 1);
554*fae548d3Szrj 
555*fae548d3Szrj       // Set BYTESHIFT to the number of dummy bytes which must be
556*fae548d3Szrj       // inserted before the data in order for this data to be
557*fae548d3Szrj       // aligned.
558*fae548d3Szrj       if (byteshift != 0)
559*fae548d3Szrj 	byteshift = (target_size / 8) - byteshift;
560*fae548d3Szrj     }
561*fae548d3Szrj 
562*fae548d3Szrj   // If --map-whole-files is set, make sure we have a
563*fae548d3Szrj   // whole file view.  Options may not yet be ready, e.g.,
564*fae548d3Szrj   // when reading a version script.  We then default to
565*fae548d3Szrj   // --no-map-whole-files.
566*fae548d3Szrj   if (this->whole_file_view_ == NULL
567*fae548d3Szrj       && parameters->options_valid()
568*fae548d3Szrj       && parameters->options().map_whole_files())
569*fae548d3Szrj     this->whole_file_view_ = this->make_view(0, this->size_, 0, cache);
570*fae548d3Szrj 
571*fae548d3Szrj   // Try to find a View with the required BYTESHIFT.
572*fae548d3Szrj   File_read::View* vshifted;
573*fae548d3Szrj   File_read::View* v = this->find_view(offset + start, size,
574*fae548d3Szrj 				       aligned ? byteshift : -1U,
575*fae548d3Szrj 				       &vshifted);
576*fae548d3Szrj   if (v != NULL)
577*fae548d3Szrj     {
578*fae548d3Szrj       if (cache)
579*fae548d3Szrj 	v->set_cache();
580*fae548d3Szrj       return v;
581*fae548d3Szrj     }
582*fae548d3Szrj 
583*fae548d3Szrj   // If VSHIFTED is not NULL, then it has the data we need, but with
584*fae548d3Szrj   // the wrong byteshift.
585*fae548d3Szrj   v = vshifted;
586*fae548d3Szrj   if (v != NULL)
587*fae548d3Szrj     {
588*fae548d3Szrj       gold_assert(aligned);
589*fae548d3Szrj 
590*fae548d3Szrj       unsigned char* pbytes;
591*fae548d3Szrj       pbytes = static_cast<unsigned char*>(malloc(v->size() + byteshift));
592*fae548d3Szrj       if (pbytes == NULL)
593*fae548d3Szrj 	gold_nomem();
594*fae548d3Szrj       memset(pbytes, 0, byteshift);
595*fae548d3Szrj       memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
596*fae548d3Szrj 
597*fae548d3Szrj       File_read::View* shifted_view =
598*fae548d3Szrj 	  new File_read::View(v->start(), v->size(), pbytes, byteshift,
599*fae548d3Szrj 			      cache, View::DATA_ALLOCATED_ARRAY);
600*fae548d3Szrj 
601*fae548d3Szrj       this->add_view(shifted_view);
602*fae548d3Szrj       return shifted_view;
603*fae548d3Szrj     }
604*fae548d3Szrj 
605*fae548d3Szrj   // Make a new view.  If we don't need an aligned view, use a
606*fae548d3Szrj   // byteshift of 0, so that we can use mmap.
607*fae548d3Szrj   return this->make_view(offset + start, size,
608*fae548d3Szrj 			 aligned ? byteshift : 0,
609*fae548d3Szrj 			 cache);
610*fae548d3Szrj }
611*fae548d3Szrj 
612*fae548d3Szrj // Get a view into the file.
613*fae548d3Szrj 
614*fae548d3Szrj const unsigned char*
get_view(off_t offset,off_t start,section_size_type size,bool aligned,bool cache)615*fae548d3Szrj File_read::get_view(off_t offset, off_t start, section_size_type size,
616*fae548d3Szrj 		    bool aligned, bool cache)
617*fae548d3Szrj {
618*fae548d3Szrj   File_read::View* pv = this->find_or_make_view(offset, start, size,
619*fae548d3Szrj 						aligned, cache);
620*fae548d3Szrj   return pv->data() + (offset + start - pv->start() + pv->byteshift());
621*fae548d3Szrj }
622*fae548d3Szrj 
623*fae548d3Szrj File_view*
get_lasting_view(off_t offset,off_t start,section_size_type size,bool aligned,bool cache)624*fae548d3Szrj File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
625*fae548d3Szrj 			    bool aligned, bool cache)
626*fae548d3Szrj {
627*fae548d3Szrj   File_read::View* pv = this->find_or_make_view(offset, start, size,
628*fae548d3Szrj 						aligned, cache);
629*fae548d3Szrj   pv->lock();
630*fae548d3Szrj   return new File_view(*this, pv,
631*fae548d3Szrj 		       (pv->data()
632*fae548d3Szrj 			+ (offset + start - pv->start() + pv->byteshift())));
633*fae548d3Szrj }
634*fae548d3Szrj 
635*fae548d3Szrj // Use readv to read COUNT entries from RM starting at START.  BASE
636*fae548d3Szrj // must be added to all file offsets in RM.
637*fae548d3Szrj 
638*fae548d3Szrj void
do_readv(off_t base,const Read_multiple & rm,size_t start,size_t count)639*fae548d3Szrj File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
640*fae548d3Szrj 		    size_t count)
641*fae548d3Szrj {
642*fae548d3Szrj   unsigned char discard[File_read::page_size];
643*fae548d3Szrj   iovec iov[File_read::max_readv_entries * 2];
644*fae548d3Szrj   size_t iov_index = 0;
645*fae548d3Szrj 
646*fae548d3Szrj   off_t first_offset = rm[start].file_offset;
647*fae548d3Szrj   off_t last_offset = first_offset;
648*fae548d3Szrj   ssize_t want = 0;
649*fae548d3Szrj   for (size_t i = 0; i < count; ++i)
650*fae548d3Szrj     {
651*fae548d3Szrj       const Read_multiple_entry& i_entry(rm[start + i]);
652*fae548d3Szrj 
653*fae548d3Szrj       if (i_entry.file_offset > last_offset)
654*fae548d3Szrj 	{
655*fae548d3Szrj 	  size_t skip = i_entry.file_offset - last_offset;
656*fae548d3Szrj 	  gold_assert(skip <= sizeof discard);
657*fae548d3Szrj 
658*fae548d3Szrj 	  iov[iov_index].iov_base = discard;
659*fae548d3Szrj 	  iov[iov_index].iov_len = skip;
660*fae548d3Szrj 	  ++iov_index;
661*fae548d3Szrj 
662*fae548d3Szrj 	  want += skip;
663*fae548d3Szrj 	}
664*fae548d3Szrj 
665*fae548d3Szrj       iov[iov_index].iov_base = i_entry.buffer;
666*fae548d3Szrj       iov[iov_index].iov_len = i_entry.size;
667*fae548d3Szrj       ++iov_index;
668*fae548d3Szrj 
669*fae548d3Szrj       want += i_entry.size;
670*fae548d3Szrj 
671*fae548d3Szrj       last_offset = i_entry.file_offset + i_entry.size;
672*fae548d3Szrj     }
673*fae548d3Szrj 
674*fae548d3Szrj   this->reopen_descriptor();
675*fae548d3Szrj 
676*fae548d3Szrj   gold_assert(iov_index < sizeof iov / sizeof iov[0]);
677*fae548d3Szrj 
678*fae548d3Szrj   if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
679*fae548d3Szrj     gold_fatal(_("%s: lseek failed: %s"),
680*fae548d3Szrj 	       this->filename().c_str(), strerror(errno));
681*fae548d3Szrj 
682*fae548d3Szrj   ssize_t got = ::readv(this->descriptor_, iov, iov_index);
683*fae548d3Szrj 
684*fae548d3Szrj   if (got < 0)
685*fae548d3Szrj     gold_fatal(_("%s: readv failed: %s"),
686*fae548d3Szrj 	       this->filename().c_str(), strerror(errno));
687*fae548d3Szrj   if (got != want)
688*fae548d3Szrj     gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
689*fae548d3Szrj 	       this->filename().c_str(),
690*fae548d3Szrj 	       got, want, static_cast<long long>(base + first_offset));
691*fae548d3Szrj }
692*fae548d3Szrj 
693*fae548d3Szrj // Portable IOV_MAX.
694*fae548d3Szrj 
695*fae548d3Szrj #if !defined(HAVE_READV)
696*fae548d3Szrj #define GOLD_IOV_MAX 1
697*fae548d3Szrj #elif defined(IOV_MAX)
698*fae548d3Szrj #define GOLD_IOV_MAX IOV_MAX
699*fae548d3Szrj #else
700*fae548d3Szrj #define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
701*fae548d3Szrj #endif
702*fae548d3Szrj 
703*fae548d3Szrj // Read several pieces of data from the file.
704*fae548d3Szrj 
705*fae548d3Szrj void
read_multiple(off_t base,const Read_multiple & rm)706*fae548d3Szrj File_read::read_multiple(off_t base, const Read_multiple& rm)
707*fae548d3Szrj {
708*fae548d3Szrj   static size_t iov_max = GOLD_IOV_MAX;
709*fae548d3Szrj   size_t count = rm.size();
710*fae548d3Szrj   size_t i = 0;
711*fae548d3Szrj   while (i < count)
712*fae548d3Szrj     {
713*fae548d3Szrj       // Find up to MAX_READV_ENTRIES consecutive entries which are
714*fae548d3Szrj       // less than one page apart.
715*fae548d3Szrj       const Read_multiple_entry& i_entry(rm[i]);
716*fae548d3Szrj       off_t i_off = i_entry.file_offset;
717*fae548d3Szrj       off_t end_off = i_off + i_entry.size;
718*fae548d3Szrj       size_t j;
719*fae548d3Szrj       for (j = i + 1; j < count; ++j)
720*fae548d3Szrj 	{
721*fae548d3Szrj 	  if (j - i >= File_read::max_readv_entries || j - i >= iov_max / 2)
722*fae548d3Szrj 	    break;
723*fae548d3Szrj 	  const Read_multiple_entry& j_entry(rm[j]);
724*fae548d3Szrj 	  off_t j_off = j_entry.file_offset;
725*fae548d3Szrj 	  gold_assert(j_off >= end_off);
726*fae548d3Szrj 	  off_t j_end_off = j_off + j_entry.size;
727*fae548d3Szrj 	  if (j_end_off - end_off >= File_read::page_size)
728*fae548d3Szrj 	    break;
729*fae548d3Szrj 	  end_off = j_end_off;
730*fae548d3Szrj 	}
731*fae548d3Szrj 
732*fae548d3Szrj       if (j == i + 1)
733*fae548d3Szrj 	this->read(base + i_off, i_entry.size, i_entry.buffer);
734*fae548d3Szrj       else
735*fae548d3Szrj 	{
736*fae548d3Szrj 	  File_read::View* view = this->find_view(base + i_off,
737*fae548d3Szrj 						  end_off - i_off,
738*fae548d3Szrj 						  -1U, NULL);
739*fae548d3Szrj 	  if (view == NULL)
740*fae548d3Szrj 	    this->do_readv(base, rm, i, j - i);
741*fae548d3Szrj 	  else
742*fae548d3Szrj 	    {
743*fae548d3Szrj 	      const unsigned char* v = (view->data()
744*fae548d3Szrj 					+ (base + i_off - view->start()
745*fae548d3Szrj 					   + view->byteshift()));
746*fae548d3Szrj 	      for (size_t k = i; k < j; ++k)
747*fae548d3Szrj 		{
748*fae548d3Szrj 		  const Read_multiple_entry& k_entry(rm[k]);
749*fae548d3Szrj 		  gold_assert((convert_to_section_size_type(k_entry.file_offset
750*fae548d3Szrj 							   - i_off)
751*fae548d3Szrj 			       + k_entry.size)
752*fae548d3Szrj 			      <= convert_to_section_size_type(end_off
753*fae548d3Szrj 							      - i_off));
754*fae548d3Szrj 		  memcpy(k_entry.buffer,
755*fae548d3Szrj 			 v + (k_entry.file_offset - i_off),
756*fae548d3Szrj 			 k_entry.size);
757*fae548d3Szrj 		}
758*fae548d3Szrj 	    }
759*fae548d3Szrj 	}
760*fae548d3Szrj 
761*fae548d3Szrj       i = j;
762*fae548d3Szrj     }
763*fae548d3Szrj }
764*fae548d3Szrj 
765*fae548d3Szrj // Mark all views as no longer cached.
766*fae548d3Szrj 
767*fae548d3Szrj void
clear_view_cache_marks()768*fae548d3Szrj File_read::clear_view_cache_marks()
769*fae548d3Szrj {
770*fae548d3Szrj   // Just ignore this if there are multiple objects associated with
771*fae548d3Szrj   // the file.  Otherwise we will wind up uncaching and freeing some
772*fae548d3Szrj   // views for other objects.
773*fae548d3Szrj   if (this->object_count_ > 1)
774*fae548d3Szrj     return;
775*fae548d3Szrj 
776*fae548d3Szrj   for (Views::iterator p = this->views_.begin();
777*fae548d3Szrj        p != this->views_.end();
778*fae548d3Szrj        ++p)
779*fae548d3Szrj     p->second->clear_cache();
780*fae548d3Szrj   for (Saved_views::iterator p = this->saved_views_.begin();
781*fae548d3Szrj        p != this->saved_views_.end();
782*fae548d3Szrj        ++p)
783*fae548d3Szrj     (*p)->clear_cache();
784*fae548d3Szrj }
785*fae548d3Szrj 
786*fae548d3Szrj // Remove all the file views.  For a file which has multiple
787*fae548d3Szrj // associated objects (i.e., an archive), we keep accessed views
788*fae548d3Szrj // around until next time, in the hopes that they will be useful for
789*fae548d3Szrj // the next object.
790*fae548d3Szrj 
791*fae548d3Szrj void
clear_views(Clear_views_mode mode)792*fae548d3Szrj File_read::clear_views(Clear_views_mode mode)
793*fae548d3Szrj {
794*fae548d3Szrj   bool keep_files_mapped = (parameters->options_valid()
795*fae548d3Szrj 			    && parameters->options().keep_files_mapped());
796*fae548d3Szrj   Views::iterator p = this->views_.begin();
797*fae548d3Szrj   while (p != this->views_.end())
798*fae548d3Szrj     {
799*fae548d3Szrj       bool should_delete;
800*fae548d3Szrj       if (p->second->is_locked() || p->second->is_permanent_view())
801*fae548d3Szrj 	should_delete = false;
802*fae548d3Szrj       else if (mode == CLEAR_VIEWS_ALL)
803*fae548d3Szrj 	should_delete = true;
804*fae548d3Szrj       else if ((p->second->should_cache()
805*fae548d3Szrj 		|| p->second == this->whole_file_view_)
806*fae548d3Szrj 	       && keep_files_mapped)
807*fae548d3Szrj 	should_delete = false;
808*fae548d3Szrj       else if (this->object_count_ > 1
809*fae548d3Szrj 	       && p->second->accessed()
810*fae548d3Szrj 	       && mode != CLEAR_VIEWS_ARCHIVE)
811*fae548d3Szrj 	should_delete = false;
812*fae548d3Szrj       else
813*fae548d3Szrj 	should_delete = true;
814*fae548d3Szrj 
815*fae548d3Szrj       if (should_delete)
816*fae548d3Szrj 	{
817*fae548d3Szrj 	  if (p->second == this->whole_file_view_)
818*fae548d3Szrj 	    this->whole_file_view_ = NULL;
819*fae548d3Szrj 	  delete p->second;
820*fae548d3Szrj 
821*fae548d3Szrj 	  // map::erase invalidates only the iterator to the deleted
822*fae548d3Szrj 	  // element.
823*fae548d3Szrj 	  Views::iterator pe = p;
824*fae548d3Szrj 	  ++p;
825*fae548d3Szrj 	  this->views_.erase(pe);
826*fae548d3Szrj 	}
827*fae548d3Szrj       else
828*fae548d3Szrj 	{
829*fae548d3Szrj 	  p->second->clear_accessed();
830*fae548d3Szrj 	  ++p;
831*fae548d3Szrj 	}
832*fae548d3Szrj     }
833*fae548d3Szrj 
834*fae548d3Szrj   Saved_views::iterator q = this->saved_views_.begin();
835*fae548d3Szrj   while (q != this->saved_views_.end())
836*fae548d3Szrj     {
837*fae548d3Szrj       if (!(*q)->is_locked())
838*fae548d3Szrj 	{
839*fae548d3Szrj 	  delete *q;
840*fae548d3Szrj 	  q = this->saved_views_.erase(q);
841*fae548d3Szrj 	}
842*fae548d3Szrj       else
843*fae548d3Szrj 	{
844*fae548d3Szrj 	  gold_assert(mode != CLEAR_VIEWS_ALL);
845*fae548d3Szrj 	  ++q;
846*fae548d3Szrj 	}
847*fae548d3Szrj     }
848*fae548d3Szrj }
849*fae548d3Szrj 
850*fae548d3Szrj // Print statistical information to stderr.  This is used for --stats.
851*fae548d3Szrj 
852*fae548d3Szrj void
print_stats()853*fae548d3Szrj File_read::print_stats()
854*fae548d3Szrj {
855*fae548d3Szrj   fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
856*fae548d3Szrj 	  program_name, File_read::total_mapped_bytes);
857*fae548d3Szrj   fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
858*fae548d3Szrj 	  program_name, File_read::maximum_mapped_bytes);
859*fae548d3Szrj }
860*fae548d3Szrj 
861*fae548d3Szrj // Class File_view.
862*fae548d3Szrj 
~File_view()863*fae548d3Szrj File_view::~File_view()
864*fae548d3Szrj {
865*fae548d3Szrj   gold_assert(this->file_.is_locked());
866*fae548d3Szrj   this->view_->unlock();
867*fae548d3Szrj }
868*fae548d3Szrj 
869*fae548d3Szrj // Class Input_file.
870*fae548d3Szrj 
871*fae548d3Szrj // Create a file given just the filename.
872*fae548d3Szrj 
Input_file(const char * name)873*fae548d3Szrj Input_file::Input_file(const char* name)
874*fae548d3Szrj   : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE)
875*fae548d3Szrj {
876*fae548d3Szrj   this->input_argument_ =
877*fae548d3Szrj     new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
878*fae548d3Szrj 			    "", false, Position_dependent_options());
879*fae548d3Szrj }
880*fae548d3Szrj 
881*fae548d3Szrj // Create a file for testing.
882*fae548d3Szrj 
Input_file(const Task * task,const char * name,const unsigned char * contents,off_t size)883*fae548d3Szrj Input_file::Input_file(const Task* task, const char* name,
884*fae548d3Szrj 		       const unsigned char* contents, off_t size)
885*fae548d3Szrj   : file_()
886*fae548d3Szrj {
887*fae548d3Szrj   this->input_argument_ =
888*fae548d3Szrj     new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
889*fae548d3Szrj 			    "", false, Position_dependent_options());
890*fae548d3Szrj   bool ok = this->file_.open(task, name, contents, size);
891*fae548d3Szrj   gold_assert(ok);
892*fae548d3Szrj }
893*fae548d3Szrj 
894*fae548d3Szrj // Return the position dependent options in force for this file.
895*fae548d3Szrj 
896*fae548d3Szrj const Position_dependent_options&
options() const897*fae548d3Szrj Input_file::options() const
898*fae548d3Szrj {
899*fae548d3Szrj   return this->input_argument_->options();
900*fae548d3Szrj }
901*fae548d3Szrj 
902*fae548d3Szrj // Return the name given by the user.  For -lc this will return "c".
903*fae548d3Szrj 
904*fae548d3Szrj const char*
name() const905*fae548d3Szrj Input_file::name() const
906*fae548d3Szrj {
907*fae548d3Szrj   return this->input_argument_->name();
908*fae548d3Szrj }
909*fae548d3Szrj 
910*fae548d3Szrj // Return whether this file is in a system directory.
911*fae548d3Szrj 
912*fae548d3Szrj bool
is_in_system_directory() const913*fae548d3Szrj Input_file::is_in_system_directory() const
914*fae548d3Szrj {
915*fae548d3Szrj   if (this->is_in_sysroot())
916*fae548d3Szrj     return true;
917*fae548d3Szrj   return parameters->options().is_in_system_directory(this->filename());
918*fae548d3Szrj }
919*fae548d3Szrj 
920*fae548d3Szrj // Return whether we are only reading symbols.
921*fae548d3Szrj 
922*fae548d3Szrj bool
just_symbols() const923*fae548d3Szrj Input_file::just_symbols() const
924*fae548d3Szrj {
925*fae548d3Szrj   return this->input_argument_->just_symbols();
926*fae548d3Szrj }
927*fae548d3Szrj 
928*fae548d3Szrj // Return whether this is a file that we will search for in the list
929*fae548d3Szrj // of directories.
930*fae548d3Szrj 
931*fae548d3Szrj bool
will_search_for() const932*fae548d3Szrj Input_file::will_search_for() const
933*fae548d3Szrj {
934*fae548d3Szrj   return (!IS_ABSOLUTE_PATH(this->input_argument_->name())
935*fae548d3Szrj 	  && (this->input_argument_->is_lib()
936*fae548d3Szrj 	      || this->input_argument_->is_searched_file()
937*fae548d3Szrj 	      || this->input_argument_->extra_search_path() != NULL));
938*fae548d3Szrj }
939*fae548d3Szrj 
940*fae548d3Szrj // Return the file last modification time.  Calls gold_fatal if the stat
941*fae548d3Szrj // system call failed.
942*fae548d3Szrj 
943*fae548d3Szrj Timespec
get_mtime()944*fae548d3Szrj File_read::get_mtime()
945*fae548d3Szrj {
946*fae548d3Szrj   struct stat file_stat;
947*fae548d3Szrj   this->reopen_descriptor();
948*fae548d3Szrj 
949*fae548d3Szrj   if (fstat(this->descriptor_, &file_stat) < 0)
950*fae548d3Szrj     gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(),
951*fae548d3Szrj 	       strerror(errno));
952*fae548d3Szrj #ifdef HAVE_STAT_ST_MTIM
953*fae548d3Szrj   return Timespec(file_stat.st_mtim.tv_sec, file_stat.st_mtim.tv_nsec);
954*fae548d3Szrj #else
955*fae548d3Szrj   return Timespec(file_stat.st_mtime, 0);
956*fae548d3Szrj #endif
957*fae548d3Szrj }
958*fae548d3Szrj 
959*fae548d3Szrj // Try to find a file in the extra search dirs.  Returns true on success.
960*fae548d3Szrj 
961*fae548d3Szrj bool
try_extra_search_path(int * pindex,const Input_file_argument * input_argument,std::string filename,std::string * found_name,std::string * namep)962*fae548d3Szrj Input_file::try_extra_search_path(int* pindex,
963*fae548d3Szrj 				  const Input_file_argument* input_argument,
964*fae548d3Szrj 				  std::string filename, std::string* found_name,
965*fae548d3Szrj 				  std::string* namep)
966*fae548d3Szrj {
967*fae548d3Szrj   if (input_argument->extra_search_path() == NULL)
968*fae548d3Szrj     return false;
969*fae548d3Szrj 
970*fae548d3Szrj   std::string name = input_argument->extra_search_path();
971*fae548d3Szrj   if (!IS_DIR_SEPARATOR(name[name.length() - 1]))
972*fae548d3Szrj     name += '/';
973*fae548d3Szrj   name += filename;
974*fae548d3Szrj 
975*fae548d3Szrj   struct stat dummy_stat;
976*fae548d3Szrj   if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
977*fae548d3Szrj     return false;
978*fae548d3Szrj 
979*fae548d3Szrj   *found_name = filename;
980*fae548d3Szrj   *namep = name;
981*fae548d3Szrj   return true;
982*fae548d3Szrj }
983*fae548d3Szrj 
984*fae548d3Szrj // Find the actual file.
985*fae548d3Szrj // If the filename is not absolute, we assume it is in the current
986*fae548d3Szrj // directory *except* when:
987*fae548d3Szrj //    A) input_argument_->is_lib() is true;
988*fae548d3Szrj //    B) input_argument_->is_searched_file() is true; or
989*fae548d3Szrj //    C) input_argument_->extra_search_path() is not empty.
990*fae548d3Szrj // In each, we look in extra_search_path + library_path to find
991*fae548d3Szrj // the file location, rather than the current directory.
992*fae548d3Szrj 
993*fae548d3Szrj bool
find_file(const Dirsearch & dirpath,int * pindex,const Input_file_argument * input_argument,bool * is_in_sysroot,std::string * found_name,std::string * namep)994*fae548d3Szrj Input_file::find_file(const Dirsearch& dirpath, int* pindex,
995*fae548d3Szrj 		      const Input_file_argument* input_argument,
996*fae548d3Szrj 		      bool* is_in_sysroot,
997*fae548d3Szrj 		      std::string* found_name, std::string* namep)
998*fae548d3Szrj {
999*fae548d3Szrj   std::string name;
1000*fae548d3Szrj 
1001*fae548d3Szrj   // Case 1: name is an absolute file, just try to open it
1002*fae548d3Szrj   // Case 2: name is relative but is_lib is false, is_searched_file is false,
1003*fae548d3Szrj   //         and extra_search_path is empty
1004*fae548d3Szrj   if (IS_ABSOLUTE_PATH(input_argument->name())
1005*fae548d3Szrj       || (!input_argument->is_lib()
1006*fae548d3Szrj 	  && !input_argument->is_searched_file()
1007*fae548d3Szrj 	  && input_argument->extra_search_path() == NULL))
1008*fae548d3Szrj     {
1009*fae548d3Szrj       name = input_argument->name();
1010*fae548d3Szrj       *found_name = name;
1011*fae548d3Szrj       *namep = name;
1012*fae548d3Szrj       return true;
1013*fae548d3Szrj     }
1014*fae548d3Szrj   // Case 3: is_lib is true or is_searched_file is true
1015*fae548d3Szrj   else if (input_argument->is_lib()
1016*fae548d3Szrj 	   || input_argument->is_searched_file())
1017*fae548d3Szrj     {
1018*fae548d3Szrj       std::vector<std::string> names;
1019*fae548d3Szrj       names.reserve(2);
1020*fae548d3Szrj       if (input_argument->is_lib())
1021*fae548d3Szrj 	{
1022*fae548d3Szrj 	  std::string prefix = "lib";
1023*fae548d3Szrj 	  prefix += input_argument->name();
1024*fae548d3Szrj 	  if (parameters->options().is_static()
1025*fae548d3Szrj 	      || !input_argument->options().Bdynamic())
1026*fae548d3Szrj 	    names.push_back(prefix + ".a");
1027*fae548d3Szrj 	  else
1028*fae548d3Szrj 	    {
1029*fae548d3Szrj 	      names.push_back(prefix + ".so");
1030*fae548d3Szrj 	      names.push_back(prefix + ".a");
1031*fae548d3Szrj 	    }
1032*fae548d3Szrj 	}
1033*fae548d3Szrj       else
1034*fae548d3Szrj 	names.push_back(input_argument->name());
1035*fae548d3Szrj 
1036*fae548d3Szrj       for (std::vector<std::string>::const_iterator n = names.begin();
1037*fae548d3Szrj 	   n != names.end();
1038*fae548d3Szrj 	   ++n)
1039*fae548d3Szrj 	if (Input_file::try_extra_search_path(pindex, input_argument, *n,
1040*fae548d3Szrj 					      found_name, namep))
1041*fae548d3Szrj 	  return true;
1042*fae548d3Szrj 
1043*fae548d3Szrj       // It is not in the extra_search_path.
1044*fae548d3Szrj       name = dirpath.find(names, is_in_sysroot, pindex, found_name);
1045*fae548d3Szrj       if (name.empty())
1046*fae548d3Szrj 	{
1047*fae548d3Szrj 	  gold_error(_("cannot find %s%s"),
1048*fae548d3Szrj 		     input_argument->is_lib() ? "-l" : "",
1049*fae548d3Szrj 		     input_argument->name());
1050*fae548d3Szrj 	  return false;
1051*fae548d3Szrj 	}
1052*fae548d3Szrj       *namep = name;
1053*fae548d3Szrj       return true;
1054*fae548d3Szrj     }
1055*fae548d3Szrj   // Case 4: extra_search_path is not empty
1056*fae548d3Szrj   else
1057*fae548d3Szrj     {
1058*fae548d3Szrj       gold_assert(input_argument->extra_search_path() != NULL);
1059*fae548d3Szrj 
1060*fae548d3Szrj       if (try_extra_search_path(pindex, input_argument, input_argument->name(),
1061*fae548d3Szrj 				found_name, namep))
1062*fae548d3Szrj 	return true;
1063*fae548d3Szrj 
1064*fae548d3Szrj       // extra_search_path failed, so check the normal search-path.
1065*fae548d3Szrj       int index = *pindex;
1066*fae548d3Szrj       if (index > 0)
1067*fae548d3Szrj 	--index;
1068*fae548d3Szrj       name = dirpath.find(std::vector<std::string>(1, input_argument->name()),
1069*fae548d3Szrj 			  is_in_sysroot, &index, found_name);
1070*fae548d3Szrj       if (name.empty())
1071*fae548d3Szrj 	{
1072*fae548d3Szrj 	  gold_error(_("cannot find %s"),
1073*fae548d3Szrj 		     input_argument->name());
1074*fae548d3Szrj 	  return false;
1075*fae548d3Szrj 	}
1076*fae548d3Szrj       *namep = name;
1077*fae548d3Szrj       *pindex = index + 1;
1078*fae548d3Szrj       return true;
1079*fae548d3Szrj     }
1080*fae548d3Szrj }
1081*fae548d3Szrj 
1082*fae548d3Szrj // Open the file.
1083*fae548d3Szrj 
1084*fae548d3Szrj bool
open(const Dirsearch & dirpath,const Task * task,int * pindex)1085*fae548d3Szrj Input_file::open(const Dirsearch& dirpath, const Task* task, int* pindex)
1086*fae548d3Szrj {
1087*fae548d3Szrj   std::string name;
1088*fae548d3Szrj   if (!Input_file::find_file(dirpath, pindex, this->input_argument_,
1089*fae548d3Szrj 			     &this->is_in_sysroot_, &this->found_name_, &name))
1090*fae548d3Szrj     return false;
1091*fae548d3Szrj 
1092*fae548d3Szrj   // Now that we've figured out where the file lives, try to open it.
1093*fae548d3Szrj 
1094*fae548d3Szrj   General_options::Object_format format =
1095*fae548d3Szrj     this->input_argument_->options().format_enum();
1096*fae548d3Szrj   bool ok;
1097*fae548d3Szrj   if (format == General_options::OBJECT_FORMAT_ELF)
1098*fae548d3Szrj     {
1099*fae548d3Szrj       ok = this->file_.open(task, name);
1100*fae548d3Szrj       this->format_ = FORMAT_ELF;
1101*fae548d3Szrj     }
1102*fae548d3Szrj   else
1103*fae548d3Szrj     {
1104*fae548d3Szrj       gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
1105*fae548d3Szrj       ok = this->open_binary(task, name);
1106*fae548d3Szrj       this->format_ = FORMAT_BINARY;
1107*fae548d3Szrj     }
1108*fae548d3Szrj 
1109*fae548d3Szrj   if (!ok)
1110*fae548d3Szrj     {
1111*fae548d3Szrj       gold_error(_("cannot open %s: %s"),
1112*fae548d3Szrj 		 name.c_str(), strerror(errno));
1113*fae548d3Szrj       this->format_ = FORMAT_NONE;
1114*fae548d3Szrj       return false;
1115*fae548d3Szrj     }
1116*fae548d3Szrj 
1117*fae548d3Szrj   return true;
1118*fae548d3Szrj }
1119*fae548d3Szrj 
1120*fae548d3Szrj // Open a file for --format binary.
1121*fae548d3Szrj 
1122*fae548d3Szrj bool
open_binary(const Task * task,const std::string & name)1123*fae548d3Szrj Input_file::open_binary(const Task* task, const std::string& name)
1124*fae548d3Szrj {
1125*fae548d3Szrj   // In order to open a binary file, we need machine code, size, and
1126*fae548d3Szrj   // endianness.  We may not have a valid target at this point, in
1127*fae548d3Szrj   // which case we use the default target.
1128*fae548d3Szrj   parameters_force_valid_target();
1129*fae548d3Szrj   const Target& target(parameters->target());
1130*fae548d3Szrj 
1131*fae548d3Szrj   Binary_to_elf binary_to_elf(target.machine_code(),
1132*fae548d3Szrj 			      target.get_size(),
1133*fae548d3Szrj 			      target.is_big_endian(),
1134*fae548d3Szrj 			      name);
1135*fae548d3Szrj   if (!binary_to_elf.convert(task))
1136*fae548d3Szrj     return false;
1137*fae548d3Szrj   return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
1138*fae548d3Szrj 			  binary_to_elf.converted_size());
1139*fae548d3Szrj }
1140*fae548d3Szrj 
1141*fae548d3Szrj } // End namespace gold.
1142