| 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> |
| David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 20 | #include <unordered_map> |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 21 | |
| David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 22 | #include "base/array_ref.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 23 | #include "debug/dwarf/dwarf_constants.h" |
| 24 | #include "debug/elf_compilation_unit.h" |
| 25 | #include "debug/elf_debug_frame_writer.h" |
| 26 | #include "debug/elf_debug_info_writer.h" |
| 27 | #include "debug/elf_debug_line_writer.h" |
| 28 | #include "debug/elf_debug_loc_writer.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 29 | #include "debug/elf_symtab_writer.h" |
| 30 | #include "debug/method_debug_info.h" |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 31 | #include "debug/xz_utils.h" |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 32 | #include "linker/elf_builder.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 33 | #include "linker/vector_output_stream.h" |
| Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 34 | #include "oat.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace debug { |
| 38 | |
| 39 | template <typename ElfTypes> |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 40 | void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 41 | const DebugInfo& debug_info, |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 42 | dwarf::CFIFormat cfi_format, |
| 43 | bool write_oat_patches) { |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 44 | // Write .strtab and .symtab. |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 45 | WriteDebugSymbols(builder, false /* mini-debug-info */, debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 46 | |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 47 | // Write .debug_frame. |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 48 | WriteCFISection(builder, debug_info.compiled_methods, cfi_format, write_oat_patches); |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 49 | |
| David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 50 | // Group the methods into compilation units based on class. |
| 51 | std::unordered_map<const DexFile::ClassDef*, ElfCompilationUnit> class_to_compilation_unit; |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 52 | for (const MethodDebugInfo& mi : debug_info.compiled_methods) { |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 53 | if (mi.dex_file != nullptr) { |
| 54 | auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index); |
| David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 55 | ElfCompilationUnit& cu = class_to_compilation_unit[&dex_class_def]; |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 56 | cu.methods.push_back(&mi); |
| 57 | // All methods must have the same addressing mode otherwise the min/max below does not work. |
| 58 | DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative); |
| 59 | cu.is_code_address_text_relative = mi.is_code_address_text_relative; |
| 60 | cu.code_address = std::min(cu.code_address, mi.code_address); |
| 61 | 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] | 62 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 65 | // Sort compilation units to make the compiler output deterministic. |
| 66 | std::vector<ElfCompilationUnit> compilation_units; |
| 67 | compilation_units.reserve(class_to_compilation_unit.size()); |
| 68 | for (auto& it : class_to_compilation_unit) { |
| 69 | // The .debug_line section requires the methods to be sorted by code address. |
| 70 | std::stable_sort(it.second.methods.begin(), |
| 71 | it.second.methods.end(), |
| 72 | [](const MethodDebugInfo* a, const MethodDebugInfo* b) { |
| 73 | return a->code_address < b->code_address; |
| 74 | }); |
| 75 | compilation_units.push_back(std::move(it.second)); |
| 76 | } |
| 77 | std::sort(compilation_units.begin(), |
| 78 | compilation_units.end(), |
| 79 | [](ElfCompilationUnit& a, ElfCompilationUnit& b) { |
| 80 | // Sort by index of the first method within the method_infos array. |
| 81 | // This assumes that the order of method_infos is deterministic. |
| 82 | // Code address is not good for sorting due to possible duplicates. |
| 83 | return a.methods.front() < b.methods.front(); |
| 84 | }); |
| 85 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 86 | // Write .debug_line section. |
| 87 | if (!compilation_units.empty()) { |
| 88 | ElfDebugLineWriter<ElfTypes> line_writer(builder); |
| 89 | line_writer.Start(); |
| 90 | for (auto& compilation_unit : compilation_units) { |
| 91 | line_writer.WriteCompilationUnit(compilation_unit); |
| 92 | } |
| 93 | line_writer.End(write_oat_patches); |
| 94 | } |
| 95 | |
| 96 | // Write .debug_info section. |
| 97 | if (!compilation_units.empty()) { |
| 98 | ElfDebugInfoWriter<ElfTypes> info_writer(builder); |
| 99 | info_writer.Start(); |
| 100 | for (const auto& compilation_unit : compilation_units) { |
| 101 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 102 | cu_writer.Write(compilation_unit); |
| 103 | } |
| 104 | info_writer.End(write_oat_patches); |
| 105 | } |
| 106 | } |
| 107 | |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 108 | template <typename ElfTypes> |
| 109 | static std::vector<uint8_t> MakeMiniDebugInfoInternal( |
| 110 | InstructionSet isa, |
| 111 | const InstructionSetFeatures* features, |
| 112 | typename ElfTypes::Addr text_section_address, |
| 113 | size_t text_section_size, |
| 114 | typename ElfTypes::Addr dex_section_address, |
| 115 | size_t dex_section_size, |
| 116 | const DebugInfo& debug_info) { |
| 117 | std::vector<uint8_t> buffer; |
| 118 | buffer.reserve(KB); |
| 119 | linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer); |
| 120 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 121 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 122 | builder->Start(false /* write_program_headers */); |
| 123 | // Mirror ELF sections as NOBITS since the added symbols will reference them. |
| 124 | builder->GetText()->AllocateVirtualMemory(text_section_address, text_section_size); |
| 125 | if (dex_section_size != 0) { |
| 126 | builder->GetDex()->AllocateVirtualMemory(dex_section_address, dex_section_size); |
| 127 | } |
| 128 | WriteDebugSymbols(builder.get(), true /* mini-debug-info */, debug_info); |
| 129 | WriteCFISection(builder.get(), |
| 130 | debug_info.compiled_methods, |
| 131 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 132 | false /* write_oat_paches */); |
| 133 | builder->End(); |
| 134 | CHECK(builder->Good()); |
| 135 | std::vector<uint8_t> compressed_buffer; |
| 136 | compressed_buffer.reserve(buffer.size() / 4); |
| David Srbecky | cf1af73 | 2018-12-04 14:31:32 +0000 | [diff] [blame^] | 137 | XzCompress(ArrayRef<const uint8_t>(buffer), &compressed_buffer); |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 138 | return compressed_buffer; |
| 139 | } |
| 140 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 141 | std::vector<uint8_t> MakeMiniDebugInfo( |
| 142 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 143 | const InstructionSetFeatures* features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 144 | uint64_t text_section_address, |
| 145 | size_t text_section_size, |
| 146 | uint64_t dex_section_address, |
| 147 | size_t dex_section_size, |
| 148 | const DebugInfo& debug_info) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 149 | if (Is64BitInstructionSet(isa)) { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 150 | return MakeMiniDebugInfoInternal<ElfTypes64>(isa, |
| 151 | features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 152 | text_section_address, |
| 153 | text_section_size, |
| 154 | dex_section_address, |
| 155 | dex_section_size, |
| 156 | debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 157 | } else { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 158 | return MakeMiniDebugInfoInternal<ElfTypes32>(isa, |
| 159 | features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 160 | text_section_address, |
| 161 | text_section_size, |
| 162 | dex_section_address, |
| 163 | dex_section_size, |
| 164 | debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | template <typename ElfTypes> |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 169 | static std::vector<uint8_t> MakeElfFileForJITInternal( |
| David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 170 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 171 | const InstructionSetFeatures* features, |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 172 | bool mini_debug_info, |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 173 | ArrayRef<const MethodDebugInfo> method_infos) { |
| 174 | CHECK_GT(method_infos.size(), 0u); |
| 175 | uint64_t min_address = std::numeric_limits<uint64_t>::max(); |
| 176 | uint64_t max_address = 0; |
| 177 | for (const MethodDebugInfo& mi : method_infos) { |
| 178 | CHECK_EQ(mi.is_code_address_text_relative, false); |
| 179 | min_address = std::min(min_address, mi.code_address); |
| 180 | max_address = std::max(max_address, mi.code_address + mi.code_size); |
| 181 | } |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 182 | DebugInfo debug_info{}; |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 183 | debug_info.compiled_methods = method_infos; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 184 | std::vector<uint8_t> buffer; |
| 185 | buffer.reserve(KB); |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 186 | linker::VectorOutputStream out("Debug ELF file", &buffer); |
| 187 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 188 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 189 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 190 | builder->Start(false /* write_program_headers */); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 191 | if (mini_debug_info) { |
| David Srbecky | ec419db | 2018-02-16 17:24:16 +0000 | [diff] [blame] | 192 | if (method_infos.size() > 1) { |
| 193 | std::vector<uint8_t> mdi = MakeMiniDebugInfo(isa, |
| 194 | features, |
| 195 | min_address, |
| 196 | max_address - min_address, |
| 197 | /* dex_section_address */ 0, |
| 198 | /* dex_section_size */ 0, |
| 199 | debug_info); |
| 200 | builder->WriteSection(".gnu_debugdata", &mdi); |
| 201 | } else { |
| 202 | // The compression is great help for multiple methods but it is not worth it for a |
| 203 | // single method due to the overheads so skip the compression here for performance. |
| 204 | builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address); |
| 205 | WriteDebugSymbols(builder.get(), true /* mini-debug-info */, debug_info); |
| 206 | WriteCFISection(builder.get(), |
| 207 | debug_info.compiled_methods, |
| 208 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 209 | false /* write_oat_paches */); |
| 210 | } |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 211 | } else { |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 212 | builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 213 | WriteDebugInfo(builder.get(), |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 214 | debug_info, |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 215 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 216 | false /* write_oat_patches */); |
| 217 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 218 | builder->End(); |
| 219 | CHECK(builder->Good()); |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 220 | return buffer; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 223 | std::vector<uint8_t> MakeElfFileForJIT( |
| David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 224 | InstructionSet isa, |
| 225 | const InstructionSetFeatures* features, |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 226 | bool mini_debug_info, |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 227 | ArrayRef<const MethodDebugInfo> method_infos) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 228 | if (Is64BitInstructionSet(isa)) { |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 229 | return MakeElfFileForJITInternal<ElfTypes64>(isa, features, mini_debug_info, method_infos); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 230 | } else { |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 231 | return MakeElfFileForJITInternal<ElfTypes32>(isa, features, mini_debug_info, method_infos); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | template <typename ElfTypes> |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 236 | static std::vector<uint8_t> WriteDebugElfFileForClassesInternal( |
| David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 237 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 238 | const InstructionSetFeatures* features, |
| 239 | const ArrayRef<mirror::Class*>& types) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 240 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 241 | std::vector<uint8_t> buffer; |
| 242 | buffer.reserve(KB); |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 243 | linker::VectorOutputStream out("Debug ELF file", &buffer); |
| 244 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 245 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 246 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 247 | builder->Start(false /* write_program_headers */); |
| 248 | ElfDebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 249 | info_writer.Start(); |
| 250 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 251 | cu_writer.Write(types); |
| 252 | info_writer.End(false /* write_oat_patches */); |
| 253 | |
| 254 | builder->End(); |
| 255 | CHECK(builder->Good()); |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 256 | return buffer; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 259 | std::vector<uint8_t> WriteDebugElfFileForClasses(InstructionSet isa, |
| 260 | const InstructionSetFeatures* features, |
| 261 | const ArrayRef<mirror::Class*>& types) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 262 | if (Is64BitInstructionSet(isa)) { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 263 | return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 264 | } else { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 265 | return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 269 | // Explicit instantiations |
| 270 | template void WriteDebugInfo<ElfTypes32>( |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 271 | linker::ElfBuilder<ElfTypes32>* builder, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 272 | const DebugInfo& debug_info, |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 273 | dwarf::CFIFormat cfi_format, |
| 274 | bool write_oat_patches); |
| 275 | template void WriteDebugInfo<ElfTypes64>( |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 276 | linker::ElfBuilder<ElfTypes64>* builder, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 277 | const DebugInfo& debug_info, |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 278 | dwarf::CFIFormat cfi_format, |
| 279 | bool write_oat_patches); |
| 280 | |
| 281 | } // namespace debug |
| 282 | } // namespace art |