Merge "Print memory maps on GC crash."
diff --git a/compiler/elf_builder.h b/compiler/elf_builder.h
index 07976e8..63d3a0d 100644
--- a/compiler/elf_builder.h
+++ b/compiler/elf_builder.h
@@ -149,19 +149,20 @@
     std::vector<ElfDynamicState> dynamics_;
   };
 
+  using PatchFn = void (*)(const std::vector<uintptr_t>& patch_locations,
+                           Elf_Addr buffer_address,
+                           Elf_Addr base_address,
+                           std::vector<uint8_t>* buffer);
+
   // Section with content based on simple memory buffer.
   // The buffer can be optionally patched before writing.
-  // The resulting address can be either absolute memory
-  // address or offset relative to the pointer location.
   class RawSection FINAL : public Section {
    public:
     RawSection(const std::string& name, Elf_Word type, Elf_Word flags,
                const Section* link, Elf_Word info, Elf_Word align, Elf_Word entsize,
-               const Section* patch_base = nullptr, bool patch_relative = false,
-               bool patch_64bit = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)))
+               PatchFn patch = nullptr, const Section* patch_base_section = nullptr)
         : Section(name, type, flags, link, info, align, entsize),
-          patched(false), patch_base_(patch_base),
-          patch_relative_(patch_relative), patch_64bit_(patch_64bit) {
+          patched_(false), patch_(patch), patch_base_section_(patch_base_section) {
     }
 
     Elf_Word GetSize() const OVERRIDE {
@@ -170,22 +171,14 @@
 
     bool Write(File* elf_file) OVERRIDE {
       if (!patch_locations_.empty()) {
-        DCHECK(patch_base_ != nullptr);
-        DCHECK(!patched);  // Do not patch twice.
-        if (patch_relative_) {
-          if (patch_64bit_) {
-            Patch<true, uint64_t>();
-          } else {
-            Patch<true, uint32_t>();
-          }
-        } else {
-          if (patch_64bit_) {
-            Patch<false, uint64_t>();
-          } else {
-            Patch<false, uint32_t>();
-          }
-        }
-        patched = true;
+        DCHECK(!patched_);  // Do not patch twice.
+        DCHECK(patch_ != nullptr);
+        DCHECK(patch_base_section_ != nullptr);
+        patch_(patch_locations_,
+               this->GetHeader()->sh_addr,
+               patch_base_section_->GetHeader()->sh_addr,
+               &buffer_);
+        patched_ = true;
       }
       return WriteArray(elf_file, buffer_.data(), buffer_.size());
     }
@@ -207,23 +200,13 @@
     }
 
    private:
-    template <bool RelativeAddress = false, typename PatchedAddress = Elf_Addr>
-    void Patch() {
-      Elf_Addr base_addr = patch_base_->GetHeader()->sh_addr;
-      Elf_Addr addr = this->GetHeader()->sh_addr;
-      for (uintptr_t patch_location : patch_locations_) {
-        typedef __attribute__((__aligned__(1))) PatchedAddress UnalignedAddress;
-        auto* to_patch = reinterpret_cast<UnalignedAddress*>(buffer_.data() + patch_location);
-        *to_patch = (base_addr + *to_patch) - (RelativeAddress ? (addr + patch_location) : 0);
-      }
-    }
-
     std::vector<uint8_t> buffer_;
     std::vector<uintptr_t> patch_locations_;
-    bool patched;
-    const Section* patch_base_;
-    bool patch_relative_;
-    bool patch_64bit_;
+    bool patched_;
+    // User-provided function to do the actual patching.
+    PatchFn patch_;
+    // The section that we patch against (usually .text).
+    const Section* patch_base_section_;
   };
 
   // Writer of .rodata section or .text section.
diff --git a/compiler/elf_writer_debug.cc b/compiler/elf_writer_debug.cc
index 28e6999..db599c4 100644
--- a/compiler/elf_writer_debug.cc
+++ b/compiler/elf_writer_debug.cc
@@ -162,33 +162,54 @@
                   ExceptionHeaderValueApplication address_type,
                   std::vector<uint8_t>* eh_frame,
                   std::vector<uintptr_t>* eh_frame_patches,
-                  std::vector<uint8_t>* eh_frame_hdr) {
+                  std::vector<uint8_t>* eh_frame_hdr,
+                  std::vector<uintptr_t>* eh_frame_hdr_patches) {
   const auto& method_infos = oat_writer->GetMethodDebugInfo();
   const InstructionSet isa = compiler->GetInstructionSet();
 
   // Write .eh_frame section.
+  std::map<uint32_t, size_t> address_to_fde_offset_map;
   size_t cie_offset = eh_frame->size();
   WriteEhFrameCIE(isa, address_type, eh_frame);
   for (const OatWriter::DebugInfo& mi : method_infos) {
-    const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
-    if (opcodes != nullptr) {
-      WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
-                      mi.low_pc_, mi.high_pc_ - mi.low_pc_,
-                      opcodes, eh_frame, eh_frame_patches);
+    if (!mi.deduped_) {  // Only one FDE per unique address.
+      const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
+      if (opcodes != nullptr) {
+        address_to_fde_offset_map.emplace(mi.low_pc_, eh_frame->size());
+        WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
+                        mi.low_pc_, mi.high_pc_ - mi.low_pc_,
+                        opcodes, eh_frame, eh_frame_patches);
+      }
     }
   }
 
   // Write .eh_frame_hdr section.
   Writer<> header(eh_frame_hdr);
   header.PushUint8(1);  // Version.
-  header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);  // Encoding of .eh_frame pointer.
-  header.PushUint8(DW_EH_PE_omit);  // Encoding of binary search table size.
-  header.PushUint8(DW_EH_PE_omit);  // Encoding of binary search table addresses.
-  // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section, and need to encode
-  // relative to this location as libunwind doesn't honor datarel for eh_frame_hdr correctly.
-  header.PushInt32(-static_cast<int32_t>(eh_frame->size() + 4U));
-  // Omit binary search table size (number of entries).
-  // Omit binary search table.
+  // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
+  // so we have to use pcrel which means relative to the pointer's location.
+  header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
+  // Encoding of binary search table size.
+  header.PushUint8(DW_EH_PE_udata4);
+  // Encoding of binary search table addresses - libunwind supports only this
+  // specific combination, which means relative to the start of .eh_frame_hdr.
+  header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
+  // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section
+  const int32_t relative_eh_frame_begin = -static_cast<int32_t>(eh_frame->size());
+  header.PushInt32(relative_eh_frame_begin - 4U);
+  // Binary search table size (number of entries).
+  header.PushUint32(address_to_fde_offset_map.size());
+  // Binary search table.
+  for (const auto& address_to_fde_offset : address_to_fde_offset_map) {
+    u_int32_t code_address = address_to_fde_offset.first;
+    size_t fde_address = address_to_fde_offset.second;
+    eh_frame_hdr_patches->push_back(header.data()->size());
+    header.PushUint32(code_address);
+    // We know the exact layout (eh_frame is immediately before eh_frame_hdr)
+    // and the data is relative to the start of the eh_frame_hdr,
+    // so patching isn't necessary (in contrast to the code address above).
+    header.PushInt32(relative_eh_frame_begin + fde_address);
+  }
 }
 
 /*
diff --git a/compiler/elf_writer_debug.h b/compiler/elf_writer_debug.h
index 5bf4841..28d0e2c 100644
--- a/compiler/elf_writer_debug.h
+++ b/compiler/elf_writer_debug.h
@@ -30,7 +30,8 @@
                   ExceptionHeaderValueApplication address_type,
                   std::vector<uint8_t>* eh_frame,
                   std::vector<uintptr_t>* eh_frame_patches,
-                  std::vector<uint8_t>* eh_frame_hdr);
+                  std::vector<uint8_t>* eh_frame_hdr,
+                  std::vector<uintptr_t>* eh_frame_hdr_patches);
 
 void WriteDebugSections(const CompilerDriver* compiler,
                         const OatWriter* oat_writer,
diff --git a/compiler/elf_writer_quick.cc b/compiler/elf_writer_quick.cc
index 178aa03..79f9955 100644
--- a/compiler/elf_writer_quick.cc
+++ b/compiler/elf_writer_quick.cc
@@ -107,12 +107,43 @@
   OatWriter* oat_writer_;
 };
 
+enum PatchResult {
+  kAbsoluteAddress,  // Absolute memory location.
+  kPointerRelativeAddress,  // Offset relative to the location of the pointer.
+  kSectionRelativeAddress,  // Offset relative to start of containing section.
+};
+
+// Patch memory addresses within a buffer.
+// It assumes that the unpatched addresses are offsets relative to base_address.
+// (which generally means method's low_pc relative to the start of .text)
+template <typename Elf_Addr, typename Address, PatchResult kPatchResult>
+static void Patch(const std::vector<uintptr_t>& patch_locations,
+                  Elf_Addr buffer_address, Elf_Addr base_address,
+                  std::vector<uint8_t>* buffer) {
+  for (uintptr_t location : patch_locations) {
+    typedef __attribute__((__aligned__(1))) Address UnalignedAddress;
+    auto* to_patch = reinterpret_cast<UnalignedAddress*>(buffer->data() + location);
+    switch (kPatchResult) {
+      case kAbsoluteAddress:
+        *to_patch = (base_address + *to_patch);
+        break;
+      case kPointerRelativeAddress:
+        *to_patch = (base_address + *to_patch) - (buffer_address + location);
+        break;
+      case kSectionRelativeAddress:
+        *to_patch = (base_address + *to_patch) - buffer_address;
+        break;
+    }
+  }
+}
+
 template <typename ElfTypes>
 bool ElfWriterQuick<ElfTypes>::Write(
     OatWriter* oat_writer,
     const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
     const std::string& android_root_unused ATTRIBUTE_UNUSED,
     bool is_host_unused ATTRIBUTE_UNUSED) {
+  using Elf_Addr = typename ElfTypes::Addr;
   const InstructionSet isa = compiler_driver_->GetInstructionSet();
 
   // Setup the builder with the main OAT sections (.rodata .text .bss).
@@ -129,27 +160,25 @@
   // but they are registred with the builder only if they are used.
   using RawSection = typename ElfBuilder<ElfTypes>::RawSection;
   const auto* text = builder->GetText();
-  constexpr bool absolute = false;  // patch to make absolute addresses.
-  constexpr bool relative = true;  // patch to make relative addresses.
   const bool is64bit = Is64BitInstructionSet(isa);
-  RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC,
-                      nullptr, 0, kPageSize, 0, text, relative, is64bit);
-  RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC,
-                          nullptr, 0, 4, 0);
-  RawSection debug_info(".debug_info", SHT_PROGBITS, 0,
-                        nullptr, 0, 1, 0, text, absolute, false /* 32bit */);
-  RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0,
-                          nullptr, 0, 1, 0);
-  RawSection debug_str(".debug_str", SHT_PROGBITS, 0,
-                       nullptr, 0, 1, 0);
-  RawSection debug_line(".debug_line", SHT_PROGBITS, 0,
-                        nullptr, 0, 1, 0, text, absolute, false /* 32bit */);
+  RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
+                      is64bit ? Patch<Elf_Addr, uint64_t, kPointerRelativeAddress> :
+                                Patch<Elf_Addr, uint32_t, kPointerRelativeAddress>,
+                      text);
+  RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0,
+                          Patch<Elf_Addr, uint32_t, kSectionRelativeAddress>, text);
+  RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
+                        Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
+  RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
+  RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
+  RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
+                        Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
   if (!oat_writer->GetMethodDebugInfo().empty()) {
     if (compiler_driver_->GetCompilerOptions().GetIncludeCFI()) {
       dwarf::WriteEhFrame(
           compiler_driver_, oat_writer, dwarf::DW_EH_PE_pcrel,
           eh_frame.GetBuffer(), eh_frame.GetPatchLocations(),
-          eh_frame_hdr.GetBuffer());
+          eh_frame_hdr.GetBuffer(), eh_frame_hdr.GetPatchLocations());
       builder->RegisterSection(&eh_frame);
       builder->RegisterSection(&eh_frame_hdr);
     }
@@ -202,6 +231,9 @@
 
   auto* symtab = builder->GetSymtab();
   for (auto it = method_info.begin(); it != method_info.end(); ++it) {
+    if (it->deduped_) {
+      continue;  // Add symbol only for the first instance.
+    }
     std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
     if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
       name += " [DEDUPED]";