ART stack unwinding fixes for libunwind/gdb/lldb.

dex2oat can already generate unwinding and symbol information which
allows tools to create backtrace of mixed native and Java code.

This is a cherry pick from aosp/master which fixes several issues.
Most notably:
 * It enables generation of ELF-64 on 64-bit systems (in dex2oat, C
   compilers already produce ELF-64).  Libunwind requires ELF-64 on
   64-bit systems for backtraces to work.
 * It enables loading of ELF files with dlopen.  This is required for
   libunwind to be able to generate backtrace of current process (i.e.
   the process requesting backtrace of itself).
 * It adds unit test to test the above (32 vs 64 bit, in-proces vs
   out-of-process, application code vs framework code).
 * Some other fixes or clean-ups which should not be of much
   significance but which are easier to include to make the
   important CLs cherry-pick cleanly.

This is squash of the following commits from aosp/master:
  7381010 ART: CFI Test
  e1bbed2 ART: Blacklist CFI test for non-compiled run-tests
  aab9f73 ART: Blacklist CFI test for JIT
  4437219 ART: Blacklist CFI test for Heap Poisoning
  a3a49fe Switch to using ELF-64 for 64-bit architectures.
  297ed22 Write 64-bit address in DWARF if we are on 64-bit architecture.
  24981a1 Set correct size of PT_PHDR ELF segment.
  1a146bf Link .dynamic to .dynstr
  67a0653 Make some parts of ELF more (pointer) aligned.
  f50fa82 Enable 64-bit CFI tests.
  49e1fab Use dlopen to load oat files.
  5dedb80 Add more logging output for dlopen.
  aa03870 Find the dlopened file using address rather than file path.
  82e73dc Release dummy MemMaps corresponding to dlopen.
  5c40961 Test that we can unwind framework code.
  020c543 Add more log output to the CFI test.
  88da3b0 ART: Fix CFI test wrt/ PIC
  a70e5b9 CFI test: kill the other process in native code.
  ad5fa8c Support generation of CFI in .debug_frame format.
  90688ae Fix build - large frame size of ElfWriterQuick<ElfTypes>::Write.
  97dabb7 Fix build breakage in dwarf_test.
  388d286 Generate just single ARM mapping symbol.
  f898087 Split .oat_patches to multiple sections.
  491a7fe Fix build - large frame size of ElfWriterQuick<ElfTypes>::Write (again).
  8363c77 Add --generate-debug-info flag and remove the other two flags.
  461d72a Generate debug info for core.oat files.

Bug: 21924613
Change-Id: I3f944a08dd2ed1df4d8a807da4fee423fdd35eb7
diff --git a/compiler/elf_builder.h b/compiler/elf_builder.h
index 2c68bb8..bbd962f 100644
--- a/compiler/elf_builder.h
+++ b/compiler/elf_builder.h
@@ -166,6 +166,10 @@
           patched_(false), patch_(patch), patch_base_section_(patch_base_section) {
     }
 
+    RawSection(const std::string& name, Elf_Word type)
+        : RawSection(name, type, 0, nullptr, 0, 1, 0, nullptr, nullptr) {
+    }
+
     Elf_Word GetSize() const OVERRIDE {
       return buffer_.size();
     }
@@ -263,7 +267,7 @@
   class StrtabSection FINAL : public Section {
    public:
     StrtabSection(const std::string& name, Elf_Word flags)
-        : Section(name, SHT_STRTAB, flags, nullptr, 0, 1, 1) {
+        : Section(name, SHT_STRTAB, flags, nullptr, 0, 1, 0) {
       buffer_.reserve(4 * KB);
       // The first entry of strtab must be empty string.
       buffer_ += '\0';
@@ -306,7 +310,7 @@
 
     SymtabSection(const std::string& name, Elf_Word type, Elf_Word flags,
                   StrtabSection* strtab)
-        : Section(name, type, flags, strtab, 0, sizeof(Elf_Word), sizeof(Elf_Sym)),
+        : Section(name, type, flags, strtab, 0, sizeof(Elf_Off), sizeof(Elf_Sym)),
           strtab_(strtab) {
     }
 
@@ -499,7 +503,7 @@
       text_(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
             nullptr, 0, kPageSize, 0, text_size, text_writer),
       bss_(".bss", bss_size),
-      dynamic_(".dynamic", &dynsym_),
+      dynamic_(".dynamic", &dynstr_),
       strtab_(".strtab", 0),
       symtab_(".symtab", SHT_SYMTAB, 0, &strtab_),
       shstrtab_(".shstrtab", 0) {
@@ -641,11 +645,10 @@
     // It is easiest to just reserve a fixed amount of space for them.
     constexpr size_t kMaxProgramHeaders = 8;
     constexpr size_t kProgramHeadersOffset = sizeof(Elf_Ehdr);
-    constexpr size_t kProgramHeadersSize = sizeof(Elf_Phdr) * kMaxProgramHeaders;
 
     // Layout of all sections - determine the final file offsets and addresses.
     // This must be done after we have built all sections and know their size.
-    Elf_Off file_offset = kProgramHeadersOffset + kProgramHeadersSize;
+    Elf_Off file_offset = kProgramHeadersOffset + sizeof(Elf_Phdr) * kMaxProgramHeaders;
     Elf_Addr load_address = file_offset;
     std::vector<Elf_Shdr> section_headers;
     section_headers.reserve(1u + sections.size());
@@ -674,7 +677,7 @@
       // Collect section headers into continuous array for convenience.
       section_headers.push_back(*header);
     }
-    Elf_Off section_headers_offset = RoundUp(file_offset, sizeof(Elf_Word));
+    Elf_Off section_headers_offset = RoundUp(file_offset, sizeof(Elf_Off));
 
     // Create program headers now that we know the layout of the whole file.
     // Each segment contains one or more sections which are mapped together.
@@ -682,8 +685,7 @@
     // PT_LOAD does the mapping.  Other PT_* types allow the program to locate
     // interesting parts of memory and their addresses overlap with PT_LOAD.
     std::vector<Elf_Phdr> program_headers;
-    program_headers.push_back(MakeProgramHeader(PT_PHDR, PF_R,
-      kProgramHeadersOffset, kProgramHeadersSize, sizeof(Elf_Word)));
+    program_headers.push_back(Elf_Phdr());  // Placeholder for PT_PHDR.
     // Create the main LOAD R segment which spans all sections up to .rodata.
     const Elf_Shdr* rodata = rodata_.GetHeader();
     program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R,
@@ -709,6 +711,9 @@
         program_headers.push_back(MakeProgramHeader(PT_GNU_EH_FRAME, PF_R, *eh_frame_hdr));
       }
     }
+    DCHECK_EQ(program_headers[0].p_type, 0u);  // Check placeholder.
+    program_headers[0] = MakeProgramHeader(PT_PHDR, PF_R,
+      kProgramHeadersOffset, program_headers.size() * sizeof(Elf_Phdr), sizeof(Elf_Off));
     CHECK_LE(program_headers.size(), kMaxProgramHeaders);
 
     // Create the main ELF header.
@@ -777,10 +782,12 @@
 
   template<typename T>
   static bool WriteArray(File* elf_file, const T* data, size_t count) {
-    DCHECK(data != nullptr);
-    if (!elf_file->WriteFully(data, count * sizeof(T))) {
-      PLOG(ERROR) << "Failed to write to file " << elf_file->GetPath();
-      return false;
+    if (count != 0) {
+      DCHECK(data != nullptr);
+      if (!elf_file->WriteFully(data, count * sizeof(T))) {
+        PLOG(ERROR) << "Failed to write to file " << elf_file->GetPath();
+        return false;
+      }
     }
     return true;
   }