blob: 68c4e1530d8b4f36de42ffa2e8c40def3323edb4 [file] [log] [blame]
David Srbeckyc5bfa972016-02-05 15:49:10 +00001/*
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 Srbeckybe50f9a2018-12-05 10:48:42 +000019#include <type_traits>
David Srbecky56da23c2017-09-08 19:59:15 +010020#include <unordered_map>
David Srbeckybe50f9a2018-12-05 10:48:42 +000021#include <vector>
David Srbeckyc5bfa972016-02-05 15:49:10 +000022
David Brazdild9c90372016-09-14 16:53:55 +010023#include "base/array_ref.h"
David Srbecky0b21e412018-12-05 13:24:06 +000024#include "base/stl_util.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000025#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 Srbecky0b21e412018-12-05 13:24:06 +000031#include "debug/elf_debug_reader.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000032#include "debug/elf_symtab_writer.h"
33#include "debug/method_debug_info.h"
David Srbecky154c57f2018-06-03 12:00:27 +010034#include "debug/xz_utils.h"
David Srbeckybe50f9a2018-12-05 10:48:42 +000035#include "elf.h"
Vladimir Marko74527972016-11-29 15:57:32 +000036#include "linker/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000037#include "linker/vector_output_stream.h"
Andreas Gamped4901292017-05-30 18:41:34 -070038#include "oat.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000039
40namespace art {
41namespace debug {
42
David Srbeckybe50f9a2018-12-05 10:48:42 +000043using ElfRuntimeTypes = std::conditional<sizeof(void*) == 4, ElfTypes32, ElfTypes64>::type;
44
David Srbeckyc5bfa972016-02-05 15:49:10 +000045template <typename ElfTypes>
Vladimir Marko74527972016-11-29 15:57:32 +000046void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder,
David Srbecky32210b92017-12-04 14:39:21 +000047 const DebugInfo& debug_info,
David Srbeckyc5bfa972016-02-05 15:49:10 +000048 bool write_oat_patches) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000049 // Write .strtab and .symtab.
Andreas Gampe3db70682018-12-26 15:12:03 -080050 WriteDebugSymbols(builder, /* mini-debug-info= */ false, debug_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +000051
David Srbecky09c2a6b2016-03-11 17:11:44 +000052 // Write .debug_frame.
David Srbecky91b29002019-02-08 15:51:31 +000053 WriteCFISection(builder, debug_info.compiled_methods, write_oat_patches);
David Srbecky09c2a6b2016-03-11 17:11:44 +000054
David Srbecky56da23c2017-09-08 19:59:15 +010055 // Group the methods into compilation units based on class.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080056 std::unordered_map<const dex::ClassDef*, ElfCompilationUnit> class_to_compilation_unit;
David Srbecky32210b92017-12-04 14:39:21 +000057 for (const MethodDebugInfo& mi : debug_info.compiled_methods) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000058 if (mi.dex_file != nullptr) {
59 auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index);
David Srbecky56da23c2017-09-08 19:59:15 +010060 ElfCompilationUnit& cu = class_to_compilation_unit[&dex_class_def];
David Srbecky09c2a6b2016-03-11 17:11:44 +000061 cu.methods.push_back(&mi);
62 // All methods must have the same addressing mode otherwise the min/max below does not work.
63 DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative);
64 cu.is_code_address_text_relative = mi.is_code_address_text_relative;
65 cu.code_address = std::min(cu.code_address, mi.code_address);
66 cu.code_end = std::max(cu.code_end, mi.code_address + mi.code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +000067 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000068 }
69
David Srbecky56da23c2017-09-08 19:59:15 +010070 // Sort compilation units to make the compiler output deterministic.
71 std::vector<ElfCompilationUnit> compilation_units;
72 compilation_units.reserve(class_to_compilation_unit.size());
73 for (auto& it : class_to_compilation_unit) {
74 // The .debug_line section requires the methods to be sorted by code address.
75 std::stable_sort(it.second.methods.begin(),
76 it.second.methods.end(),
77 [](const MethodDebugInfo* a, const MethodDebugInfo* b) {
78 return a->code_address < b->code_address;
79 });
80 compilation_units.push_back(std::move(it.second));
81 }
82 std::sort(compilation_units.begin(),
83 compilation_units.end(),
84 [](ElfCompilationUnit& a, ElfCompilationUnit& b) {
85 // Sort by index of the first method within the method_infos array.
86 // This assumes that the order of method_infos is deterministic.
87 // Code address is not good for sorting due to possible duplicates.
88 return a.methods.front() < b.methods.front();
89 });
90
David Srbeckyc5bfa972016-02-05 15:49:10 +000091 // Write .debug_line section.
92 if (!compilation_units.empty()) {
93 ElfDebugLineWriter<ElfTypes> line_writer(builder);
94 line_writer.Start();
95 for (auto& compilation_unit : compilation_units) {
96 line_writer.WriteCompilationUnit(compilation_unit);
97 }
98 line_writer.End(write_oat_patches);
99 }
100
101 // Write .debug_info section.
102 if (!compilation_units.empty()) {
103 ElfDebugInfoWriter<ElfTypes> info_writer(builder);
104 info_writer.Start();
105 for (const auto& compilation_unit : compilation_units) {
106 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
107 cu_writer.Write(compilation_unit);
108 }
109 info_writer.End(write_oat_patches);
110 }
111}
112
David Srbecky154c57f2018-06-03 12:00:27 +0100113template <typename ElfTypes>
114static std::vector<uint8_t> MakeMiniDebugInfoInternal(
115 InstructionSet isa,
116 const InstructionSetFeatures* features,
117 typename ElfTypes::Addr text_section_address,
118 size_t text_section_size,
119 typename ElfTypes::Addr dex_section_address,
120 size_t dex_section_size,
121 const DebugInfo& debug_info) {
122 std::vector<uint8_t> buffer;
123 buffer.reserve(KB);
124 linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer);
125 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
126 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
Andreas Gampe3db70682018-12-26 15:12:03 -0800127 builder->Start(/* write_program_headers= */ false);
David Srbecky154c57f2018-06-03 12:00:27 +0100128 // Mirror ELF sections as NOBITS since the added symbols will reference them.
David Srbecky49b2b202019-02-01 13:35:48 +0000129 if (text_section_size != 0) {
130 builder->GetText()->AllocateVirtualMemory(text_section_address, text_section_size);
131 }
David Srbecky154c57f2018-06-03 12:00:27 +0100132 if (dex_section_size != 0) {
133 builder->GetDex()->AllocateVirtualMemory(dex_section_address, dex_section_size);
134 }
David Srbecky49b2b202019-02-01 13:35:48 +0000135 if (!debug_info.Empty()) {
136 WriteDebugSymbols(builder.get(), /* mini-debug-info= */ true, debug_info);
137 }
138 if (!debug_info.compiled_methods.empty()) {
David Srbecky91b29002019-02-08 15:51:31 +0000139 WriteCFISection(builder.get(), debug_info.compiled_methods, /* write_oat_patches= */ false);
David Srbecky49b2b202019-02-01 13:35:48 +0000140 }
David Srbecky154c57f2018-06-03 12:00:27 +0100141 builder->End();
142 CHECK(builder->Good());
143 std::vector<uint8_t> compressed_buffer;
144 compressed_buffer.reserve(buffer.size() / 4);
David Srbeckycf1af732018-12-04 14:31:32 +0000145 XzCompress(ArrayRef<const uint8_t>(buffer), &compressed_buffer);
David Srbecky154c57f2018-06-03 12:00:27 +0100146 return compressed_buffer;
147}
148
David Srbeckyc5bfa972016-02-05 15:49:10 +0000149std::vector<uint8_t> MakeMiniDebugInfo(
150 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000151 const InstructionSetFeatures* features,
David Srbecky32210b92017-12-04 14:39:21 +0000152 uint64_t text_section_address,
153 size_t text_section_size,
154 uint64_t dex_section_address,
155 size_t dex_section_size,
156 const DebugInfo& debug_info) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000157 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000158 return MakeMiniDebugInfoInternal<ElfTypes64>(isa,
159 features,
David Srbecky32210b92017-12-04 14:39:21 +0000160 text_section_address,
161 text_section_size,
162 dex_section_address,
163 dex_section_size,
164 debug_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000165 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000166 return MakeMiniDebugInfoInternal<ElfTypes32>(isa,
167 features,
David Srbecky32210b92017-12-04 14:39:21 +0000168 text_section_address,
169 text_section_size,
170 dex_section_address,
171 dex_section_size,
172 debug_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000173 }
174}
175
David Srbeckybe50f9a2018-12-05 10:48:42 +0000176std::vector<uint8_t> MakeElfFileForJIT(
David Srbeckyfe736b72016-03-09 11:44:44 +0000177 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000178 const InstructionSetFeatures* features,
David Srbeckyf4886df2017-12-11 16:06:29 +0000179 bool mini_debug_info,
David Srbeckybe50f9a2018-12-05 10:48:42 +0000180 const MethodDebugInfo& method_info) {
181 using ElfTypes = ElfRuntimeTypes;
182 CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa)));
183 CHECK_EQ(method_info.is_code_address_text_relative, false);
David Srbecky32210b92017-12-04 14:39:21 +0000184 DebugInfo debug_info{};
David Srbeckybe50f9a2018-12-05 10:48:42 +0000185 debug_info.compiled_methods = ArrayRef<const MethodDebugInfo>(&method_info, 1);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000186 std::vector<uint8_t> buffer;
187 buffer.reserve(KB);
Vladimir Marko74527972016-11-29 15:57:32 +0000188 linker::VectorOutputStream out("Debug ELF file", &buffer);
189 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
190 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000191 // No program headers since the ELF file is not linked and has no allocated sections.
Andreas Gampe3db70682018-12-26 15:12:03 -0800192 builder->Start(/* write_program_headers= */ false);
David Srbeckybe50f9a2018-12-05 10:48:42 +0000193 builder->GetText()->AllocateVirtualMemory(method_info.code_address, method_info.code_size);
David Srbeckyf4886df2017-12-11 16:06:29 +0000194 if (mini_debug_info) {
David Srbeckybe50f9a2018-12-05 10:48:42 +0000195 // The compression is great help for multiple methods but it is not worth it for a
196 // single method due to the overheads so skip the compression here for performance.
Andreas Gampe3db70682018-12-26 15:12:03 -0800197 WriteDebugSymbols(builder.get(), /* mini-debug-info= */ true, debug_info);
David Srbecky91b29002019-02-08 15:51:31 +0000198 WriteCFISection(builder.get(), debug_info.compiled_methods, /* write_oat_patches= */ false);
David Srbeckyf4886df2017-12-11 16:06:29 +0000199 } else {
David Srbecky91b29002019-02-08 15:51:31 +0000200 WriteDebugInfo(builder.get(), debug_info, /* write_oat_patches= */ false);
David Srbeckyf4886df2017-12-11 16:06:29 +0000201 }
David Srbeckyc5bfa972016-02-05 15:49:10 +0000202 builder->End();
203 CHECK(builder->Good());
David Srbecky0b21e412018-12-05 13:24:06 +0000204 // Verify the ELF file by reading it back using the trivial reader.
205 if (kIsDebugBuild) {
206 using Elf_Sym = typename ElfTypes::Sym;
207 using Elf_Addr = typename ElfTypes::Addr;
208 size_t num_syms = 0;
209 size_t num_cfis = 0;
210 ReadElfSymbols<ElfTypes>(
211 buffer.data(),
212 [&](Elf_Sym sym, const char*) {
213 DCHECK_EQ(sym.st_value, method_info.code_address + CompiledMethod::CodeDelta(isa));
214 DCHECK_EQ(sym.st_size, method_info.code_size);
215 num_syms++;
216 },
217 [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) {
218 DCHECK_EQ(addr, method_info.code_address);
219 DCHECK_EQ(size, method_info.code_size);
220 DCHECK_GE(opcodes.size(), method_info.cfi.size());
221 DCHECK_EQ(memcmp(opcodes.data(), method_info.cfi.data(), method_info.cfi.size()), 0);
222 num_cfis++;
223 });
224 DCHECK_EQ(num_syms, 1u);
David Srbecky51bc7522019-01-05 15:41:06 +0000225 // CFI might be missing. TODO: Ensure we have CFI for all methods.
226 DCHECK_LE(num_cfis, 1u);
David Srbecky0b21e412018-12-05 13:24:06 +0000227 }
Vladimir Marko93205e32016-04-13 11:59:46 +0100228 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000229}
230
David Srbecky0b21e412018-12-05 13:24:06 +0000231// Combine several mini-debug-info ELF files into one, while filtering some symbols.
232std::vector<uint8_t> PackElfFileForJIT(
233 InstructionSet isa,
234 const InstructionSetFeatures* features,
235 std::vector<const uint8_t*>& added_elf_files,
236 std::vector<const void*>& removed_symbols,
237 /*out*/ size_t* num_symbols) {
238 using ElfTypes = ElfRuntimeTypes;
239 using Elf_Addr = typename ElfTypes::Addr;
240 using Elf_Sym = typename ElfTypes::Sym;
241 CHECK_EQ(sizeof(Elf_Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa)));
242 const bool is64bit = Is64BitInstructionSet(isa);
243 auto is_removed_symbol = [&removed_symbols](Elf_Addr addr) {
244 const void* code_ptr = reinterpret_cast<const void*>(addr);
245 return std::binary_search(removed_symbols.begin(), removed_symbols.end(), code_ptr);
246 };
247 uint64_t min_address = std::numeric_limits<uint64_t>::max();
248 uint64_t max_address = 0;
249
250 // Produce the inner ELF file.
251 // It will contain the symbols (.symtab) and unwind information (.debug_frame).
252 std::vector<uint8_t> inner_elf_file;
253 {
254 inner_elf_file.reserve(1 * KB); // Approximate size of ELF file with a single symbol.
255 linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &inner_elf_file);
256 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
257 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
258 builder->Start(/*write_program_headers=*/ false);
259 auto* text = builder->GetText();
260 auto* strtab = builder->GetStrTab();
261 auto* symtab = builder->GetSymTab();
262 auto* debug_frame = builder->GetDebugFrame();
263 std::deque<Elf_Sym> symbols;
264 std::vector<uint8_t> debug_frame_buffer;
David Srbecky91b29002019-02-08 15:51:31 +0000265 WriteCIE(isa, &debug_frame_buffer);
David Srbecky0b21e412018-12-05 13:24:06 +0000266
267 // Write symbols names. All other data is buffered.
268 strtab->Start();
269 strtab->Write(""); // strtab should start with empty string.
270 for (const uint8_t* added_elf_file : added_elf_files) {
271 ReadElfSymbols<ElfTypes>(
272 added_elf_file,
273 [&](Elf_Sym sym, const char* name) {
274 if (is_removed_symbol(sym.st_value)) {
275 return;
276 }
277 sym.st_name = strtab->Write(name);
278 symbols.push_back(sym);
279 min_address = std::min<uint64_t>(min_address, sym.st_value);
280 max_address = std::max<uint64_t>(max_address, sym.st_value + sym.st_size);
281 },
282 [&](Elf_Addr addr, Elf_Addr size, ArrayRef<const uint8_t> opcodes) {
283 if (is_removed_symbol(addr)) {
284 return;
285 }
David Srbecky91b29002019-02-08 15:51:31 +0000286 dwarf::WriteFDE(is64bit,
287 /*section_address=*/ 0,
288 /*cie_address=*/ 0,
289 addr,
290 size,
291 opcodes,
292 debug_frame_buffer.size(),
293 &debug_frame_buffer,
294 /*patch_locations=*/ nullptr);
David Srbecky0b21e412018-12-05 13:24:06 +0000295 });
296 }
297 strtab->End();
298
299 // Create .text covering the code range. Needed for gdb to find the symbols.
300 if (max_address > min_address) {
301 text->AllocateVirtualMemory(min_address, max_address - min_address);
302 }
303
304 // Add the symbols.
305 *num_symbols = symbols.size();
306 for (; !symbols.empty(); symbols.pop_front()) {
307 symtab->Add(symbols.front(), text);
308 }
309 symtab->WriteCachedSection();
310
311 // Add the CFI/unwind section.
312 debug_frame->Start();
313 debug_frame->WriteFully(debug_frame_buffer.data(), debug_frame_buffer.size());
314 debug_frame->End();
315
316 builder->End();
317 CHECK(builder->Good());
318 }
319
320 // Produce the outer ELF file.
321 // It contains only the inner ELF file compressed as .gnu_debugdata section.
322 // This extra wrapping is not necessary but the compression saves space.
323 std::vector<uint8_t> outer_elf_file;
324 {
325 std::vector<uint8_t> gnu_debugdata;
326 gnu_debugdata.reserve(inner_elf_file.size() / 4);
327 XzCompress(ArrayRef<const uint8_t>(inner_elf_file), &gnu_debugdata);
328
329 outer_elf_file.reserve(KB + gnu_debugdata.size());
330 linker::VectorOutputStream out("Mini-debug-info ELF file for JIT", &outer_elf_file);
331 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
332 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
333 builder->Start(/*write_program_headers=*/ false);
334 if (max_address > min_address) {
335 builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address);
336 }
337 builder->WriteSection(".gnu_debugdata", &gnu_debugdata);
338 builder->End();
339 CHECK(builder->Good());
340 }
341
342 return outer_elf_file;
343}
344
David Srbeckybe50f9a2018-12-05 10:48:42 +0000345std::vector<uint8_t> WriteDebugElfFileForClasses(
David Srbeckyfe736b72016-03-09 11:44:44 +0000346 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000347 const InstructionSetFeatures* features,
348 const ArrayRef<mirror::Class*>& types)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700349 REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckybe50f9a2018-12-05 10:48:42 +0000350 using ElfTypes = ElfRuntimeTypes;
351 CHECK_EQ(sizeof(ElfTypes::Addr), static_cast<size_t>(GetInstructionSetPointerSize(isa)));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000352 std::vector<uint8_t> buffer;
353 buffer.reserve(KB);
Vladimir Marko74527972016-11-29 15:57:32 +0000354 linker::VectorOutputStream out("Debug ELF file", &buffer);
355 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
356 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000357 // No program headers since the ELF file is not linked and has no allocated sections.
Andreas Gampe3db70682018-12-26 15:12:03 -0800358 builder->Start(/* write_program_headers= */ false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000359 ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
360 info_writer.Start();
361 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
362 cu_writer.Write(types);
Andreas Gampe3db70682018-12-26 15:12:03 -0800363 info_writer.End(/* write_oat_patches= */ false);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000364
365 builder->End();
366 CHECK(builder->Good());
Vladimir Marko93205e32016-04-13 11:59:46 +0100367 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000368}
369
David Srbeckyc5bfa972016-02-05 15:49:10 +0000370// Explicit instantiations
371template void WriteDebugInfo<ElfTypes32>(
Vladimir Marko74527972016-11-29 15:57:32 +0000372 linker::ElfBuilder<ElfTypes32>* builder,
David Srbecky32210b92017-12-04 14:39:21 +0000373 const DebugInfo& debug_info,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000374 bool write_oat_patches);
375template void WriteDebugInfo<ElfTypes64>(
Vladimir Marko74527972016-11-29 15:57:32 +0000376 linker::ElfBuilder<ElfTypes64>* builder,
David Srbecky32210b92017-12-04 14:39:21 +0000377 const DebugInfo& debug_info,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000378 bool write_oat_patches);
379
380} // namespace debug
381} // namespace art