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 | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 47 | const DebugInfo& debug_info, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 48 | dwarf::CFIFormat cfi_format, |
| 49 | bool write_oat_patches) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 50 | // Write .strtab and .symtab. |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 51 | WriteDebugSymbols(builder, false /* mini-debug-info */, debug_info); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 52 | |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 53 | // Write .debug_frame. |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 54 | WriteCFISection(builder, debug_info.compiled_methods, cfi_format, write_oat_patches); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 55 | |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 56 | // Group the methods into compilation units based on class. |
| 57 | std::unordered_map<const DexFile::ClassDef*, ElfCompilationUnit> class_to_compilation_unit; |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 58 | for (const MethodDebugInfo& mi : debug_info.compiled_methods) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 59 | if (mi.dex_file != nullptr) { |
| 60 | auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index); |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 61 | ElfCompilationUnit& cu = class_to_compilation_unit[&dex_class_def]; |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 62 | cu.methods.push_back(&mi); |
| 63 | // All methods must have the same addressing mode otherwise the min/max below does not work. |
| 64 | DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative); |
| 65 | cu.is_code_address_text_relative = mi.is_code_address_text_relative; |
| 66 | cu.code_address = std::min(cu.code_address, mi.code_address); |
| 67 | 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] | 68 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 69 | } |
| 70 | |
David Srbecky | 56da23c | 2017-09-08 19:59:15 +0100 | [diff] [blame] | 71 | // Sort compilation units to make the compiler output deterministic. |
| 72 | std::vector<ElfCompilationUnit> compilation_units; |
| 73 | compilation_units.reserve(class_to_compilation_unit.size()); |
| 74 | for (auto& it : class_to_compilation_unit) { |
| 75 | // The .debug_line section requires the methods to be sorted by code address. |
| 76 | std::stable_sort(it.second.methods.begin(), |
| 77 | it.second.methods.end(), |
| 78 | [](const MethodDebugInfo* a, const MethodDebugInfo* b) { |
| 79 | return a->code_address < b->code_address; |
| 80 | }); |
| 81 | compilation_units.push_back(std::move(it.second)); |
| 82 | } |
| 83 | std::sort(compilation_units.begin(), |
| 84 | compilation_units.end(), |
| 85 | [](ElfCompilationUnit& a, ElfCompilationUnit& b) { |
| 86 | // Sort by index of the first method within the method_infos array. |
| 87 | // This assumes that the order of method_infos is deterministic. |
| 88 | // Code address is not good for sorting due to possible duplicates. |
| 89 | return a.methods.front() < b.methods.front(); |
| 90 | }); |
| 91 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 92 | // Write .debug_line section. |
| 93 | if (!compilation_units.empty()) { |
| 94 | ElfDebugLineWriter<ElfTypes> line_writer(builder); |
| 95 | line_writer.Start(); |
| 96 | for (auto& compilation_unit : compilation_units) { |
| 97 | line_writer.WriteCompilationUnit(compilation_unit); |
| 98 | } |
| 99 | line_writer.End(write_oat_patches); |
| 100 | } |
| 101 | |
| 102 | // Write .debug_info section. |
| 103 | if (!compilation_units.empty()) { |
| 104 | ElfDebugInfoWriter<ElfTypes> info_writer(builder); |
| 105 | info_writer.Start(); |
| 106 | for (const auto& compilation_unit : compilation_units) { |
| 107 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 108 | cu_writer.Write(compilation_unit); |
| 109 | } |
| 110 | info_writer.End(write_oat_patches); |
| 111 | } |
| 112 | } |
| 113 | |
David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 114 | template <typename ElfTypes> |
| 115 | static std::vector<uint8_t> MakeMiniDebugInfoInternal( |
| 116 | InstructionSet isa, |
| 117 | const InstructionSetFeatures* features, |
| 118 | typename ElfTypes::Addr text_section_address, |
| 119 | size_t text_section_size, |
| 120 | typename ElfTypes::Addr dex_section_address, |
| 121 | size_t dex_section_size, |
| 122 | const DebugInfo& debug_info) { |
| 123 | std::vector<uint8_t> buffer; |
| 124 | buffer.reserve(KB); |
| 125 | linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer); |
| 126 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 127 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 128 | builder->Start(false /* write_program_headers */); |
| 129 | // Mirror ELF sections as NOBITS since the added symbols will reference them. |
| 130 | builder->GetText()->AllocateVirtualMemory(text_section_address, text_section_size); |
| 131 | if (dex_section_size != 0) { |
| 132 | builder->GetDex()->AllocateVirtualMemory(dex_section_address, dex_section_size); |
| 133 | } |
| 134 | WriteDebugSymbols(builder.get(), true /* mini-debug-info */, debug_info); |
| 135 | WriteCFISection(builder.get(), |
| 136 | debug_info.compiled_methods, |
| 137 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 138 | false /* write_oat_paches */); |
| 139 | builder->End(); |
| 140 | CHECK(builder->Good()); |
| 141 | std::vector<uint8_t> compressed_buffer; |
| 142 | compressed_buffer.reserve(buffer.size() / 4); |
David Srbecky | cf1af73 | 2018-12-04 14:31:32 +0000 | [diff] [blame] | 143 | XzCompress(ArrayRef<const uint8_t>(buffer), &compressed_buffer); |
David Srbecky | 154c57f | 2018-06-03 12:00:27 +0100 | [diff] [blame] | 144 | return compressed_buffer; |
| 145 | } |
| 146 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 147 | std::vector<uint8_t> MakeMiniDebugInfo( |
| 148 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 149 | const InstructionSetFeatures* features, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 150 | uint64_t text_section_address, |
| 151 | size_t text_section_size, |
| 152 | uint64_t dex_section_address, |
| 153 | size_t dex_section_size, |
| 154 | const DebugInfo& debug_info) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 155 | if (Is64BitInstructionSet(isa)) { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 156 | return MakeMiniDebugInfoInternal<ElfTypes64>(isa, |
| 157 | features, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 158 | text_section_address, |
| 159 | text_section_size, |
| 160 | dex_section_address, |
| 161 | dex_section_size, |
| 162 | debug_info); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 163 | } else { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 164 | return MakeMiniDebugInfoInternal<ElfTypes32>(isa, |
| 165 | features, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 166 | text_section_address, |
| 167 | text_section_size, |
| 168 | dex_section_address, |
| 169 | dex_section_size, |
| 170 | debug_info); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 174 | std::vector<uint8_t> MakeElfFileForJIT( |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 175 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 176 | const InstructionSetFeatures* features, |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 177 | bool mini_debug_info, |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 178 | const MethodDebugInfo& method_info) { |
| 179 | using ElfTypes = ElfRuntimeTypes; |
| 180 | CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
| 181 | CHECK_EQ(method_info.is_code_address_text_relative, false); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 182 | DebugInfo debug_info{}; |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 183 | debug_info.compiled_methods = ArrayRef<const MethodDebugInfo>(&method_info, 1); |
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 | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 191 | builder->GetText()->AllocateVirtualMemory(method_info.code_address, method_info.code_size); |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 192 | if (mini_debug_info) { |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 193 | // The compression is great help for multiple methods but it is not worth it for a |
| 194 | // single method due to the overheads so skip the compression here for performance. |
| 195 | WriteDebugSymbols(builder.get(), true /* mini-debug-info */, debug_info); |
| 196 | WriteCFISection(builder.get(), |
| 197 | debug_info.compiled_methods, |
| 198 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 199 | false /* write_oat_paches */); |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 200 | } else { |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 201 | WriteDebugInfo(builder.get(), |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 202 | debug_info, |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 203 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 204 | false /* write_oat_patches */); |
| 205 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 206 | builder->End(); |
| 207 | CHECK(builder->Good()); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 208 | // Verify the ELF file by reading it back using the trivial reader. |
| 209 | if (kIsDebugBuild) { |
| 210 | using Elf_Sym = typename ElfTypes::Sym; |
| 211 | using Elf_Addr = typename ElfTypes::Addr; |
| 212 | size_t num_syms = 0; |
| 213 | size_t num_cfis = 0; |
| 214 | ReadElfSymbols<ElfTypes>( |
| 215 | buffer.data(), |
| 216 | [&](Elf_Sym sym, const char*) { |
| 217 | DCHECK_EQ(sym.st_value, method_info.code_address + CompiledMethod::CodeDelta(isa)); |
| 218 | DCHECK_EQ(sym.st_size, method_info.code_size); |
| 219 | num_syms++; |
| 220 | }, |
| 221 | [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) { |
| 222 | DCHECK_EQ(addr, method_info.code_address); |
| 223 | DCHECK_EQ(size, method_info.code_size); |
| 224 | DCHECK_GE(opcodes.size(), method_info.cfi.size()); |
| 225 | DCHECK_EQ(memcmp(opcodes.data(), method_info.cfi.data(), method_info.cfi.size()), 0); |
| 226 | num_cfis++; |
| 227 | }); |
| 228 | DCHECK_EQ(num_syms, 1u); |
David Srbecky | 51bc752 | 2019-01-05 15:41:06 +0000 | [diff] [blame^] | 229 | // CFI might be missing. TODO: Ensure we have CFI for all methods. |
| 230 | DCHECK_LE(num_cfis, 1u); |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 231 | } |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 232 | return buffer; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
David Srbecky | 0b21e41 | 2018-12-05 13:24:06 +0000 | [diff] [blame] | 235 | // Combine several mini-debug-info ELF files into one, while filtering some symbols. |
| 236 | std::vector<uint8_t> PackElfFileForJIT( |
| 237 | InstructionSet isa, |
| 238 | const InstructionSetFeatures* features, |
| 239 | std::vector<const uint8_t*>& added_elf_files, |
| 240 | std::vector<const void*>& removed_symbols, |
| 241 | /*out*/ size_t* num_symbols) { |
| 242 | using ElfTypes = ElfRuntimeTypes; |
| 243 | using Elf_Addr = typename ElfTypes::Addr; |
| 244 | using Elf_Sym = typename ElfTypes::Sym; |
| 245 | CHECK_EQ(sizeof(Elf_Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
| 246 | const bool is64bit = Is64BitInstructionSet(isa); |
| 247 | auto is_removed_symbol = [&removed_symbols](Elf_Addr addr) { |
| 248 | const void* code_ptr = reinterpret_cast<const void*>(addr); |
| 249 | return std::binary_search(removed_symbols.begin(), removed_symbols.end(), code_ptr); |
| 250 | }; |
| 251 | uint64_t min_address = std::numeric_limits<uint64_t>::max(); |
| 252 | uint64_t max_address = 0; |
| 253 | |
| 254 | // Produce the inner ELF file. |
| 255 | // It will contain the symbols (.symtab) and unwind information (.debug_frame). |
| 256 | std::vector<uint8_t> inner_elf_file; |
| 257 | { |
| 258 | inner_elf_file.reserve(1 * KB); // Approximate size of ELF file with a single symbol. |
| 259 | linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &inner_elf_file); |
| 260 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 261 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 262 | builder->Start(/*write_program_headers=*/ false); |
| 263 | auto* text = builder->GetText(); |
| 264 | auto* strtab = builder->GetStrTab(); |
| 265 | auto* symtab = builder->GetSymTab(); |
| 266 | auto* debug_frame = builder->GetDebugFrame(); |
| 267 | std::deque<Elf_Sym> symbols; |
| 268 | std::vector<uint8_t> debug_frame_buffer; |
| 269 | WriteCIE(isa, dwarf::DW_DEBUG_FRAME_FORMAT, &debug_frame_buffer); |
| 270 | |
| 271 | // Write symbols names. All other data is buffered. |
| 272 | strtab->Start(); |
| 273 | strtab->Write(""); // strtab should start with empty string. |
| 274 | for (const uint8_t* added_elf_file : added_elf_files) { |
| 275 | ReadElfSymbols<ElfTypes>( |
| 276 | added_elf_file, |
| 277 | [&](Elf_Sym sym, const char* name) { |
| 278 | if (is_removed_symbol(sym.st_value)) { |
| 279 | return; |
| 280 | } |
| 281 | sym.st_name = strtab->Write(name); |
| 282 | symbols.push_back(sym); |
| 283 | min_address = std::min<uint64_t>(min_address, sym.st_value); |
| 284 | max_address = std::max<uint64_t>(max_address, sym.st_value + sym.st_size); |
| 285 | }, |
| 286 | [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) { |
| 287 | if (is_removed_symbol(addr)) { |
| 288 | return; |
| 289 | } |
| 290 | WriteFDE(is64bit, |
| 291 | /*section_address=*/ 0, |
| 292 | /*cie_address=*/ 0, |
| 293 | addr, |
| 294 | size, |
| 295 | opcodes, |
| 296 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 297 | debug_frame_buffer.size(), |
| 298 | &debug_frame_buffer, |
| 299 | /*patch_locations=*/ nullptr); |
| 300 | }); |
| 301 | } |
| 302 | strtab->End(); |
| 303 | |
| 304 | // Create .text covering the code range. Needed for gdb to find the symbols. |
| 305 | if (max_address > min_address) { |
| 306 | text->AllocateVirtualMemory(min_address, max_address - min_address); |
| 307 | } |
| 308 | |
| 309 | // Add the symbols. |
| 310 | *num_symbols = symbols.size(); |
| 311 | for (; !symbols.empty(); symbols.pop_front()) { |
| 312 | symtab->Add(symbols.front(), text); |
| 313 | } |
| 314 | symtab->WriteCachedSection(); |
| 315 | |
| 316 | // Add the CFI/unwind section. |
| 317 | debug_frame->Start(); |
| 318 | debug_frame->WriteFully(debug_frame_buffer.data(), debug_frame_buffer.size()); |
| 319 | debug_frame->End(); |
| 320 | |
| 321 | builder->End(); |
| 322 | CHECK(builder->Good()); |
| 323 | } |
| 324 | |
| 325 | // Produce the outer ELF file. |
| 326 | // It contains only the inner ELF file compressed as .gnu_debugdata section. |
| 327 | // This extra wrapping is not necessary but the compression saves space. |
| 328 | std::vector<uint8_t> outer_elf_file; |
| 329 | { |
| 330 | std::vector<uint8_t> gnu_debugdata; |
| 331 | gnu_debugdata.reserve(inner_elf_file.size() / 4); |
| 332 | XzCompress(ArrayRef<const uint8_t>(inner_elf_file), &gnu_debugdata); |
| 333 | |
| 334 | outer_elf_file.reserve(KB + gnu_debugdata.size()); |
| 335 | linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &outer_elf_file); |
| 336 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 337 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| 338 | builder->Start(/*write_program_headers=*/ false); |
| 339 | if (max_address > min_address) { |
| 340 | builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address); |
| 341 | } |
| 342 | builder->WriteSection(".gnu_debugdata", &gnu_debugdata); |
| 343 | builder->End(); |
| 344 | CHECK(builder->Good()); |
| 345 | } |
| 346 | |
| 347 | return outer_elf_file; |
| 348 | } |
| 349 | |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 350 | std::vector<uint8_t> WriteDebugElfFileForClasses( |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 351 | InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 352 | const InstructionSetFeatures* features, |
| 353 | const ArrayRef<mirror::Class*>& types) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 354 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Srbecky | be50f9a | 2018-12-05 10:48:42 +0000 | [diff] [blame] | 355 | using ElfTypes = ElfRuntimeTypes; |
| 356 | CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa))); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 357 | std::vector<uint8_t> buffer; |
| 358 | buffer.reserve(KB); |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 359 | linker::VectorOutputStream out("Debug ELF file", &buffer); |
| 360 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 361 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 362 | // No program headers since the ELF file is not linked and has no allocated sections. |
| 363 | builder->Start(false /* write_program_headers */); |
| 364 | ElfDebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 365 | info_writer.Start(); |
| 366 | ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer); |
| 367 | cu_writer.Write(types); |
| 368 | info_writer.End(false /* write_oat_patches */); |
| 369 | |
| 370 | builder->End(); |
| 371 | CHECK(builder->Good()); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 372 | return buffer; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 373 | } |
| 374 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 375 | // Explicit instantiations |
| 376 | template void WriteDebugInfo<ElfTypes32>( |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 377 | linker::ElfBuilder<ElfTypes32>* builder, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 378 | const DebugInfo& debug_info, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 379 | dwarf::CFIFormat cfi_format, |
| 380 | bool write_oat_patches); |
| 381 | template void WriteDebugInfo<ElfTypes64>( |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 382 | linker::ElfBuilder<ElfTypes64>* builder, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 383 | const DebugInfo& debug_info, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 384 | dwarf::CFIFormat cfi_format, |
| 385 | bool write_oat_patches); |
| 386 | |
| 387 | } // namespace debug |
| 388 | } // namespace art |