|
|
ee698d |
diff -rupN --no-dereference binutils-2.38/gold/fileread.cc binutils-2.38-new/gold/fileread.cc
|
|
|
ee698d |
--- binutils-2.38/gold/fileread.cc 2022-01-22 13:14:09.000000000 +0100
|
|
|
ee698d |
+++ binutils-2.38-new/gold/fileread.cc 2022-04-26 13:54:56.336537927 +0200
|
|
|
0c89c8 |
@@ -385,6 +385,12 @@ File_read::do_read(off_t start, section_
|
|
|
0c89c8 |
ssize_t bytes;
|
|
|
0c89c8 |
if (this->whole_file_view_ != NULL)
|
|
|
0c89c8 |
{
|
|
|
0c89c8 |
+ // See PR 23765 for an example of a testcase that triggers this error.
|
|
|
0c89c8 |
+ if (((ssize_t) start) < 0)
|
|
|
0c89c8 |
+ gold_fatal(_("%s: read failed, starting offset (%#llx) less than zero"),
|
|
|
0c89c8 |
+ this->filename().c_str(),
|
|
|
0c89c8 |
+ static_cast<long long>(start));
|
|
|
0c89c8 |
+
|
|
|
0c89c8 |
bytes = this->size_ - start;
|
|
|
0c89c8 |
if (static_cast<section_size_type>(bytes) >= size)
|
|
|
0c89c8 |
{
|
|
|
ee698d |
diff -rupN --no-dereference binutils-2.38/gold/fileread.cc.orig binutils-2.38-new/gold/fileread.cc.orig
|
|
|
ee698d |
--- binutils-2.38/gold/fileread.cc.orig 1970-01-01 01:00:00.000000000 +0100
|
|
|
ee698d |
+++ binutils-2.38-new/gold/fileread.cc.orig 2022-01-22 13:14:09.000000000 +0100
|
|
|
ee698d |
@@ -0,0 +1,1172 @@
|
|
|
ee698d |
+// fileread.cc -- read files for gold
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Copyright (C) 2006-2022 Free Software Foundation, Inc.
|
|
|
ee698d |
+// Written by Ian Lance Taylor <iant@google.com>.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// This file is part of gold.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// This program is free software; you can redistribute it and/or modify
|
|
|
ee698d |
+// it under the terms of the GNU General Public License as published by
|
|
|
ee698d |
+// the Free Software Foundation; either version 3 of the License, or
|
|
|
ee698d |
+// (at your option) any later version.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// This program is distributed in the hope that it will be useful,
|
|
|
ee698d |
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
ee698d |
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
ee698d |
+// GNU General Public License for more details.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// You should have received a copy of the GNU General Public License
|
|
|
ee698d |
+// along with this program; if not, write to the Free Software
|
|
|
ee698d |
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
ee698d |
+// MA 02110-1301, USA.
|
|
|
ee698d |
+
|
|
|
ee698d |
+#include "gold.h"
|
|
|
ee698d |
+
|
|
|
ee698d |
+#include <cstring>
|
|
|
ee698d |
+#include <cerrno>
|
|
|
ee698d |
+#include <climits>
|
|
|
ee698d |
+#include <fcntl.h>
|
|
|
ee698d |
+#include <unistd.h>
|
|
|
ee698d |
+
|
|
|
ee698d |
+#ifdef HAVE_SYS_MMAN_H
|
|
|
ee698d |
+#include <sys/mman.h>
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+#ifdef HAVE_READV
|
|
|
ee698d |
+#include <sys/uio.h>
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+#include <sys/stat.h>
|
|
|
ee698d |
+#include "filenames.h"
|
|
|
ee698d |
+
|
|
|
ee698d |
+#include "debug.h"
|
|
|
ee698d |
+#include "parameters.h"
|
|
|
ee698d |
+#include "options.h"
|
|
|
ee698d |
+#include "dirsearch.h"
|
|
|
ee698d |
+#include "target.h"
|
|
|
ee698d |
+#include "binary.h"
|
|
|
ee698d |
+#include "descriptors.h"
|
|
|
ee698d |
+#include "gold-threads.h"
|
|
|
ee698d |
+#include "fileread.h"
|
|
|
ee698d |
+
|
|
|
ee698d |
+// For systems without mmap support.
|
|
|
ee698d |
+#ifndef HAVE_MMAP
|
|
|
ee698d |
+# define mmap gold_mmap
|
|
|
ee698d |
+# define munmap gold_munmap
|
|
|
ee698d |
+# ifndef MAP_FAILED
|
|
|
ee698d |
+# define MAP_FAILED (reinterpret_cast<void*>(-1))
|
|
|
ee698d |
+# endif
|
|
|
ee698d |
+# ifndef PROT_READ
|
|
|
ee698d |
+# define PROT_READ 0
|
|
|
ee698d |
+# endif
|
|
|
ee698d |
+# ifndef MAP_PRIVATE
|
|
|
ee698d |
+# define MAP_PRIVATE 0
|
|
|
ee698d |
+# endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+# ifndef ENOSYS
|
|
|
ee698d |
+# define ENOSYS EINVAL
|
|
|
ee698d |
+# endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+static void *
|
|
|
ee698d |
+gold_mmap(void *, size_t, int, int, int, off_t)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ errno = ENOSYS;
|
|
|
ee698d |
+ return MAP_FAILED;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+static int
|
|
|
ee698d |
+gold_munmap(void *, size_t)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ errno = ENOSYS;
|
|
|
ee698d |
+ return -1;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+#ifndef HAVE_READV
|
|
|
ee698d |
+struct iovec { void* iov_base; size_t iov_len; };
|
|
|
ee698d |
+ssize_t
|
|
|
ee698d |
+readv(int, const iovec*, int)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_unreachable();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+namespace gold
|
|
|
ee698d |
+{
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Get the last modified time of an unopened file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+get_mtime(const char* filename, Timespec* mtime)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ struct stat file_stat;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (stat(filename, &file_stat) < 0)
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+#ifdef HAVE_STAT_ST_MTIM
|
|
|
ee698d |
+ mtime->seconds = file_stat.st_mtim.tv_sec;
|
|
|
ee698d |
+ mtime->nanoseconds = file_stat.st_mtim.tv_nsec;
|
|
|
ee698d |
+#else
|
|
|
ee698d |
+ mtime->seconds = file_stat.st_mtime;
|
|
|
ee698d |
+ mtime->nanoseconds = 0;
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Class File_read.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// A lock for the File_read static variables.
|
|
|
ee698d |
+static Lock* file_counts_lock = NULL;
|
|
|
ee698d |
+static Initialize_lock file_counts_initialize_lock(&file_counts_lock);
|
|
|
ee698d |
+
|
|
|
ee698d |
+// The File_read static variables.
|
|
|
ee698d |
+unsigned long long File_read::total_mapped_bytes;
|
|
|
ee698d |
+unsigned long long File_read::current_mapped_bytes;
|
|
|
ee698d |
+unsigned long long File_read::maximum_mapped_bytes;
|
|
|
ee698d |
+std::vector<std::string> File_read::files_read;
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Class File_read::View.
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_read::View::~View()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(!this->is_locked());
|
|
|
ee698d |
+ switch (this->data_ownership_)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ case DATA_ALLOCATED_ARRAY:
|
|
|
ee698d |
+ free(const_cast<unsigned char*>(this->data_));
|
|
|
ee698d |
+ break;
|
|
|
ee698d |
+ case DATA_MMAPPED:
|
|
|
ee698d |
+ if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
|
|
|
ee698d |
+ gold_warning(_("munmap failed: %s"), strerror(errno));
|
|
|
ee698d |
+ if (!parameters->options_valid() || parameters->options().stats())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ file_counts_initialize_lock.initialize();
|
|
|
ee698d |
+ Hold_optional_lock hl(file_counts_lock);
|
|
|
ee698d |
+ File_read::current_mapped_bytes -= this->size_;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ break;
|
|
|
ee698d |
+ case DATA_NOT_OWNED:
|
|
|
ee698d |
+ break;
|
|
|
ee698d |
+ default:
|
|
|
ee698d |
+ gold_unreachable();
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::View::lock()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ ++this->lock_count_;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::View::unlock()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->lock_count_ > 0);
|
|
|
ee698d |
+ --this->lock_count_;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+File_read::View::is_locked()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ return this->lock_count_ > 0;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Class File_read.
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_read::~File_read()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->token_.is_writable());
|
|
|
ee698d |
+ if (this->is_descriptor_opened_)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ release_descriptor(this->descriptor_, true);
|
|
|
ee698d |
+ this->descriptor_ = -1;
|
|
|
ee698d |
+ this->is_descriptor_opened_ = false;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ this->name_.clear();
|
|
|
ee698d |
+ this->clear_views(CLEAR_VIEWS_ALL);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Open the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+File_read::open(const Task* task, const std::string& name)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->token_.is_writable()
|
|
|
ee698d |
+ && this->descriptor_ < 0
|
|
|
ee698d |
+ && !this->is_descriptor_opened_
|
|
|
ee698d |
+ && this->name_.empty());
|
|
|
ee698d |
+ this->name_ = name;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->descriptor_ = open_descriptor(-1, this->name_.c_str(),
|
|
|
ee698d |
+ O_RDONLY);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (this->descriptor_ >= 0)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ this->is_descriptor_opened_ = true;
|
|
|
ee698d |
+ struct stat s;
|
|
|
ee698d |
+ if (::fstat(this->descriptor_, &s) < 0)
|
|
|
ee698d |
+ gold_error(_("%s: fstat failed: %s"),
|
|
|
ee698d |
+ this->name_.c_str(), strerror(errno));
|
|
|
ee698d |
+ this->size_ = s.st_size;
|
|
|
ee698d |
+ gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
|
|
|
ee698d |
+ this->name_.c_str());
|
|
|
ee698d |
+ this->token_.add_writer(task);
|
|
|
ee698d |
+ file_counts_initialize_lock.initialize();
|
|
|
ee698d |
+ Hold_optional_lock hl(file_counts_lock);
|
|
|
ee698d |
+ record_file_read(this->name_);
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ return this->descriptor_ >= 0;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Open the file with the contents in memory.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+File_read::open(const Task* task, const std::string& name,
|
|
|
ee698d |
+ const unsigned char* contents, off_t size)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->token_.is_writable()
|
|
|
ee698d |
+ && this->descriptor_ < 0
|
|
|
ee698d |
+ && !this->is_descriptor_opened_
|
|
|
ee698d |
+ && this->name_.empty());
|
|
|
ee698d |
+ this->name_ = name;
|
|
|
ee698d |
+ this->whole_file_view_ = new View(0, size, contents, 0, false,
|
|
|
ee698d |
+ View::DATA_NOT_OWNED);
|
|
|
ee698d |
+ this->add_view(this->whole_file_view_);
|
|
|
ee698d |
+ this->size_ = size;
|
|
|
ee698d |
+ this->token_.add_writer(task);
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Reopen a descriptor if necessary.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::reopen_descriptor()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ if (!this->is_descriptor_opened_)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ this->descriptor_ = open_descriptor(this->descriptor_,
|
|
|
ee698d |
+ this->name_.c_str(),
|
|
|
ee698d |
+ O_RDONLY);
|
|
|
ee698d |
+ if (this->descriptor_ < 0)
|
|
|
ee698d |
+ gold_fatal(_("could not reopen file %s"), this->name_.c_str());
|
|
|
ee698d |
+ this->is_descriptor_opened_ = true;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Release the file. This is called when we are done with the file in
|
|
|
ee698d |
+// a Task.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::release()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->is_locked());
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (!parameters->options_valid() || parameters->options().stats())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ file_counts_initialize_lock.initialize();
|
|
|
ee698d |
+ Hold_optional_lock hl(file_counts_lock);
|
|
|
ee698d |
+ File_read::total_mapped_bytes += this->mapped_bytes_;
|
|
|
ee698d |
+ File_read::current_mapped_bytes += this->mapped_bytes_;
|
|
|
ee698d |
+ if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
|
|
|
ee698d |
+ File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->mapped_bytes_ = 0;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Only clear views if there is only one attached object. Otherwise
|
|
|
ee698d |
+ // we waste time trying to clear cached archive views. Similarly
|
|
|
ee698d |
+ // for releasing the descriptor.
|
|
|
ee698d |
+ if (this->object_count_ <= 1)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ this->clear_views(CLEAR_VIEWS_NORMAL);
|
|
|
ee698d |
+ if (this->is_descriptor_opened_)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ release_descriptor(this->descriptor_, false);
|
|
|
ee698d |
+ this->is_descriptor_opened_ = false;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->released_ = true;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Lock the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::lock(const Task* task)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->released_);
|
|
|
ee698d |
+ gold_debug(DEBUG_FILES, "Locking file \"%s\"", this->name_.c_str());
|
|
|
ee698d |
+ this->token_.add_writer(task);
|
|
|
ee698d |
+ this->released_ = false;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Unlock the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::unlock(const Task* task)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_debug(DEBUG_FILES, "Unlocking file \"%s\"", this->name_.c_str());
|
|
|
ee698d |
+ this->release();
|
|
|
ee698d |
+ this->token_.remove_writer(task);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return whether the file is locked.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+File_read::is_locked() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ if (!this->token_.is_writable())
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+ // The file is not locked, so it should have been released.
|
|
|
ee698d |
+ gold_assert(this->released_);
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// See if we have a view which covers the file starting at START for
|
|
|
ee698d |
+// SIZE bytes. Return a pointer to the View if found, NULL if not.
|
|
|
ee698d |
+// If BYTESHIFT is not -1U, the returned View must have the specified
|
|
|
ee698d |
+// byte shift; otherwise, it may have any byte shift. If VSHIFTED is
|
|
|
ee698d |
+// not NULL, this sets *VSHIFTED to a view which would have worked if
|
|
|
ee698d |
+// not for the requested BYTESHIFT.
|
|
|
ee698d |
+
|
|
|
ee698d |
+inline File_read::View*
|
|
|
ee698d |
+File_read::find_view(off_t start, section_size_type size,
|
|
|
ee698d |
+ unsigned int byteshift, File_read::View** vshifted) const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(start <= this->size_
|
|
|
ee698d |
+ && (static_cast<unsigned long long>(size)
|
|
|
ee698d |
+ <= static_cast<unsigned long long>(this->size_ - start)));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (vshifted != NULL)
|
|
|
ee698d |
+ *vshifted = NULL;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // If we have the whole file mmapped, and the alignment is right,
|
|
|
ee698d |
+ // we can return it.
|
|
|
ee698d |
+ if (this->whole_file_view_)
|
|
|
ee698d |
+ if (byteshift == -1U || byteshift == 0)
|
|
|
ee698d |
+ return this->whole_file_view_;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ off_t page = File_read::page_offset(start);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ unsigned int bszero = 0;
|
|
|
ee698d |
+ Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1,
|
|
|
ee698d |
+ bszero));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ while (p != this->views_.end() && p->first.first <= page)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (p->second->start() <= start
|
|
|
ee698d |
+ && (p->second->start() + static_cast<off_t>(p->second->size())
|
|
|
ee698d |
+ >= start + static_cast<off_t>(size)))
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (byteshift == -1U || byteshift == p->second->byteshift())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ p->second->set_accessed();
|
|
|
ee698d |
+ return p->second;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (vshifted != NULL && *vshifted == NULL)
|
|
|
ee698d |
+ *vshifted = p->second;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ ++p;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ return NULL;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Read SIZE bytes from the file starting at offset START. Read into
|
|
|
ee698d |
+// the buffer at P.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::do_read(off_t start, section_size_type size, void* p)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ ssize_t bytes;
|
|
|
ee698d |
+ if (this->whole_file_view_ != NULL)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ bytes = this->size_ - start;
|
|
|
ee698d |
+ if (static_cast<section_size_type>(bytes) >= size)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ memcpy(p, this->whole_file_view_->data() + start, size);
|
|
|
ee698d |
+ return;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ this->reopen_descriptor();
|
|
|
ee698d |
+
|
|
|
ee698d |
+ char *read_ptr = static_cast<char *>(p);
|
|
|
ee698d |
+ off_t read_pos = start;
|
|
|
ee698d |
+ size_t to_read = size;
|
|
|
ee698d |
+ do
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ bytes = ::pread(this->descriptor_, read_ptr, to_read, read_pos);
|
|
|
ee698d |
+ if (bytes < 0)
|
|
|
ee698d |
+ gold_fatal(_("%s: pread failed: %s"),
|
|
|
ee698d |
+ this->filename().c_str(), strerror(errno));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ read_pos += bytes;
|
|
|
ee698d |
+ read_ptr += bytes;
|
|
|
ee698d |
+ to_read -= bytes;
|
|
|
ee698d |
+ if (to_read == 0)
|
|
|
ee698d |
+ return;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ while (bytes > 0);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ bytes = size - to_read;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
|
|
|
ee698d |
+ this->filename().c_str(),
|
|
|
ee698d |
+ static_cast<long long>(bytes),
|
|
|
ee698d |
+ static_cast<long long>(size),
|
|
|
ee698d |
+ static_cast<long long>(start));
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Read data from the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::read(off_t start, section_size_type size, void* p)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ const File_read::View* pv = this->find_view(start, size, -1U, NULL);
|
|
|
ee698d |
+ if (pv != NULL)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
|
|
|
ee698d |
+ return;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->do_read(start, size, p);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Add a new view. There may already be an existing view at this
|
|
|
ee698d |
+// offset. If there is, the new view will be larger, and should
|
|
|
ee698d |
+// replace the old view.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::add_view(File_read::View* v)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ std::pair<Views::iterator, bool> ins =
|
|
|
ee698d |
+ this->views_.insert(std::make_pair(std::make_pair(v->start(),
|
|
|
ee698d |
+ v->byteshift()),
|
|
|
ee698d |
+ v));
|
|
|
ee698d |
+ if (ins.second)
|
|
|
ee698d |
+ return;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // There was an existing view at this offset. It must not be large
|
|
|
ee698d |
+ // enough. We can't delete it here, since something might be using
|
|
|
ee698d |
+ // it; we put it on a list to be deleted when the file is unlocked.
|
|
|
ee698d |
+ File_read::View* vold = ins.first->second;
|
|
|
ee698d |
+ gold_assert(vold->size() < v->size());
|
|
|
ee698d |
+ if (vold->should_cache())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ v->set_cache();
|
|
|
ee698d |
+ vold->clear_cache();
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ this->saved_views_.push_back(vold);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ ins.first->second = v;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Make a new view with a specified byteshift, reading the data from
|
|
|
ee698d |
+// the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_read::View*
|
|
|
ee698d |
+File_read::make_view(off_t start, section_size_type size,
|
|
|
ee698d |
+ unsigned int byteshift, bool cache)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(size > 0);
|
|
|
ee698d |
+ gold_assert(start <= this->size_
|
|
|
ee698d |
+ && (static_cast<unsigned long long>(size)
|
|
|
ee698d |
+ <= static_cast<unsigned long long>(this->size_ - start)));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ off_t poff = File_read::page_offset(start);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ section_size_type psize = File_read::pages(size + (start - poff));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (poff + static_cast<off_t>(psize) >= this->size_)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ psize = this->size_ - poff;
|
|
|
ee698d |
+ gold_assert(psize >= size);
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ void* p;
|
|
|
ee698d |
+ View::Data_ownership ownership;
|
|
|
ee698d |
+ if (byteshift != 0)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ p = malloc(psize + byteshift);
|
|
|
ee698d |
+ if (p == NULL)
|
|
|
ee698d |
+ gold_nomem();
|
|
|
ee698d |
+ memset(p, 0, byteshift);
|
|
|
ee698d |
+ this->do_read(poff, psize, static_cast<unsigned char*>(p) + byteshift);
|
|
|
ee698d |
+ ownership = View::DATA_ALLOCATED_ARRAY;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ this->reopen_descriptor();
|
|
|
ee698d |
+ p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE, this->descriptor_, poff);
|
|
|
ee698d |
+ if (p != MAP_FAILED)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ ownership = View::DATA_MMAPPED;
|
|
|
ee698d |
+ this->mapped_bytes_ += psize;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ p = malloc(psize);
|
|
|
ee698d |
+ if (p == NULL)
|
|
|
ee698d |
+ gold_nomem();
|
|
|
ee698d |
+ this->do_read(poff, psize, p);
|
|
|
ee698d |
+ ownership = View::DATA_ALLOCATED_ARRAY;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ const unsigned char* pbytes = static_cast<const unsigned char*>(p);
|
|
|
ee698d |
+ File_read::View* v = new File_read::View(poff, psize, pbytes, byteshift,
|
|
|
ee698d |
+ cache, ownership);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->add_view(v);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ return v;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Find a View or make a new one, shifted as required by the file
|
|
|
ee698d |
+// offset OFFSET and ALIGNED.
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_read::View*
|
|
|
ee698d |
+File_read::find_or_make_view(off_t offset, off_t start,
|
|
|
ee698d |
+ section_size_type size, bool aligned, bool cache)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ // Check that start and end of the view are within the file.
|
|
|
ee698d |
+ if (start > this->size_
|
|
|
ee698d |
+ || (static_cast<unsigned long long>(size)
|
|
|
ee698d |
+ > static_cast<unsigned long long>(this->size_ - start)))
|
|
|
ee698d |
+ gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
|
|
|
ee698d |
+ "size of file; the file may be corrupt"),
|
|
|
ee698d |
+ this->filename().c_str(),
|
|
|
ee698d |
+ static_cast<long long>(size),
|
|
|
ee698d |
+ static_cast<long long>(start));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ unsigned int byteshift;
|
|
|
ee698d |
+ if (offset == 0)
|
|
|
ee698d |
+ byteshift = 0;
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ unsigned int target_size = (!parameters->target_valid()
|
|
|
ee698d |
+ ? 64
|
|
|
ee698d |
+ : parameters->target().get_size());
|
|
|
ee698d |
+ byteshift = offset & ((target_size / 8) - 1);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Set BYTESHIFT to the number of dummy bytes which must be
|
|
|
ee698d |
+ // inserted before the data in order for this data to be
|
|
|
ee698d |
+ // aligned.
|
|
|
ee698d |
+ if (byteshift != 0)
|
|
|
ee698d |
+ byteshift = (target_size / 8) - byteshift;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // If --map-whole-files is set, make sure we have a
|
|
|
ee698d |
+ // whole file view. Options may not yet be ready, e.g.,
|
|
|
ee698d |
+ // when reading a version script. We then default to
|
|
|
ee698d |
+ // --no-map-whole-files.
|
|
|
ee698d |
+ if (this->whole_file_view_ == NULL
|
|
|
ee698d |
+ && parameters->options_valid()
|
|
|
ee698d |
+ && parameters->options().map_whole_files())
|
|
|
ee698d |
+ this->whole_file_view_ = this->make_view(0, this->size_, 0, cache);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Try to find a View with the required BYTESHIFT.
|
|
|
ee698d |
+ File_read::View* vshifted;
|
|
|
ee698d |
+ File_read::View* v = this->find_view(offset + start, size,
|
|
|
ee698d |
+ aligned ? byteshift : -1U,
|
|
|
ee698d |
+ &vshifted);
|
|
|
ee698d |
+ if (v != NULL)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (cache)
|
|
|
ee698d |
+ v->set_cache();
|
|
|
ee698d |
+ return v;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // If VSHIFTED is not NULL, then it has the data we need, but with
|
|
|
ee698d |
+ // the wrong byteshift.
|
|
|
ee698d |
+ v = vshifted;
|
|
|
ee698d |
+ if (v != NULL)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_assert(aligned);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ unsigned char* pbytes;
|
|
|
ee698d |
+ pbytes = static_cast<unsigned char*>(malloc(v->size() + byteshift));
|
|
|
ee698d |
+ if (pbytes == NULL)
|
|
|
ee698d |
+ gold_nomem();
|
|
|
ee698d |
+ memset(pbytes, 0, byteshift);
|
|
|
ee698d |
+ memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
|
|
|
ee698d |
+
|
|
|
ee698d |
+ File_read::View* shifted_view =
|
|
|
ee698d |
+ new File_read::View(v->start(), v->size(), pbytes, byteshift,
|
|
|
ee698d |
+ cache, View::DATA_ALLOCATED_ARRAY);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->add_view(shifted_view);
|
|
|
ee698d |
+ return shifted_view;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Make a new view. If we don't need an aligned view, use a
|
|
|
ee698d |
+ // byteshift of 0, so that we can use mmap.
|
|
|
ee698d |
+ return this->make_view(offset + start, size,
|
|
|
ee698d |
+ aligned ? byteshift : 0,
|
|
|
ee698d |
+ cache);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Get a view into the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+const unsigned char*
|
|
|
ee698d |
+File_read::get_view(off_t offset, off_t start, section_size_type size,
|
|
|
ee698d |
+ bool aligned, bool cache)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ File_read::View* pv = this->find_or_make_view(offset, start, size,
|
|
|
ee698d |
+ aligned, cache);
|
|
|
ee698d |
+ return pv->data() + (offset + start - pv->start() + pv->byteshift());
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_view*
|
|
|
ee698d |
+File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
|
|
|
ee698d |
+ bool aligned, bool cache)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ File_read::View* pv = this->find_or_make_view(offset, start, size,
|
|
|
ee698d |
+ aligned, cache);
|
|
|
ee698d |
+ pv->lock();
|
|
|
ee698d |
+ return new File_view(*this, pv,
|
|
|
ee698d |
+ (pv->data()
|
|
|
ee698d |
+ + (offset + start - pv->start() + pv->byteshift())));
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Use readv to read COUNT entries from RM starting at START. BASE
|
|
|
ee698d |
+// must be added to all file offsets in RM.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
|
|
|
ee698d |
+ size_t count)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ unsigned char discard[File_read::page_size];
|
|
|
ee698d |
+ iovec iov[File_read::max_readv_entries * 2];
|
|
|
ee698d |
+ size_t iov_index = 0;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ off_t first_offset = rm[start].file_offset;
|
|
|
ee698d |
+ off_t last_offset = first_offset;
|
|
|
ee698d |
+ ssize_t want = 0;
|
|
|
ee698d |
+ for (size_t i = 0; i < count; ++i)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ const Read_multiple_entry& i_entry(rm[start + i]);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (i_entry.file_offset > last_offset)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ size_t skip = i_entry.file_offset - last_offset;
|
|
|
ee698d |
+ gold_assert(skip <= sizeof discard);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ iov[iov_index].iov_base = discard;
|
|
|
ee698d |
+ iov[iov_index].iov_len = skip;
|
|
|
ee698d |
+ ++iov_index;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ want += skip;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ iov[iov_index].iov_base = i_entry.buffer;
|
|
|
ee698d |
+ iov[iov_index].iov_len = i_entry.size;
|
|
|
ee698d |
+ ++iov_index;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ want += i_entry.size;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ last_offset = i_entry.file_offset + i_entry.size;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ this->reopen_descriptor();
|
|
|
ee698d |
+
|
|
|
ee698d |
+ gold_assert(iov_index < sizeof iov / sizeof iov[0]);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
|
|
|
ee698d |
+ gold_fatal(_("%s: lseek failed: %s"),
|
|
|
ee698d |
+ this->filename().c_str(), strerror(errno));
|
|
|
ee698d |
+
|
|
|
ee698d |
+ ssize_t got = ::readv(this->descriptor_, iov, iov_index);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (got < 0)
|
|
|
ee698d |
+ gold_fatal(_("%s: readv failed: %s"),
|
|
|
ee698d |
+ this->filename().c_str(), strerror(errno));
|
|
|
ee698d |
+ if (got != want)
|
|
|
ee698d |
+ gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
|
|
|
ee698d |
+ this->filename().c_str(),
|
|
|
ee698d |
+ got, want, static_cast<long long>(base + first_offset));
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Portable IOV_MAX.
|
|
|
ee698d |
+
|
|
|
ee698d |
+#if !defined(HAVE_READV)
|
|
|
ee698d |
+#define GOLD_IOV_MAX 1
|
|
|
ee698d |
+#elif defined(IOV_MAX)
|
|
|
ee698d |
+#define GOLD_IOV_MAX IOV_MAX
|
|
|
ee698d |
+#else
|
|
|
ee698d |
+#define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Read several pieces of data from the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::read_multiple(off_t base, const Read_multiple& rm)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ static size_t iov_max = GOLD_IOV_MAX;
|
|
|
ee698d |
+ size_t count = rm.size();
|
|
|
ee698d |
+ size_t i = 0;
|
|
|
ee698d |
+ while (i < count)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ // Find up to MAX_READV_ENTRIES consecutive entries which are
|
|
|
ee698d |
+ // less than one page apart.
|
|
|
ee698d |
+ const Read_multiple_entry& i_entry(rm[i]);
|
|
|
ee698d |
+ off_t i_off = i_entry.file_offset;
|
|
|
ee698d |
+ off_t end_off = i_off + i_entry.size;
|
|
|
ee698d |
+ size_t j;
|
|
|
ee698d |
+ for (j = i + 1; j < count; ++j)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (j - i >= File_read::max_readv_entries || j - i >= iov_max / 2)
|
|
|
ee698d |
+ break;
|
|
|
ee698d |
+ const Read_multiple_entry& j_entry(rm[j]);
|
|
|
ee698d |
+ off_t j_off = j_entry.file_offset;
|
|
|
ee698d |
+ gold_assert(j_off >= end_off);
|
|
|
ee698d |
+ off_t j_end_off = j_off + j_entry.size;
|
|
|
ee698d |
+ if (j_end_off - end_off >= File_read::page_size)
|
|
|
ee698d |
+ break;
|
|
|
ee698d |
+ end_off = j_end_off;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (j == i + 1)
|
|
|
ee698d |
+ this->read(base + i_off, i_entry.size, i_entry.buffer);
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ File_read::View* view = this->find_view(base + i_off,
|
|
|
ee698d |
+ end_off - i_off,
|
|
|
ee698d |
+ -1U, NULL);
|
|
|
ee698d |
+ if (view == NULL)
|
|
|
ee698d |
+ this->do_readv(base, rm, i, j - i);
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ const unsigned char* v = (view->data()
|
|
|
ee698d |
+ + (base + i_off - view->start()
|
|
|
ee698d |
+ + view->byteshift()));
|
|
|
ee698d |
+ for (size_t k = i; k < j; ++k)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ const Read_multiple_entry& k_entry(rm[k]);
|
|
|
ee698d |
+ gold_assert((convert_to_section_size_type(k_entry.file_offset
|
|
|
ee698d |
+ - i_off)
|
|
|
ee698d |
+ + k_entry.size)
|
|
|
ee698d |
+ <= convert_to_section_size_type(end_off
|
|
|
ee698d |
+ - i_off));
|
|
|
ee698d |
+ memcpy(k_entry.buffer,
|
|
|
ee698d |
+ v + (k_entry.file_offset - i_off),
|
|
|
ee698d |
+ k_entry.size);
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ i = j;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Mark all views as no longer cached.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::clear_view_cache_marks()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ // Just ignore this if there are multiple objects associated with
|
|
|
ee698d |
+ // the file. Otherwise we will wind up uncaching and freeing some
|
|
|
ee698d |
+ // views for other objects.
|
|
|
ee698d |
+ if (this->object_count_ > 1)
|
|
|
ee698d |
+ return;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ for (Views::iterator p = this->views_.begin();
|
|
|
ee698d |
+ p != this->views_.end();
|
|
|
ee698d |
+ ++p)
|
|
|
ee698d |
+ p->second->clear_cache();
|
|
|
ee698d |
+ for (Saved_views::iterator p = this->saved_views_.begin();
|
|
|
ee698d |
+ p != this->saved_views_.end();
|
|
|
ee698d |
+ ++p)
|
|
|
ee698d |
+ (*p)->clear_cache();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Remove all the file views. For a file which has multiple
|
|
|
ee698d |
+// associated objects (i.e., an archive), we keep accessed views
|
|
|
ee698d |
+// around until next time, in the hopes that they will be useful for
|
|
|
ee698d |
+// the next object.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::clear_views(Clear_views_mode mode)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ bool keep_files_mapped = (parameters->options_valid()
|
|
|
ee698d |
+ && parameters->options().keep_files_mapped());
|
|
|
ee698d |
+ Views::iterator p = this->views_.begin();
|
|
|
ee698d |
+ while (p != this->views_.end())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ bool should_delete;
|
|
|
ee698d |
+ if (p->second->is_locked() || p->second->is_permanent_view())
|
|
|
ee698d |
+ should_delete = false;
|
|
|
ee698d |
+ else if (mode == CLEAR_VIEWS_ALL)
|
|
|
ee698d |
+ should_delete = true;
|
|
|
ee698d |
+ else if ((p->second->should_cache()
|
|
|
ee698d |
+ || p->second == this->whole_file_view_)
|
|
|
ee698d |
+ && keep_files_mapped)
|
|
|
ee698d |
+ should_delete = false;
|
|
|
ee698d |
+ else if (this->object_count_ > 1
|
|
|
ee698d |
+ && p->second->accessed()
|
|
|
ee698d |
+ && mode != CLEAR_VIEWS_ARCHIVE)
|
|
|
ee698d |
+ should_delete = false;
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ should_delete = true;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (should_delete)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (p->second == this->whole_file_view_)
|
|
|
ee698d |
+ this->whole_file_view_ = NULL;
|
|
|
ee698d |
+ delete p->second;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // map::erase invalidates only the iterator to the deleted
|
|
|
ee698d |
+ // element.
|
|
|
ee698d |
+ Views::iterator pe = p;
|
|
|
ee698d |
+ ++p;
|
|
|
ee698d |
+ this->views_.erase(pe);
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ p->second->clear_accessed();
|
|
|
ee698d |
+ ++p;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ Saved_views::iterator q = this->saved_views_.begin();
|
|
|
ee698d |
+ while (q != this->saved_views_.end())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ if (!(*q)->is_locked())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ delete *q;
|
|
|
ee698d |
+ q = this->saved_views_.erase(q);
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_assert(mode != CLEAR_VIEWS_ALL);
|
|
|
ee698d |
+ ++q;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Print statistical information to stderr. This is used for --stats.
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::print_stats()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
|
|
|
ee698d |
+ program_name, File_read::total_mapped_bytes);
|
|
|
ee698d |
+ fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
|
|
|
ee698d |
+ program_name, File_read::maximum_mapped_bytes);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Class File_view.
|
|
|
ee698d |
+
|
|
|
ee698d |
+File_view::~File_view()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ gold_assert(this->file_.is_locked());
|
|
|
ee698d |
+ this->view_->unlock();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Class Input_file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Create a file given just the filename.
|
|
|
ee698d |
+
|
|
|
ee698d |
+Input_file::Input_file(const char* name)
|
|
|
ee698d |
+ : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ this->input_argument_ =
|
|
|
ee698d |
+ new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
|
|
|
ee698d |
+ "", false, Position_dependent_options());
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Create a file for testing.
|
|
|
ee698d |
+
|
|
|
ee698d |
+Input_file::Input_file(const Task* task, const char* name,
|
|
|
ee698d |
+ const unsigned char* contents, off_t size)
|
|
|
ee698d |
+ : file_()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ this->input_argument_ =
|
|
|
ee698d |
+ new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
|
|
|
ee698d |
+ "", false, Position_dependent_options());
|
|
|
ee698d |
+ bool ok = this->file_.open(task, name, contents, size);
|
|
|
ee698d |
+ gold_assert(ok);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return the position dependent options in force for this file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+const Position_dependent_options&
|
|
|
ee698d |
+Input_file::options() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ return this->input_argument_->options();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return the name given by the user. For -lc this will return "c".
|
|
|
ee698d |
+
|
|
|
ee698d |
+const char*
|
|
|
ee698d |
+Input_file::name() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ return this->input_argument_->name();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return whether this file is in a system directory.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::is_in_system_directory() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ if (this->is_in_sysroot())
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+ return parameters->options().is_in_system_directory(this->filename());
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return whether we are only reading symbols.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::just_symbols() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ return this->input_argument_->just_symbols();
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return whether this is a file that we will search for in the list
|
|
|
ee698d |
+// of directories.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::will_search_for() const
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ return (!IS_ABSOLUTE_PATH(this->input_argument_->name())
|
|
|
ee698d |
+ && (this->input_argument_->is_lib()
|
|
|
ee698d |
+ || this->input_argument_->is_searched_file()
|
|
|
ee698d |
+ || this->input_argument_->extra_search_path() != NULL));
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Return the file last modification time. Calls gold_fatal if the stat
|
|
|
ee698d |
+// system call failed.
|
|
|
ee698d |
+
|
|
|
ee698d |
+Timespec
|
|
|
ee698d |
+File_read::get_mtime()
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ struct stat file_stat;
|
|
|
ee698d |
+ this->reopen_descriptor();
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (fstat(this->descriptor_, &file_stat) < 0)
|
|
|
ee698d |
+ gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(),
|
|
|
ee698d |
+ strerror(errno));
|
|
|
ee698d |
+#ifdef HAVE_STAT_ST_MTIM
|
|
|
ee698d |
+ return Timespec(file_stat.st_mtim.tv_sec, file_stat.st_mtim.tv_nsec);
|
|
|
ee698d |
+#else
|
|
|
ee698d |
+ return Timespec(file_stat.st_mtime, 0);
|
|
|
ee698d |
+#endif
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Try to find a file in the extra search dirs. Returns true on success.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::try_extra_search_path(int* pindex,
|
|
|
ee698d |
+ const Input_file_argument* input_argument,
|
|
|
ee698d |
+ std::string filename, std::string* found_name,
|
|
|
ee698d |
+ std::string* namep)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ if (input_argument->extra_search_path() == NULL)
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ std::string name = input_argument->extra_search_path();
|
|
|
ee698d |
+ if (!IS_DIR_SEPARATOR(name[name.length() - 1]))
|
|
|
ee698d |
+ name += '/';
|
|
|
ee698d |
+ name += filename;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ struct stat dummy_stat;
|
|
|
ee698d |
+ if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ *found_name = filename;
|
|
|
ee698d |
+ *namep = name;
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Find the actual file.
|
|
|
ee698d |
+// If the filename is not absolute, we assume it is in the current
|
|
|
ee698d |
+// directory *except* when:
|
|
|
ee698d |
+// A) input_argument_->is_lib() is true;
|
|
|
ee698d |
+// B) input_argument_->is_searched_file() is true; or
|
|
|
ee698d |
+// C) input_argument_->extra_search_path() is not empty.
|
|
|
ee698d |
+// In each, we look in extra_search_path + library_path to find
|
|
|
ee698d |
+// the file location, rather than the current directory.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::find_file(const Dirsearch& dirpath, int* pindex,
|
|
|
ee698d |
+ const Input_file_argument* input_argument,
|
|
|
ee698d |
+ bool* is_in_sysroot,
|
|
|
ee698d |
+ std::string* found_name, std::string* namep)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ std::string name;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Case 1: name is an absolute file, just try to open it
|
|
|
ee698d |
+ // Case 2: name is relative but is_lib is false, is_searched_file is false,
|
|
|
ee698d |
+ // and extra_search_path is empty
|
|
|
ee698d |
+ if (IS_ABSOLUTE_PATH(input_argument->name())
|
|
|
ee698d |
+ || (!input_argument->is_lib()
|
|
|
ee698d |
+ && !input_argument->is_searched_file()
|
|
|
ee698d |
+ && input_argument->extra_search_path() == NULL))
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ name = input_argument->name();
|
|
|
ee698d |
+ *found_name = name;
|
|
|
ee698d |
+ *namep = name;
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ // Case 3: is_lib is true or is_searched_file is true
|
|
|
ee698d |
+ else if (input_argument->is_lib()
|
|
|
ee698d |
+ || input_argument->is_searched_file())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ std::vector<std::string> names;
|
|
|
ee698d |
+ names.reserve(2);
|
|
|
ee698d |
+ if (input_argument->is_lib())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ std::string prefix = "lib";
|
|
|
ee698d |
+ prefix += input_argument->name();
|
|
|
ee698d |
+ if (parameters->options().is_static()
|
|
|
ee698d |
+ || !input_argument->options().Bdynamic())
|
|
|
ee698d |
+ names.push_back(prefix + ".a");
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ names.push_back(prefix + ".so");
|
|
|
ee698d |
+ names.push_back(prefix + ".a");
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ names.push_back(input_argument->name());
|
|
|
ee698d |
+
|
|
|
ee698d |
+ for (std::vector<std::string>::const_iterator n = names.begin();
|
|
|
ee698d |
+ n != names.end();
|
|
|
ee698d |
+ ++n)
|
|
|
ee698d |
+ if (Input_file::try_extra_search_path(pindex, input_argument, *n,
|
|
|
ee698d |
+ found_name, namep))
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // It is not in the extra_search_path.
|
|
|
ee698d |
+ name = dirpath.find(names, is_in_sysroot, pindex, found_name);
|
|
|
ee698d |
+ if (name.empty())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_error(_("cannot find %s%s"),
|
|
|
ee698d |
+ input_argument->is_lib() ? "-l" : "",
|
|
|
ee698d |
+ input_argument->name());
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ *namep = name;
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ // Case 4: extra_search_path is not empty
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_assert(input_argument->extra_search_path() != NULL);
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (try_extra_search_path(pindex, input_argument, input_argument->name(),
|
|
|
ee698d |
+ found_name, namep))
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // extra_search_path failed, so check the normal search-path.
|
|
|
ee698d |
+ int index = *pindex;
|
|
|
ee698d |
+ if (index > 0)
|
|
|
ee698d |
+ --index;
|
|
|
ee698d |
+ name = dirpath.find(std::vector<std::string>(1, input_argument->name()),
|
|
|
ee698d |
+ is_in_sysroot, &index, found_name);
|
|
|
ee698d |
+ if (name.empty())
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_error(_("cannot find %s"),
|
|
|
ee698d |
+ input_argument->name());
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ *namep = name;
|
|
|
ee698d |
+ *pindex = index + 1;
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Open the file.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::open(const Dirsearch& dirpath, const Task* task, int* pindex)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ std::string name;
|
|
|
ee698d |
+ if (!Input_file::find_file(dirpath, pindex, this->input_argument_,
|
|
|
ee698d |
+ &this->is_in_sysroot_, &this->found_name_, &name))
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+
|
|
|
ee698d |
+ // Now that we've figured out where the file lives, try to open it.
|
|
|
ee698d |
+
|
|
|
ee698d |
+ General_options::Object_format format =
|
|
|
ee698d |
+ this->input_argument_->options().format_enum();
|
|
|
ee698d |
+ bool ok;
|
|
|
ee698d |
+ if (format == General_options::OBJECT_FORMAT_ELF)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ ok = this->file_.open(task, name);
|
|
|
ee698d |
+ this->format_ = FORMAT_ELF;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+ else
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
|
|
|
ee698d |
+ ok = this->open_binary(task, name);
|
|
|
ee698d |
+ this->format_ = FORMAT_BINARY;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ if (!ok)
|
|
|
ee698d |
+ {
|
|
|
ee698d |
+ gold_error(_("cannot open %s: %s"),
|
|
|
ee698d |
+ name.c_str(), strerror(errno));
|
|
|
ee698d |
+ this->format_ = FORMAT_NONE;
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+ }
|
|
|
ee698d |
+
|
|
|
ee698d |
+ return true;
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+// Open a file for --format binary.
|
|
|
ee698d |
+
|
|
|
ee698d |
+bool
|
|
|
ee698d |
+Input_file::open_binary(const Task* task, const std::string& name)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ // In order to open a binary file, we need machine code, size, and
|
|
|
ee698d |
+ // endianness. We may not have a valid target at this point, in
|
|
|
ee698d |
+ // which case we use the default target.
|
|
|
ee698d |
+ parameters_force_valid_target();
|
|
|
ee698d |
+ const Target& target(parameters->target());
|
|
|
ee698d |
+
|
|
|
ee698d |
+ Binary_to_elf binary_to_elf(target.machine_code(),
|
|
|
ee698d |
+ target.get_size(),
|
|
|
ee698d |
+ target.is_big_endian(),
|
|
|
ee698d |
+ name);
|
|
|
ee698d |
+ if (!binary_to_elf.convert(task))
|
|
|
ee698d |
+ return false;
|
|
|
ee698d |
+ return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
|
|
|
ee698d |
+ binary_to_elf.converted_size());
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::record_file_read(const std::string& name)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ File_read::files_read.push_back(name);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+void
|
|
|
ee698d |
+File_read::write_dependency_file(const char* dependency_file_name,
|
|
|
ee698d |
+ const char* output_file_name)
|
|
|
ee698d |
+{
|
|
|
ee698d |
+ FILE *depfile = fopen(dependency_file_name, "w");
|
|
|
ee698d |
+
|
|
|
ee698d |
+ fprintf(depfile, "%s:", output_file_name);
|
|
|
ee698d |
+ for (std::vector<std::string>::const_iterator it = files_read.begin();
|
|
|
ee698d |
+ it != files_read.end();
|
|
|
ee698d |
+ ++it)
|
|
|
ee698d |
+ fprintf(depfile, " \\\n %s", it->c_str());
|
|
|
ee698d |
+ fprintf(depfile, "\n");
|
|
|
ee698d |
+
|
|
|
ee698d |
+ for (std::vector<std::string>::const_iterator it = files_read.begin();
|
|
|
ee698d |
+ it != files_read.end();
|
|
|
ee698d |
+ ++it)
|
|
|
ee698d |
+ fprintf(depfile, "\n%s:\n", it->c_str());
|
|
|
ee698d |
+
|
|
|
ee698d |
+ fclose(depfile);
|
|
|
ee698d |
+}
|
|
|
ee698d |
+
|
|
|
ee698d |
+} // End namespace gold.
|