David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "elf_debug_writer.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "debug/dwarf/dwarf_constants.h" |
| 22 | #include "debug/elf_compilation_unit.h" |
| 23 | #include "debug/elf_debug_frame_writer.h" |
| 24 | #include "debug/elf_debug_info_writer.h" |
| 25 | #include "debug/elf_debug_line_writer.h" |
| 26 | #include "debug/elf_debug_loc_writer.h" |
| 27 | #include "debug/elf_gnu_debugdata_writer.h" |
| 28 | #include "debug/elf_symtab_writer.h" |
| 29 | #include "debug/method_debug_info.h" |
| 30 | #include "elf_builder.h" |
| 31 | #include "linker/vector_output_stream.h" |
| 32 | #include "utils/array_ref.h" |
| 33 | |
| 34 | namespace art { |
| 35 | namespace debug { |
| 36 | |
| 37 | template <typename ElfTypes> |
| 38 | void WriteDebugInfo(ElfBuilder<ElfTypes>* builder, |
| 39 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 40 | dwarf::CFIFormat cfi_format, |
| 41 | bool write_oat_patches) { |
| 42 | // Add methods to .symtab. |
| 43 | WriteDebugSymbols(builder, method_infos, true /* with_signature */); |
| 44 | // Generate CFI (stack unwinding information). |
| 45 | WriteCFISection(builder, method_infos, cfi_format, write_oat_patches); |
| 46 | // Write DWARF .debug_* sections. |
| 47 | WriteDebugSections(builder, method_infos, write_oat_patches); |
| 48 | } |
| 49 | |
| 50 | template<typename ElfTypes> |
| 51 | static void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
| 52 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 53 | bool write_oat_patches) { |
| 54 | // Group the methods into compilation units based on source file. |
| 55 | std::vector<ElfCompilationUnit> compilation_units; |
| 56 | const char* last_source_file = nullptr; |
| 57 | for (const MethodDebugInfo& mi : method_infos) { |
| 58 | auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index); |
| 59 | const char* source_file = mi.dex_file->GetSourceFile(dex_class_def); |
| 60 | if (compilation_units.empty() || source_file != last_source_file) { |
| 61 | compilation_units.push_back(ElfCompilationUnit()); |
| 62 | } |
| 63 | ElfCompilationUnit& cu = compilation_units.back(); |
| 64 | cu.methods.push_back(&mi); |
| 65 | cu.low_pc = std::min(cu.low_pc, mi.low_pc); |
| 66 | cu.high_pc = std::max(cu.high_pc, mi.high_pc); |
| 67 | last_source_file = source_file; |
| 68 | } |
| 69 | |
| 70 | // Write .debug_line section. |
| 71 | if (!compilation_units.empty()) { |
| 72 | ElfDebugLineWriter<ElfTypes> line_writer(builder); |
| 73 | line_writer.Start(); |
| 74 | for (auto& compilation_unit : compilation_units) { |
| 75 | line_writer.WriteCompilationUnit(compilation_unit); |
| 76 | } |
| 77 | line_writer.End(write_oat_patches); |
| 78 | } |
| 79 | |
| 80 | // Write .debug_info section. |
| 81 | if (!compilation_units.empty()) { |
| 82 | ElfDebugInfoWriter<ElfTypes> info_writer(builder); |
| 83 | info_writer.Start(); |
| 84 | for (const auto& compilation_unit : compilation_units) { |
| 85 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 86 | cu_writer.Write(compilation_unit); |
| 87 | } |
| 88 | info_writer.End(write_oat_patches); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | std::vector<uint8_t> MakeMiniDebugInfo( |
| 93 | InstructionSet isa, |
| 94 | size_t rodata_size, |
| 95 | size_t text_size, |
| 96 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
| 97 | if (Is64BitInstructionSet(isa)) { |
| 98 | return MakeMiniDebugInfoInternal<ElfTypes64>(isa, rodata_size, text_size, method_infos); |
| 99 | } else { |
| 100 | return MakeMiniDebugInfoInternal<ElfTypes32>(isa, rodata_size, text_size, method_infos); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | template <typename ElfTypes> |
| 105 | static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal( |
| 106 | const MethodDebugInfo& method_info) { |
| 107 | const InstructionSet isa = method_info.compiled_method->GetInstructionSet(); |
| 108 | std::vector<uint8_t> buffer; |
| 109 | buffer.reserve(KB); |
| 110 | VectorOutputStream out("Debug ELF file", &buffer); |
| 111 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 112 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 113 | builder->Start(false /* write_program_headers */); |
| 114 | WriteDebugInfo(builder.get(), |
| 115 | ArrayRef<const MethodDebugInfo>(&method_info, 1), |
| 116 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 117 | false /* write_oat_patches */); |
| 118 | builder->End(); |
| 119 | CHECK(builder->Good()); |
| 120 | // Make a copy of the buffer. We want to shrink it anyway. |
| 121 | uint8_t* result = new uint8_t[buffer.size()]; |
| 122 | CHECK(result != nullptr); |
| 123 | memcpy(result, buffer.data(), buffer.size()); |
| 124 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 125 | } |
| 126 | |
| 127 | ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const MethodDebugInfo& method_info) { |
| 128 | const InstructionSet isa = method_info.compiled_method->GetInstructionSet(); |
| 129 | if (Is64BitInstructionSet(isa)) { |
| 130 | return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info); |
| 131 | } else { |
| 132 | return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | template <typename ElfTypes> |
| 137 | static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal( |
| 138 | const InstructionSet isa, const ArrayRef<mirror::Class*>& types) |
| 139 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 140 | std::vector<uint8_t> buffer; |
| 141 | buffer.reserve(KB); |
| 142 | VectorOutputStream out("Debug ELF file", &buffer); |
| 143 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 144 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 145 | builder->Start(false /* write_program_headers */); |
| 146 | ElfDebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 147 | info_writer.Start(); |
| 148 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 149 | cu_writer.Write(types); |
| 150 | info_writer.End(false /* write_oat_patches */); |
| 151 | |
| 152 | builder->End(); |
| 153 | CHECK(builder->Good()); |
| 154 | // Make a copy of the buffer. We want to shrink it anyway. |
| 155 | uint8_t* result = new uint8_t[buffer.size()]; |
| 156 | CHECK(result != nullptr); |
| 157 | memcpy(result, buffer.data(), buffer.size()); |
| 158 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 159 | } |
| 160 | |
| 161 | ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa, |
| 162 | const ArrayRef<mirror::Class*>& types) { |
| 163 | if (Is64BitInstructionSet(isa)) { |
| 164 | return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types); |
| 165 | } else { |
| 166 | return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Explicit instantiations |
| 171 | template void WriteDebugInfo<ElfTypes32>( |
| 172 | ElfBuilder<ElfTypes32>* builder, |
| 173 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 174 | dwarf::CFIFormat cfi_format, |
| 175 | bool write_oat_patches); |
| 176 | template void WriteDebugInfo<ElfTypes64>( |
| 177 | ElfBuilder<ElfTypes64>* builder, |
| 178 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 179 | dwarf::CFIFormat cfi_format, |
| 180 | bool write_oat_patches); |
| 181 | |
| 182 | } // namespace debug |
| 183 | } // namespace art |