Add options to set OS and filename

This allows Fuchsia to use dump_syms directly without a
postprocessing step.

Change-Id: I84507f8bedddfcdcdb237119457c8ddf8ac354d5
Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/1850718
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/src/common/linux/dump_symbols.cc b/src/common/linux/dump_symbols.cc
index 40bb3ff..660f133 100644
--- a/src/common/linux/dump_symbols.cc
+++ b/src/common/linux/dump_symbols.cc
@@ -921,6 +921,7 @@
 template<typename ElfClass>
 bool InitModuleForElfClass(const typename ElfClass::Ehdr* elf_header,
                            const string& obj_filename,
+                           const string& obj_os,
                            scoped_ptr<Module>& module) {
   PageAllocator allocator;
   wasteful_vector<uint8_t> identifier(&allocator, kDefaultBuildIdSize);
@@ -945,7 +946,6 @@
              ? name_buf
              : google_breakpad::BaseName(obj_filename);
 
-  string os = "Linux";
   // Add an extra "0" at the end.  PDB files on Windows have an 'age'
   // number appended to the end of the file identifier; this isn't
   // really used or necessary on other platforms, but be consistent.
@@ -953,7 +953,7 @@
   // This is just the raw Build ID in hex.
   string code_id = FileID::ConvertIdentifierToString(identifier);
 
-  module.reset(new Module(name, os, architecture, id, code_id));
+  module.reset(new Module(name, obj_os, architecture, id, code_id));
 
   return true;
 }
@@ -961,6 +961,7 @@
 template<typename ElfClass>
 bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
                             const string& obj_filename,
+                            const string& obj_os,
                             const std::vector<string>& debug_dirs,
                             const DumpOptions& options,
                             Module** out_module) {
@@ -969,7 +970,8 @@
   *out_module = NULL;
 
   scoped_ptr<Module> module;
-  if (!InitModuleForElfClass<ElfClass>(elf_header, obj_filename, module)) {
+  if (!InitModuleForElfClass<ElfClass>(elf_header, obj_filename, obj_os,
+                                       module)) {
     return false;
   }
 
@@ -1017,6 +1019,7 @@
 // Not explicitly exported, but not static so it can be used in unit tests.
 bool ReadSymbolDataInternal(const uint8_t* obj_file,
                             const string& obj_filename,
+                            const string& obj_os,
                             const std::vector<string>& debug_dirs,
                             const DumpOptions& options,
                             Module** module) {
@@ -1028,24 +1031,27 @@
   int elfclass = ElfClass(obj_file);
   if (elfclass == ELFCLASS32) {
     return ReadSymbolDataElfClass<ElfClass32>(
-        reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, debug_dirs,
-        options, module);
+        reinterpret_cast<const Elf32_Ehdr*>(obj_file), obj_filename, obj_os,
+        debug_dirs, options, module);
   }
   if (elfclass == ELFCLASS64) {
     return ReadSymbolDataElfClass<ElfClass64>(
-        reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, debug_dirs,
-        options, module);
+        reinterpret_cast<const Elf64_Ehdr*>(obj_file), obj_filename, obj_os,
+        debug_dirs, options, module);
   }
 
   return false;
 }
 
-bool WriteSymbolFile(const string &obj_file,
+bool WriteSymbolFile(const string &load_path,
+                     const string &obj_file,
+                     const string &obj_os,
                      const std::vector<string>& debug_dirs,
                      const DumpOptions& options,
                      std::ostream &sym_stream) {
   Module* module;
-  if (!ReadSymbolData(obj_file, debug_dirs, options, &module))
+  if (!ReadSymbolData(load_path, obj_file, obj_os, debug_dirs, options,
+                      &module))
     return false;
 
   bool result = module->Write(sym_stream, options.symbol_data);
@@ -1056,11 +1062,13 @@
 // Read the selected object file's debugging information, and write out the
 // header only to |stream|. Return true on success; if an error occurs, report
 // it and return false.
-bool WriteSymbolFileHeader(const string& obj_file,
+bool WriteSymbolFileHeader(const string& load_path,
+                           const string& obj_file,
+                           const string& obj_os,
                            std::ostream &sym_stream) {
   MmapWrapper map_wrapper;
   void* elf_header = NULL;
-  if (!LoadELF(obj_file, &map_wrapper, &elf_header)) {
+  if (!LoadELF(load_path, &map_wrapper, &elf_header)) {
     fprintf(stderr, "Could not load ELF file: %s\n", obj_file.c_str());
     return false;
   }
@@ -1074,13 +1082,15 @@
   scoped_ptr<Module> module;
   if (elfclass == ELFCLASS32) {
     if (!InitModuleForElfClass<ElfClass32>(
-        reinterpret_cast<const Elf32_Ehdr*>(elf_header), obj_file, module)) {
+        reinterpret_cast<const Elf32_Ehdr*>(elf_header), obj_file, obj_os,
+        module)) {
       fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
       return false;
     }
   } else if (elfclass == ELFCLASS64) {
     if (!InitModuleForElfClass<ElfClass64>(
-        reinterpret_cast<const Elf64_Ehdr*>(elf_header), obj_file, module)) {
+        reinterpret_cast<const Elf64_Ehdr*>(elf_header), obj_file, obj_os,
+        module)) {
       fprintf(stderr, "Failed to load ELF module: %s\n", obj_file.c_str());
       return false;
     }
@@ -1092,17 +1102,19 @@
   return module->Write(sym_stream, ALL_SYMBOL_DATA);
 }
 
-bool ReadSymbolData(const string& obj_file,
+bool ReadSymbolData(const string& load_path,
+                    const string& obj_file,
+                    const string& obj_os,
                     const std::vector<string>& debug_dirs,
                     const DumpOptions& options,
                     Module** module) {
   MmapWrapper map_wrapper;
   void* elf_header = NULL;
-  if (!LoadELF(obj_file, &map_wrapper, &elf_header))
+  if (!LoadELF(load_path, &map_wrapper, &elf_header))
     return false;
 
   return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header),
-                                obj_file, debug_dirs, options, module);
+                                obj_file, obj_os, debug_dirs, options, module);
 }
 
 }  // namespace google_breakpad
diff --git a/src/common/linux/dump_symbols.h b/src/common/linux/dump_symbols.h
index 1f204cb..eaddd8b 100644
--- a/src/common/linux/dump_symbols.h
+++ b/src/common/linux/dump_symbols.h
@@ -62,21 +62,28 @@
 // If OBJ_FILE has been stripped but contains a .gnu_debuglink section,
 // then look for the debug file in DEBUG_DIRS.
 // SYMBOL_DATA allows limiting the type of symbol data written.
-bool WriteSymbolFile(const string &obj_file,
+bool WriteSymbolFile(const string &load_path,
+                     const string &obj_file,
+                     const string &obj_os,
                      const std::vector<string>& debug_dirs,
                      const DumpOptions& options,
                      std::ostream &sym_stream);
 
 // Read the selected object file's debugging information, and write out the
 // header only to |stream|. Return true on success; if an error occurs, report
-// it and return false.
-bool WriteSymbolFileHeader(const string& obj_file,
+// it and return false. |obj_file| becomes the MODULE file name and |obj_os|
+// becomes the MODULE operating system.
+bool WriteSymbolFileHeader(const string& load_path,
+                           const string& obj_file,
+                           const string& obj_os,
                            std::ostream &sym_stream);
 
 // As above, but simply return the debugging information in MODULE
 // instead of writing it to a stream. The caller owns the resulting
 // Module object and must delete it when finished.
-bool ReadSymbolData(const string& obj_file,
+bool ReadSymbolData(const string& load_path,
+                    const string& obj_file,
+                    const string& obj_os,
                     const std::vector<string>& debug_dirs,
                     const DumpOptions& options,
                     Module** module);
diff --git a/src/common/linux/dump_symbols_unittest.cc b/src/common/linux/dump_symbols_unittest.cc
index f011c8a..54c2109 100644
--- a/src/common/linux/dump_symbols_unittest.cc
+++ b/src/common/linux/dump_symbols_unittest.cc
@@ -51,6 +51,7 @@
 
 bool ReadSymbolDataInternal(const uint8_t* obj_file,
                             const string& obj_filename,
+                            const string& obj_os,
                             const std::vector<string>& debug_dir,
                             const DumpOptions& options,
                             Module** module);
@@ -94,6 +95,7 @@
   DumpOptions options(ALL_SYMBOL_DATA, true);
   EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header),
                                       "foo",
+                                      "Linux",
                                       vector<string>(),
                                       options,
                                       &module));
@@ -130,6 +132,7 @@
   DumpOptions options(ALL_SYMBOL_DATA, true);
   EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata,
                                      "foo",
+                                     "Linux",
                                      vector<string>(),
                                      options,
                                      &module));
@@ -186,6 +189,7 @@
   DumpOptions options(ALL_SYMBOL_DATA, true);
   EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata,
                                      "foo",
+                                     "Linux",
                                      vector<string>(),
                                      options,
                                      &module));
diff --git a/src/tools/linux/dump_syms/dump_syms.cc b/src/tools/linux/dump_syms/dump_syms.cc
index 8495317..ebdf231 100644
--- a/src/tools/linux/dump_syms/dump_syms.cc
+++ b/src/tools/linux/dump_syms/dump_syms.cc
@@ -45,10 +45,14 @@
   fprintf(stderr, "Usage: %s [OPTION] <binary-with-debugging-info> "
           "[directories-for-debug-file]\n\n", self);
   fprintf(stderr, "Options:\n");
-  fprintf(stderr, "  -i:   Output module header information only.\n");
-  fprintf(stderr, "  -c    Do not generate CFI section\n");
-  fprintf(stderr, "  -r    Do not handle inter-compilation unit references\n");
-  fprintf(stderr, "  -v    Print all warnings to stderr\n");
+  fprintf(stderr, "  -i:         Output module header information only.\n");
+  fprintf(stderr, "  -c          Do not generate CFI section\n");
+  fprintf(stderr, "  -r          Do not handle inter-compilation "
+                                 "unit references\n");
+  fprintf(stderr, "  -v          Print all warnings to stderr\n");
+  fprintf(stderr, "  -n <name>   Use specified name for name of the object\n");
+  fprintf(stderr, "  -o <os>     Use specified name for the "
+                                 "operating system\n");
   return 1;
 }
 
@@ -59,6 +63,8 @@
   bool cfi = true;
   bool handle_inter_cu_refs = true;
   bool log_to_stderr = false;
+  std::string obj_name;
+  const char* obj_os = "Linux";
   int arg_index = 1;
   while (arg_index < argc && strlen(argv[arg_index]) > 0 &&
          argv[arg_index][0] == '-') {
@@ -70,6 +76,20 @@
       handle_inter_cu_refs = false;
     } else if (strcmp("-v", argv[arg_index]) == 0) {
       log_to_stderr = true;
+    } else if (strcmp("-n", argv[arg_index]) == 0) {
+      if (arg_index + 1 >= argc) {
+        fprintf(stderr, "Missing argument to -n\n");
+        return usage(argv[0]);
+      }
+      obj_name = argv[arg_index + 1];
+      ++arg_index;
+    } else if (strcmp("-o", argv[arg_index]) == 0) {
+      if (arg_index + 1 >= argc) {
+        fprintf(stderr, "Missing argument to -o\n");
+        return usage(argv[0]);
+      }
+      obj_os = argv[arg_index + 1];
+      ++arg_index;
     } else {
       printf("2.4 %s\n", argv[arg_index]);
       return usage(argv[0]);
@@ -95,15 +115,19 @@
     debug_dirs.push_back(argv[debug_dir_index]);
   }
 
+  if (obj_name.empty())
+    obj_name = binary;
+
   if (header_only) {
-    if (!WriteSymbolFileHeader(binary, std::cout)) {
+    if (!WriteSymbolFileHeader(binary, obj_name, obj_os, std::cout)) {
       fprintf(saved_stderr, "Failed to process file.\n");
       return 1;
     }
   } else {
     SymbolData symbol_data = cfi ? ALL_SYMBOL_DATA : NO_CFI;
     google_breakpad::DumpOptions options(symbol_data, handle_inter_cu_refs);
-    if (!WriteSymbolFile(binary, debug_dirs, options, std::cout)) {
+    if (!WriteSymbolFile(binary, obj_name, obj_os, debug_dirs, options,
+                         std::cout)) {
       fprintf(saved_stderr, "Failed to write symbol file.\n");
       return 1;
     }