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