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); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 65 | // All methods must have the same addressing mode otherwise the min/max below does not work. |
| 66 | DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative); |
| 67 | cu.is_code_address_text_relative = mi.is_code_address_text_relative; |
| 68 | cu.code_address = std::min(cu.code_address, mi.code_address); |
| 69 | cu.code_end = std::max(cu.code_end, mi.code_address + mi.code_size); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 70 | last_source_file = source_file; |
| 71 | } |
| 72 | |
| 73 | // Write .debug_line section. |
| 74 | if (!compilation_units.empty()) { |
| 75 | ElfDebugLineWriter<ElfTypes> line_writer(builder); |
| 76 | line_writer.Start(); |
| 77 | for (auto& compilation_unit : compilation_units) { |
| 78 | line_writer.WriteCompilationUnit(compilation_unit); |
| 79 | } |
| 80 | line_writer.End(write_oat_patches); |
| 81 | } |
| 82 | |
| 83 | // Write .debug_info section. |
| 84 | if (!compilation_units.empty()) { |
| 85 | ElfDebugInfoWriter<ElfTypes> info_writer(builder); |
| 86 | info_writer.Start(); |
| 87 | for (const auto& compilation_unit : compilation_units) { |
| 88 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 89 | cu_writer.Write(compilation_unit); |
| 90 | } |
| 91 | info_writer.End(write_oat_patches); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | std::vector<uint8_t> MakeMiniDebugInfo( |
| 96 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 97 | const InstructionSetFeatures* features, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 98 | size_t rodata_size, |
| 99 | size_t text_size, |
| 100 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
| 101 | if (Is64BitInstructionSet(isa)) { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 102 | return MakeMiniDebugInfoInternal<ElfTypes64>(isa, |
| 103 | features, |
| 104 | rodata_size, |
| 105 | text_size, |
| 106 | method_infos); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 107 | } else { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 108 | return MakeMiniDebugInfoInternal<ElfTypes32>(isa, |
| 109 | features, |
| 110 | rodata_size, |
| 111 | text_size, |
| 112 | method_infos); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | template <typename ElfTypes> |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 117 | static ArrayRef<const uint8_t> WriteDebugElfFileForMethodsInternal( |
| 118 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 119 | const InstructionSetFeatures* features, |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 120 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 121 | std::vector<uint8_t> buffer; |
| 122 | buffer.reserve(KB); |
| 123 | VectorOutputStream out("Debug ELF file", &buffer); |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 124 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out)); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 125 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 126 | builder->Start(false /* write_program_headers */); |
| 127 | WriteDebugInfo(builder.get(), |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 128 | method_infos, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 129 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 130 | false /* write_oat_patches */); |
| 131 | builder->End(); |
| 132 | CHECK(builder->Good()); |
| 133 | // Make a copy of the buffer. We want to shrink it anyway. |
| 134 | uint8_t* result = new uint8_t[buffer.size()]; |
| 135 | CHECK(result != nullptr); |
| 136 | memcpy(result, buffer.data(), buffer.size()); |
| 137 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 138 | } |
| 139 | |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 140 | ArrayRef<const uint8_t> WriteDebugElfFileForMethods( |
| 141 | InstructionSet isa, |
| 142 | const InstructionSetFeatures* features, |
| 143 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 144 | if (Is64BitInstructionSet(isa)) { |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 145 | return WriteDebugElfFileForMethodsInternal<ElfTypes64>(isa, features, method_infos); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 146 | } else { |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 147 | return WriteDebugElfFileForMethodsInternal<ElfTypes32>(isa, features, method_infos); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | template <typename ElfTypes> |
| 152 | static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal( |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 153 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 154 | const InstructionSetFeatures* features, |
| 155 | const ArrayRef<mirror::Class*>& types) |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 156 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 157 | std::vector<uint8_t> buffer; |
| 158 | buffer.reserve(KB); |
| 159 | VectorOutputStream out("Debug ELF file", &buffer); |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 160 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out)); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 161 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 162 | builder->Start(false /* write_program_headers */); |
| 163 | ElfDebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 164 | info_writer.Start(); |
| 165 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 166 | cu_writer.Write(types); |
| 167 | info_writer.End(false /* write_oat_patches */); |
| 168 | |
| 169 | builder->End(); |
| 170 | CHECK(builder->Good()); |
| 171 | // Make a copy of the buffer. We want to shrink it anyway. |
| 172 | uint8_t* result = new uint8_t[buffer.size()]; |
| 173 | CHECK(result != nullptr); |
| 174 | memcpy(result, buffer.data(), buffer.size()); |
| 175 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 176 | } |
| 177 | |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 178 | ArrayRef<const uint8_t> WriteDebugElfFileForClasses(InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 179 | const InstructionSetFeatures* features, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 180 | const ArrayRef<mirror::Class*>& types) { |
| 181 | if (Is64BitInstructionSet(isa)) { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 182 | return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 183 | } else { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 184 | return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Explicit instantiations |
| 189 | template void WriteDebugInfo<ElfTypes32>( |
| 190 | ElfBuilder<ElfTypes32>* builder, |
| 191 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 192 | dwarf::CFIFormat cfi_format, |
| 193 | bool write_oat_patches); |
| 194 | template void WriteDebugInfo<ElfTypes64>( |
| 195 | ElfBuilder<ElfTypes64>* builder, |
| 196 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 197 | dwarf::CFIFormat cfi_format, |
| 198 | bool write_oat_patches); |
| 199 | |
| 200 | } // namespace debug |
| 201 | } // namespace art |