| 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 | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 24 | #include "base/stl_util.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 25 | #include "debug/dwarf/dwarf_constants.h" |
| 26 | #include "debug/elf_compilation_unit.h" |
| 27 | #include "debug/elf_debug_frame_writer.h" |
| 28 | #include "debug/elf_debug_info_writer.h" |
| 29 | #include "debug/elf_debug_line_writer.h" |
| 30 | #include "debug/elf_debug_loc_writer.h" |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 31 | #include "debug/elf_debug_reader.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 32 | #include "debug/elf_symtab_writer.h" |
| 33 | #include "debug/method_debug_info.h" |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 34 | #include "debug/xz_utils.h" |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 35 | #include "elf.h" |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 36 | #include "linker/elf_builder.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 37 | #include "linker/vector_output_stream.h" |
| Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 38 | #include "oat.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | namespace debug { |
| 42 | |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 43 | using ElfRuntimeTypes = std::conditional<sizeof(void*) == 4, ElfTypes32, ElfTypes64>::type; |
| 44 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 45 | template <typename ElfTypes> |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 46 | void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder, |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 47 | const DebugInfo& debug_info) { |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 48 | // Write .strtab and .symtab. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 49 | WriteDebugSymbols(builder, /* mini-debug-info= */ false, 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 | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 52 | WriteCFISection(builder, debug_info.compiled_methods); |
| 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. |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 55 | std::unordered_map<const dex::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 | } |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 97 | line_writer.End(); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 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 | } |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 108 | info_writer.End(); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 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)); |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 126 | builder->Start(/* write_program_headers= */ false); |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 127 | // Mirror ELF sections as NOBITS since the added symbols will reference them. |
| David Srbecky | 49b2b20 | 2019-02-01 13:35:48 +0000 | [diff] [blame] | 128 | if (text_section_size != 0) { |
| 129 | builder->GetText()->AllocateVirtualMemory(text_section_address, text_section_size); |
| 130 | } |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 131 | if (dex_section_size != 0) { |
| 132 | builder->GetDex()->AllocateVirtualMemory(dex_section_address, dex_section_size); |
| 133 | } |
| David Srbecky | 49b2b20 | 2019-02-01 13:35:48 +0000 | [diff] [blame] | 134 | if (!debug_info.Empty()) { |
| 135 | WriteDebugSymbols(builder.get(), /* mini-debug-info= */ true, debug_info); |
| 136 | } |
| 137 | if (!debug_info.compiled_methods.empty()) { |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 138 | WriteCFISection(builder.get(), debug_info.compiled_methods); |
| David Srbecky | 49b2b20 | 2019-02-01 13:35:48 +0000 | [diff] [blame] | 139 | } |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 140 | builder->End(); |
| 141 | CHECK(builder->Good()); |
| 142 | std::vector<uint8_t> compressed_buffer; |
| 143 | compressed_buffer.reserve(buffer.size() / 4); |
| David Srbecky | cf1af73 | 2018-12-04 14:31:32 +0000 | [diff] [blame] | 144 | XzCompress(ArrayRef<const uint8_t>(buffer), &compressed_buffer); |
| David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 145 | return compressed_buffer; |
| 146 | } |
| 147 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 148 | std::vector<uint8_t> MakeMiniDebugInfo( |
| 149 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 150 | const InstructionSetFeatures* features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 151 | uint64_t text_section_address, |
| 152 | size_t text_section_size, |
| 153 | uint64_t dex_section_address, |
| 154 | size_t dex_section_size, |
| 155 | const DebugInfo& debug_info) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 156 | if (Is64BitInstructionSet(isa)) { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 157 | return MakeMiniDebugInfoInternal<ElfTypes64>(isa, |
| 158 | features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 159 | text_section_address, |
| 160 | text_section_size, |
| 161 | dex_section_address, |
| 162 | dex_section_size, |
| 163 | debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 164 | } else { |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 165 | return MakeMiniDebugInfoInternal<ElfTypes32>(isa, |
| 166 | features, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 167 | text_section_address, |
| 168 | text_section_size, |
| 169 | dex_section_address, |
| 170 | dex_section_size, |
| 171 | debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 175 | std::vector<uint8_t> MakeElfFileForJIT( |
| David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 176 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 177 | const InstructionSetFeatures* features, |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 178 | bool mini_debug_info, |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 179 | const MethodDebugInfo& method_info) { |
| 180 | using ElfTypes = ElfRuntimeTypes; |
| 181 | CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
| 182 | CHECK_EQ(method_info.is_code_address_text_relative, false); |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 183 | DebugInfo debug_info{}; |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 184 | debug_info.compiled_methods = ArrayRef<const MethodDebugInfo>(&method_info, 1); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 185 | std::vector<uint8_t> buffer; |
| 186 | buffer.reserve(KB); |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 187 | linker::VectorOutputStream out("Debug ELF file", &buffer); |
| 188 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 189 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 190 | // No program headers since the ELF file is not linked and has no allocated sections. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 191 | builder->Start(/* write_program_headers= */ false); |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 192 | builder->GetText()->AllocateVirtualMemory(method_info.code_address, method_info.code_size); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 193 | if (mini_debug_info) { |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 194 | // The compression is great help for multiple methods but it is not worth it for a |
| 195 | // single method due to the overheads so skip the compression here for performance. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 196 | WriteDebugSymbols(builder.get(), /* mini-debug-info= */ true, debug_info); |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 197 | WriteCFISection(builder.get(), debug_info.compiled_methods); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 198 | } else { |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 199 | WriteDebugInfo(builder.get(), debug_info); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 200 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 201 | builder->End(); |
| 202 | CHECK(builder->Good()); |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 203 | // Verify the ELF file by reading it back using the trivial reader. |
| 204 | if (kIsDebugBuild) { |
| 205 | using Elf_Sym = typename ElfTypes::Sym; |
| 206 | using Elf_Addr = typename ElfTypes::Addr; |
| 207 | size_t num_syms = 0; |
| 208 | size_t num_cfis = 0; |
| 209 | ReadElfSymbols<ElfTypes>( |
| 210 | buffer.data(), |
| 211 | [&](Elf_Sym sym, const char*) { |
| 212 | DCHECK_EQ(sym.st_value, method_info.code_address + CompiledMethod::CodeDelta(isa)); |
| 213 | DCHECK_EQ(sym.st_size, method_info.code_size); |
| 214 | num_syms++; |
| 215 | }, |
| 216 | [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) { |
| 217 | DCHECK_EQ(addr, method_info.code_address); |
| 218 | DCHECK_EQ(size, method_info.code_size); |
| 219 | DCHECK_GE(opcodes.size(), method_info.cfi.size()); |
| 220 | DCHECK_EQ(memcmp(opcodes.data(), method_info.cfi.data(), method_info.cfi.size()), 0); |
| 221 | num_cfis++; |
| 222 | }); |
| 223 | DCHECK_EQ(num_syms, 1u); |
| David Srbecky | 51bc752 | 2019-01-05 15:41:06 +0000 | [diff] [blame] | 224 | // CFI might be missing. TODO: Ensure we have CFI for all methods. |
| 225 | DCHECK_LE(num_cfis, 1u); |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 226 | } |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 227 | return buffer; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 230 | // Combine several mini-debug-info ELF files into one, while filtering some symbols. |
| 231 | std::vector<uint8_t> PackElfFileForJIT( |
| 232 | InstructionSet isa, |
| 233 | const InstructionSetFeatures* features, |
| 234 | std::vector<const uint8_t*>& added_elf_files, |
| 235 | std::vector<const void*>& removed_symbols, |
| 236 | /*out*/ size_t* num_symbols) { |
| 237 | using ElfTypes = ElfRuntimeTypes; |
| 238 | using Elf_Addr = typename ElfTypes::Addr; |
| 239 | using Elf_Sym = typename ElfTypes::Sym; |
| 240 | CHECK_EQ(sizeof(Elf_Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
| 241 | const bool is64bit = Is64BitInstructionSet(isa); |
| 242 | auto is_removed_symbol = [&removed_symbols](Elf_Addr addr) { |
| 243 | const void* code_ptr = reinterpret_cast<const void*>(addr); |
| 244 | return std::binary_search(removed_symbols.begin(), removed_symbols.end(), code_ptr); |
| 245 | }; |
| 246 | uint64_t min_address = std::numeric_limits<uint64_t>::max(); |
| 247 | uint64_t max_address = 0; |
| 248 | |
| 249 | // Produce the inner ELF file. |
| 250 | // It will contain the symbols (.symtab) and unwind information (.debug_frame). |
| 251 | std::vector<uint8_t> inner_elf_file; |
| 252 | { |
| 253 | inner_elf_file.reserve(1 * KB); // Approximate size of ELF file with a single symbol. |
| 254 | linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &inner_elf_file); |
| 255 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 256 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 257 | builder->Start(/*write_program_headers=*/ false); |
| 258 | auto* text = builder->GetText(); |
| 259 | auto* strtab = builder->GetStrTab(); |
| 260 | auto* symtab = builder->GetSymTab(); |
| 261 | auto* debug_frame = builder->GetDebugFrame(); |
| 262 | std::deque<Elf_Sym> symbols; |
| 263 | std::vector<uint8_t> debug_frame_buffer; |
| David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 264 | WriteCIE(isa, &debug_frame_buffer); |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 265 | |
| 266 | // Write symbols names. All other data is buffered. |
| 267 | strtab->Start(); |
| 268 | strtab->Write(""); // strtab should start with empty string. |
| 269 | for (const uint8_t* added_elf_file : added_elf_files) { |
| 270 | ReadElfSymbols<ElfTypes>( |
| 271 | added_elf_file, |
| 272 | [&](Elf_Sym sym, const char* name) { |
| 273 | if (is_removed_symbol(sym.st_value)) { |
| 274 | return; |
| 275 | } |
| 276 | sym.st_name = strtab->Write(name); |
| 277 | symbols.push_back(sym); |
| 278 | min_address = std::min<uint64_t>(min_address, sym.st_value); |
| 279 | max_address = std::max<uint64_t>(max_address, sym.st_value + sym.st_size); |
| 280 | }, |
| 281 | [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) { |
| 282 | if (is_removed_symbol(addr)) { |
| 283 | return; |
| 284 | } |
| David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 285 | dwarf::WriteFDE(is64bit, |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 286 | /* cie_pointer= */ 0, |
| David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 287 | addr, |
| 288 | size, |
| 289 | opcodes, |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 290 | &debug_frame_buffer); |
| David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 291 | }); |
| 292 | } |
| 293 | strtab->End(); |
| 294 | |
| 295 | // Create .text covering the code range. Needed for gdb to find the symbols. |
| 296 | if (max_address > min_address) { |
| 297 | text->AllocateVirtualMemory(min_address, max_address - min_address); |
| 298 | } |
| 299 | |
| 300 | // Add the symbols. |
| 301 | *num_symbols = symbols.size(); |
| 302 | for (; !symbols.empty(); symbols.pop_front()) { |
| 303 | symtab->Add(symbols.front(), text); |
| 304 | } |
| 305 | symtab->WriteCachedSection(); |
| 306 | |
| 307 | // Add the CFI/unwind section. |
| 308 | debug_frame->Start(); |
| 309 | debug_frame->WriteFully(debug_frame_buffer.data(), debug_frame_buffer.size()); |
| 310 | debug_frame->End(); |
| 311 | |
| 312 | builder->End(); |
| 313 | CHECK(builder->Good()); |
| 314 | } |
| 315 | |
| 316 | // Produce the outer ELF file. |
| 317 | // It contains only the inner ELF file compressed as .gnu_debugdata section. |
| 318 | // This extra wrapping is not necessary but the compression saves space. |
| 319 | std::vector<uint8_t> outer_elf_file; |
| 320 | { |
| 321 | std::vector<uint8_t> gnu_debugdata; |
| 322 | gnu_debugdata.reserve(inner_elf_file.size() / 4); |
| 323 | XzCompress(ArrayRef<const uint8_t>(inner_elf_file), &gnu_debugdata); |
| 324 | |
| 325 | outer_elf_file.reserve(KB + gnu_debugdata.size()); |
| 326 | linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &outer_elf_file); |
| 327 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 328 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 329 | builder->Start(/*write_program_headers=*/ false); |
| 330 | if (max_address > min_address) { |
| 331 | builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address); |
| 332 | } |
| 333 | builder->WriteSection(".gnu_debugdata", &gnu_debugdata); |
| 334 | builder->End(); |
| 335 | CHECK(builder->Good()); |
| 336 | } |
| 337 | |
| 338 | return outer_elf_file; |
| 339 | } |
| 340 | |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 341 | std::vector<uint8_t> WriteDebugElfFileForClasses( |
| David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 342 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 343 | const InstructionSetFeatures* features, |
| 344 | const ArrayRef<mirror::Class*>& types) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 345 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 346 | using ElfTypes = ElfRuntimeTypes; |
| 347 | CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 348 | std::vector<uint8_t> buffer; |
| 349 | buffer.reserve(KB); |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 350 | linker::VectorOutputStream out("Debug ELF file", &buffer); |
| 351 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 352 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 353 | // No program headers since the ELF file is not linked and has no allocated sections. |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 354 | builder->Start(/* write_program_headers= */ false); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 355 | ElfDebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 356 | info_writer.Start(); |
| 357 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 358 | cu_writer.Write(types); |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 359 | info_writer.End(); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 360 | |
| 361 | builder->End(); |
| 362 | CHECK(builder->Good()); |
| Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 363 | return buffer; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 366 | // Explicit instantiations |
| 367 | template void WriteDebugInfo<ElfTypes32>( |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 368 | linker::ElfBuilder<ElfTypes32>* builder, |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 369 | const DebugInfo& debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 370 | template void WriteDebugInfo<ElfTypes64>( |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 371 | linker::ElfBuilder<ElfTypes64>* builder, |
| David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame^] | 372 | const DebugInfo& debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 373 | |
| 374 | } // namespace debug |
| 375 | } // namespace art |