Full support for --sysroot.
[external/binutils.git] / gold / fileread.cc
index 3cb2685..1bbd841 100644 (file)
@@ -26,6 +26,8 @@
 #include <cerrno>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/mman.h>
+#include "filenames.h"
 
 #include "options.h"
 #include "dirsearch.h"
@@ -39,7 +41,14 @@ namespace gold
 File_read::View::~View()
 {
   gold_assert(!this->is_locked());
-  delete[] this->data_;
+  if (!this->mapped_)
+    delete[] this->data_;
+  else
+    {
+      if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
+        fprintf(stderr, _("%s: munmap failed: %s\n"),
+                program_name, strerror(errno));
+    }
 }
 
 void
@@ -258,11 +267,32 @@ File_read::find_or_make_view(off_t start, off_t size, bool cache)
       gold_assert(psize >= size);
     }
 
-  unsigned char* p = new unsigned char[psize];
+  File_read::View* v;
 
-  this->do_read(poff, psize, p);
+  if (this->contents_ != NULL)
+    {
+      unsigned char* p = new unsigned char[psize];
+      this->do_read(poff, psize, p);
+      v = new File_read::View(poff, psize, p, cache, false);
+    }
+  else
+    {
+      void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
+                       this->descriptor_, poff);
+      if (p == MAP_FAILED)
+        {
+          fprintf(stderr, _("%s: %s: mmap offset %lld size %lld failed: %s\n"),
+                  program_name, this->filename().c_str(),
+                  static_cast<long long>(poff),
+                  static_cast<long long>(psize),
+                  strerror(errno));
+          gold_exit(false);
+        }
+
+      const unsigned char* pbytes = static_cast<const unsigned char*>(p);
+      v = new File_read::View(poff, psize, pbytes, cache, true);
+    }
 
-  File_read::View* v = new File_read::View(poff, psize, p, cache);
   ins.first->second = v;
   return v;
 }
@@ -342,21 +372,38 @@ Input_file::Input_file(const char* name, const unsigned char* contents,
   : file_()
 {
   this->input_argument_ =
-    new Input_file_argument(name, false, Position_dependent_options());
+    new Input_file_argument(name, false, "", Position_dependent_options());
   bool ok = file_.open(name, contents, size);
   gold_assert(ok);
 }
 
 // Open the file.
 
+// If the filename is not absolute, we assume it is in the current
+// directory *except* when:
+//    A) input_argument_->is_lib() is true; or
+//    B) input_argument_->extra_search_path() is not empty.
+// In both cases, we look in extra_search_path + library_path to find
+// the file location, rather than the current directory.
+
 void
 Input_file::open(const General_options& options, const Dirsearch& dirpath)
 {
   std::string name;
-  if (!this->input_argument_->is_lib())
+
+  // Case 1: name is an absolute file, just try to open it
+  // Case 2: name is relative but is_lib is false and extra_search_path
+  //         is empty
+  if (IS_ABSOLUTE_PATH (this->input_argument_->name())
+      || (!this->input_argument_->is_lib()
+         && this->input_argument_->extra_search_path() == NULL))
     name = this->input_argument_->name();
-  else
+
+  // Case 3: is_lib is true
+  else if (this->input_argument_->is_lib())
     {
+      // We don't yet support extra_search_path with -l.
+      gold_assert(this->input_argument_->extra_search_path() == NULL);
       std::string n1("lib");
       n1 += this->input_argument_->name();
       std::string n2;
@@ -367,15 +414,41 @@ Input_file::open(const General_options& options, const Dirsearch& dirpath)
          n2 = n1 + ".a";
          n1 += ".so";
        }
-      name = dirpath.find(n1, n2);
+      name = dirpath.find(n1, n2, &this->is_in_sysroot_);
       if (name.empty())
        {
-         fprintf(stderr, _("%s: cannot find %s\n"), program_name,
+         fprintf(stderr, _("%s: cannot find -l%s\n"), program_name,
                  this->input_argument_->name());
          gold_exit(false);
        }
     }
 
+  // Case 4: extra_search_path is not empty
+  else
+    {
+      gold_assert(this->input_argument_->extra_search_path() != NULL);
+
+      // First, check extra_search_path.
+      name = this->input_argument_->extra_search_path();
+      if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
+        name += '/';
+      name += this->input_argument_->name();
+      struct stat dummy_stat;
+      if (::stat(name.c_str(), &dummy_stat) < 0)
+        {
+          // extra_search_path failed, so check the normal search-path.
+          name = dirpath.find(this->input_argument_->name(), "",
+                             &this->is_in_sysroot_);
+          if (name.empty())
+            {
+              fprintf(stderr, _("%s: cannot find %s\n"), program_name,
+                      this->input_argument_->name());
+              gold_exit(false);
+            }
+        }
+    }
+
+  // Now that we've figured out where the file lives, try to open it.
   if (!this->file_.open(name))
     {
       fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,