Change the method which generates DWARF mini-debug-info.

This splits some code from CL198651. It moves the WriteSection
call out one level and does not otherwise change behaviour.

Change-Id: I7dc1c7c08b577b50bf6fa366a9b0ca757048b81e
diff --git a/compiler/elf_builder.h b/compiler/elf_builder.h
index bc7c83e..b673eeb 100644
--- a/compiler/elf_builder.h
+++ b/compiler/elf_builder.h
@@ -361,12 +361,6 @@
     other_sections_.push_back(std::move(s));
   }
 
-  // Set where the next section will be allocated in the virtual address space.
-  void SetVirtualAddress(Elf_Addr address) {
-    DCHECK_GE(address, virtual_address_);
-    virtual_address_ = address;
-  }
-
   // Reserve space for ELF header and program headers.
   // We do not know the number of headers until later, so
   // it is easiest to just reserve a fixed amount of space.
diff --git a/compiler/elf_writer_debug.cc b/compiler/elf_writer_debug.cc
index ca8cd68..e2481b0 100644
--- a/compiler/elf_writer_debug.cc
+++ b/compiler/elf_writer_debug.cc
@@ -1549,20 +1549,20 @@
 }
 
 template <typename ElfTypes>
-void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder,
-                        const ArrayRef<const MethodDebugInfo>& method_infos) {
-  const InstructionSet isa = parent_builder->GetIsa();
+std::vector<uint8_t> MakeMiniDebugInfoInternal(
+    InstructionSet isa,
+    size_t rodata_section_size,
+    size_t text_section_size,
+    const ArrayRef<const MethodDebugInfo>& method_infos) {
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
   VectorOutputStream out("Mini-debug-info ELF file", &buffer);
   std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
   builder->Start();
-  // Write .rodata and .text as NOBITS sections.
-  // This allows tools to detect virtual address relocation of the parent ELF file.
-  builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress());
-  builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize());
-  builder->SetVirtualAddress(parent_builder->GetText()->GetAddress());
-  builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize());
+  // Mirror .rodata and .text as NOBITS sections.
+  // It is needed to detected relocations after compression.
+  builder->GetRoData()->WriteNoBitsSection(rodata_section_size);
+  builder->GetText()->WriteNoBitsSection(text_section_size);
   WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */);
   WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */);
   builder->End();
@@ -1570,7 +1570,19 @@
   std::vector<uint8_t> compressed_buffer;
   compressed_buffer.reserve(buffer.size() / 4);
   XzCompress(&buffer, &compressed_buffer);
-  parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer);
+  return compressed_buffer;
+}
+
+std::vector<uint8_t> MakeMiniDebugInfo(
+    InstructionSet isa,
+    size_t rodata_size,
+    size_t text_size,
+    const ArrayRef<const MethodDebugInfo>& method_infos) {
+  if (Is64BitInstructionSet(isa)) {
+    return MakeMiniDebugInfoInternal<ElfTypes64>(isa, rodata_size, text_size, method_infos);
+  } else {
+    return MakeMiniDebugInfoInternal<ElfTypes32>(isa, rodata_size, text_size, method_infos);
+  }
 }
 
 template <typename ElfTypes>
@@ -1649,12 +1661,6 @@
     const ArrayRef<const MethodDebugInfo>& method_infos,
     CFIFormat cfi_format,
     bool write_oat_patches);
-template void WriteMiniDebugInfo<ElfTypes32>(
-    ElfBuilder<ElfTypes32>* builder,
-    const ArrayRef<const MethodDebugInfo>& method_infos);
-template void WriteMiniDebugInfo<ElfTypes64>(
-    ElfBuilder<ElfTypes64>* builder,
-    const ArrayRef<const MethodDebugInfo>& method_infos);
 
 }  // namespace dwarf
 }  // namespace art
diff --git a/compiler/elf_writer_debug.h b/compiler/elf_writer_debug.h
index 8e8472f..e289197 100644
--- a/compiler/elf_writer_debug.h
+++ b/compiler/elf_writer_debug.h
@@ -36,9 +36,10 @@
                     CFIFormat cfi_format,
                     bool write_oat_patches);
 
-template <typename ElfTypes>
-void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* builder,
-                        const ArrayRef<const MethodDebugInfo>& method_infos);
+std::vector<uint8_t> MakeMiniDebugInfo(InstructionSet isa,
+                                       size_t rodata_section_size,
+                                       size_t text_section_size,
+                                       const ArrayRef<const MethodDebugInfo>& method_infos);
 
 ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info);
 
diff --git a/compiler/elf_writer_quick.cc b/compiler/elf_writer_quick.cc
index f2a95f2..05ac219 100644
--- a/compiler/elf_writer_quick.cc
+++ b/compiler/elf_writer_quick.cc
@@ -155,7 +155,12 @@
   }
   if (compiler_options_->GetGenerateMiniDebugInfo()) {
     // Generate only some information and compress it.
-    dwarf::WriteMiniDebugInfo(builder_.get(), method_infos);
+    std::vector<uint8_t> xz_elf_file = MakeMiniDebugInfo(
+        builder_->GetIsa(),
+        builder_->GetRoData()->GetSize(),
+        builder_->GetText()->GetSize(),
+        method_infos);
+    builder_->WriteSection(".gnu_debugdata", &xz_elf_file);
   }
 }