From Craig Silverstein: rename some option functions in preparation
[external/binutils.git] / gold / fileread.cc
index ac96f5f..2931d43 100644 (file)
 #include <sys/uio.h>
 #include "filenames.h"
 
+#include "parameters.h"
 #include "options.h"
 #include "dirsearch.h"
+#include "target.h"
+#include "binary.h"
 #include "fileread.h"
 
 namespace gold
@@ -122,7 +125,7 @@ File_read::open(const Task* task, const std::string& name)
   return this->descriptor_ >= 0;
 }
 
-// Open the file for testing purposes.
+// Open the file with the contents in memory.
 
 bool
 File_read::open(const Task* task, const std::string& name,
@@ -576,11 +579,36 @@ Input_file::Input_file(const Task* task, const char* name,
   : file_()
 {
   this->input_argument_ =
-    new Input_file_argument(name, false, "", Position_dependent_options());
+    new Input_file_argument(name, false, "", false,
+                           Position_dependent_options());
   bool ok = file_.open(task, name, contents, size);
   gold_assert(ok);
 }
 
+// Return the position dependent options in force for this file.
+
+const Position_dependent_options&
+Input_file::options() const
+{
+  return this->input_argument_->options();
+}
+
+// Return the name given by the user.  For -lc this will return "c".
+
+const char*
+Input_file::name() const
+{
+  return this->input_argument_->name();
+}
+
+// Return whether we are only reading symbols.
+
+bool
+Input_file::just_symbols() const
+{
+  return this->input_argument_->just_symbols();
+}
+
 // Open the file.
 
 // If the filename is not absolute, we assume it is in the current
@@ -615,7 +643,7 @@ Input_file::open(const General_options& options, const Dirsearch& dirpath,
       n1 += this->input_argument_->name();
       std::string n2;
       if (options.is_static()
-         || this->input_argument_->options().do_static_search())
+         || !this->input_argument_->options().Bdynamic())
        n1 += ".a";
       else
        {
@@ -661,7 +689,19 @@ Input_file::open(const General_options& options, const Dirsearch& dirpath,
     }
 
   // Now that we've figured out where the file lives, try to open it.
-  if (!this->file_.open(task, name))
+
+  General_options::Object_format format =
+    this->input_argument_->options().format_enum();
+  bool ok;
+  if (format == General_options::OBJECT_FORMAT_ELF)
+    ok = this->file_.open(task, name);
+  else
+    {
+      gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
+      ok = this->open_binary(options, task, name);
+    }
+
+  if (!ok)
     {
       gold_error(_("cannot open %s: %s"),
                 name.c_str(), strerror(errno));
@@ -671,4 +711,29 @@ Input_file::open(const General_options& options, const Dirsearch& dirpath,
   return true;
 }
 
+// Open a file for --format binary.
+
+bool
+Input_file::open_binary(const General_options&,
+                       const Task* task, const std::string& name)
+{
+  // In order to open a binary file, we need machine code, size, and
+  // endianness.  We may not have a valid target at this point, in
+  // which case we use the default target.
+  const Target* target;
+  if (parameters->target_valid())
+    target = &parameters->target();
+  else
+    target = &parameters->default_target();
+
+  Binary_to_elf binary_to_elf(target->machine_code(),
+                             target->get_size(),
+                             target->is_big_endian(),
+                             name);
+  if (!binary_to_elf.convert(task))
+    return false;
+  return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
+                         binary_to_elf.converted_size());
+}
+
 } // End namespace gold.