Move GetSectionByName out of CompilationUnit.

For the upcoming Dwarf5 LineInfo changes, we will need to get
several additional sections by name, but without a the Compilation
Unit. This change prepares for that.

Change-Id: I566855abb339a856110a2f7d243d3848fe2b3c18
Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/2268861
Reviewed-by: Mark Mentovai <mark@chromium.org>
diff --git a/src/common/dwarf/dwarf2reader.cc b/src/common/dwarf/dwarf2reader.cc
index 138f920..a7eedaa 100644
--- a/src/common/dwarf/dwarf2reader.cc
+++ b/src/common/dwarf/dwarf2reader.cc
@@ -53,6 +53,18 @@
 
 namespace dwarf2reader {
 
+const SectionMap::const_iterator GetSectionByName(const SectionMap&
+                                                  sections, const char *name) {
+  assert(name[0] == '.');
+  auto iter = sections.find(name);
+  if (iter != sections.end())
+    return iter;
+  std::string macho_name("__");
+  macho_name += name + 1;
+  iter = sections.find(macho_name);
+  return iter;
+}
+
 CompilationUnit::CompilationUnit(const string& path,
                                  const SectionMap& sections, uint64_t offset,
                                  ByteReader* reader, Dwarf2Handler* handler)
@@ -100,7 +112,8 @@
     return;
 
   // First get the debug_abbrev section.
-  SectionMap::const_iterator iter = GetSectionByName(".debug_abbrev");
+  SectionMap::const_iterator iter =
+      GetSectionByName(sections_, ".debug_abbrev");
   assert(iter != sections_.end());
 
   abbrevs_ = new std::vector<Abbrev>;
@@ -352,7 +365,8 @@
 
 uint64_t CompilationUnit::Start() {
   // First get the debug_info section.
-  SectionMap::const_iterator iter = GetSectionByName(".debug_info");
+  SectionMap::const_iterator iter =
+      GetSectionByName(sections_, ".debug_info");
   assert(iter != sections_.end());
 
   // Set up our buffer
@@ -383,28 +397,28 @@
   ReadAbbrevs();
 
   // Set the string section if we have one.
-  iter = GetSectionByName(".debug_str");
+  iter = GetSectionByName(sections_, ".debug_str");
   if (iter != sections_.end()) {
     string_buffer_ = iter->second.first;
     string_buffer_length_ = iter->second.second;
   }
 
   // Set the line string section if we have one.
-  iter = GetSectionByName(".debug_line_str");
+  iter = GetSectionByName(sections_, ".debug_line_str");
   if (iter != sections_.end()) {
     line_string_buffer_ = iter->second.first;
     line_string_buffer_length_ = iter->second.second;
   }
 
   // Set the string offsets section if we have one.
-  iter = GetSectionByName(".debug_str_offsets");
+  iter = GetSectionByName(sections_, ".debug_str_offsets");
   if (iter != sections_.end()) {
     str_offsets_buffer_ = iter->second.first;
     str_offsets_buffer_length_ = iter->second.second;
   }
 
   // Set the address section if we have one.
-  iter = GetSectionByName(".debug_addr");
+  iter = GetSectionByName(sections_, ".debug_addr");
   if (iter != sections_.end()) {
     addr_buffer_ = iter->second.first;
     addr_buffer_length_ = iter->second.second;
diff --git a/src/common/dwarf/dwarf2reader.h b/src/common/dwarf/dwarf2reader.h
index cd91b10..228c867 100644
--- a/src/common/dwarf/dwarf2reader.h
+++ b/src/common/dwarf/dwarf2reader.h
@@ -65,6 +65,13 @@
 // This maps from a string naming a section to a pair containing a
 // the data for the section, and the size of the section.
 typedef std::map<string, std::pair<const uint8_t *, uint64_t> > SectionMap;
+
+// Abstract away the difference between elf and mach-o section names.
+// Elf-names use ".section_name, mach-o uses "__section_name".  Pass "name" in
+// the elf form, ".section_name".
+const SectionMap::const_iterator GetSectionByName(const SectionMap&
+                                                  sections, const char *name);
+
 typedef std::list<std::pair<enum DwarfAttribute, enum DwarfForm> >
     AttributeList;
 typedef AttributeList::iterator AttributeIterator;
@@ -544,20 +551,6 @@
   void ReadDebugSectionsFromDwo(ElfReader* elf_reader,
                                 SectionMap* sections);
 
-  // Abstract away the difference between elf, mach-o, and Mac OS section names.
-  // Elf-names use ".section_name, others use "__section_name".  Pass "name" in
-  // the elf form, ".section_name".
-  const SectionMap::const_iterator GetSectionByName(const char *name) {
-    assert(name[0] == '.');
-    auto iter = sections_.find(name);
-    if (iter != sections_.end())
-      return iter;
-    std::string macho_name("__");
-    macho_name += name + 1;
-    iter = sections_.find(macho_name);
-    return iter;
-  }
-
   // Path of the file containing the debug information.
   const string path_;
 
diff --git a/src/common/dwarf/functioninfo.cc b/src/common/dwarf/functioninfo.cc
index 358a6ee..edd1bb7 100644
--- a/src/common/dwarf/functioninfo.cc
+++ b/src/common/dwarf/functioninfo.cc
@@ -164,7 +164,8 @@
                                                      enum DwarfForm form,
                                                      uint64_t data) {
   if (attr == DW_AT_stmt_list) {
-    SectionMap::const_iterator iter = sections_.find("__debug_line");
+    SectionMap::const_iterator iter =
+        GetSectionByName(sections_, ".debug_line");
     assert(iter != sections_.end());
 
     scoped_ptr<LineInfo> lireader(new LineInfo(iter->second.first + data,
diff --git a/src/common/dwarf_cu_to_module.cc b/src/common/dwarf_cu_to_module.cc
index 24bbf83..be044ce 100644
--- a/src/common/dwarf_cu_to_module.cc
+++ b/src/common/dwarf_cu_to_module.cc
@@ -935,11 +935,7 @@
   const dwarf2reader::SectionMap &section_map
       = cu_context_->file_context->section_map();
   dwarf2reader::SectionMap::const_iterator map_entry
-      = section_map.find(".debug_line");
-  // Mac OS X puts DWARF data in sections whose names begin with "__"
-  // instead of ".".
-  if (map_entry == section_map.end())
-    map_entry = section_map.find("__debug_line");
+      = dwarf2reader::GetSectionByName(section_map, ".debug_line");
   if (map_entry == section_map.end()) {
     cu_context_->reporter->MissingSection(".debug_line");
     return;