blob: e8c3b6e43217f564d9b5493bcc04c2fb15c91208 [file] [log] [blame]
David Srbecky3b9d57a2015-04-10 00:22:14 +01001/*
2 * Copyright (C) 2015 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_writer_debug.h"
18
David Srbecky626a1662015-04-12 13:12:26 +010019#include <unordered_set>
20
David Srbecky3b9d57a2015-04-10 00:22:14 +010021#include "compiled_method.h"
22#include "driver/compiler_driver.h"
23#include "dex_file-inl.h"
24#include "dwarf/headers.h"
25#include "dwarf/register.h"
26#include "oat_writer.h"
27
28namespace art {
29namespace dwarf {
30
David Srbecky527c9c72015-04-17 21:14:10 +010031static void WriteEhFrameCIE(InstructionSet isa,
32 ExceptionHeaderValueApplication addr_type,
33 std::vector<uint8_t>* eh_frame) {
David Srbecky3b9d57a2015-04-10 00:22:14 +010034 // Scratch registers should be marked as undefined. This tells the
35 // debugger that its value in the previous frame is not recoverable.
36 bool is64bit = Is64BitInstructionSet(isa);
37 switch (isa) {
38 case kArm:
39 case kThumb2: {
40 DebugFrameOpCodeWriter<> opcodes;
41 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
42 // core registers.
43 for (int reg = 0; reg < 13; reg++) {
44 if (reg < 4 || reg == 12) {
45 opcodes.Undefined(Reg::ArmCore(reg));
46 } else {
47 opcodes.SameValue(Reg::ArmCore(reg));
48 }
49 }
50 // fp registers.
51 for (int reg = 0; reg < 32; reg++) {
52 if (reg < 16) {
53 opcodes.Undefined(Reg::ArmFp(reg));
54 } else {
55 opcodes.SameValue(Reg::ArmFp(reg));
56 }
57 }
David Srbecky527c9c72015-04-17 21:14:10 +010058 auto return_reg = Reg::ArmCore(14); // R14(LR).
59 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010060 return;
61 }
62 case kArm64: {
63 DebugFrameOpCodeWriter<> opcodes;
64 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
65 // core registers.
66 for (int reg = 0; reg < 30; reg++) {
67 if (reg < 8 || reg == 16 || reg == 17) {
68 opcodes.Undefined(Reg::Arm64Core(reg));
69 } else {
70 opcodes.SameValue(Reg::Arm64Core(reg));
71 }
72 }
73 // fp registers.
74 for (int reg = 0; reg < 32; reg++) {
75 if (reg < 8 || reg >= 16) {
76 opcodes.Undefined(Reg::Arm64Fp(reg));
77 } else {
78 opcodes.SameValue(Reg::Arm64Fp(reg));
79 }
80 }
David Srbecky527c9c72015-04-17 21:14:10 +010081 auto return_reg = Reg::Arm64Core(30); // R30(LR).
82 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010083 return;
84 }
85 case kMips:
86 case kMips64: {
87 DebugFrameOpCodeWriter<> opcodes;
88 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
89 // core registers.
90 for (int reg = 1; reg < 26; reg++) {
91 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
92 opcodes.Undefined(Reg::MipsCore(reg));
93 } else {
94 opcodes.SameValue(Reg::MipsCore(reg));
95 }
96 }
David Srbecky527c9c72015-04-17 21:14:10 +010097 auto return_reg = Reg::MipsCore(31); // R31(RA).
98 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +010099 return;
100 }
101 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100102 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
103 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100104 DebugFrameOpCodeWriter<> opcodes;
105 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
106 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
107 // core registers.
108 for (int reg = 0; reg < 8; reg++) {
109 if (reg <= 3) {
110 opcodes.Undefined(Reg::X86Core(reg));
111 } else if (reg == 4) {
112 // Stack pointer.
113 } else {
114 opcodes.SameValue(Reg::X86Core(reg));
115 }
116 }
117 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100118 if (generate_opcodes_for_x86_fp) {
119 for (int reg = 0; reg < 8; reg++) {
120 opcodes.Undefined(Reg::X86Fp(reg));
121 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100122 }
David Srbecky527c9c72015-04-17 21:14:10 +0100123 auto return_reg = Reg::X86Core(8); // R8(EIP).
124 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100125 return;
126 }
127 case kX86_64: {
128 DebugFrameOpCodeWriter<> opcodes;
129 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
130 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
131 // core registers.
132 for (int reg = 0; reg < 16; reg++) {
133 if (reg == 4) {
134 // Stack pointer.
135 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
136 opcodes.Undefined(Reg::X86_64Core(reg));
137 } else {
138 opcodes.SameValue(Reg::X86_64Core(reg));
139 }
140 }
141 // fp registers.
142 for (int reg = 0; reg < 16; reg++) {
143 if (reg < 12) {
144 opcodes.Undefined(Reg::X86_64Fp(reg));
145 } else {
146 opcodes.SameValue(Reg::X86_64Fp(reg));
147 }
148 }
David Srbecky527c9c72015-04-17 21:14:10 +0100149 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
150 WriteEhFrameCIE(is64bit, addr_type, return_reg, opcodes, eh_frame);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100151 return;
152 }
153 case kNone:
154 break;
155 }
156 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
157 UNREACHABLE();
158}
159
David Srbecky8dc73242015-04-12 11:40:39 +0100160void WriteEhFrame(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100161 const OatWriter* oat_writer,
162 ExceptionHeaderValueApplication address_type,
163 std::vector<uint8_t>* eh_frame,
164 std::vector<uintptr_t>* eh_frame_patches,
165 std::vector<uint8_t>* eh_frame_hdr) {
David Srbecky8dc73242015-04-12 11:40:39 +0100166 const auto& method_infos = oat_writer->GetMethodDebugInfo();
167 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky527c9c72015-04-17 21:14:10 +0100168
169 // Write .eh_frame section.
David Srbecky8dc73242015-04-12 11:40:39 +0100170 size_t cie_offset = eh_frame->size();
David Srbecky527c9c72015-04-17 21:14:10 +0100171 WriteEhFrameCIE(isa, address_type, eh_frame);
David Srbecky8dc73242015-04-12 11:40:39 +0100172 for (const OatWriter::DebugInfo& mi : method_infos) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100173 if (!mi.deduped_) { // Only one FDE per unique address.
174 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
175 if (opcodes != nullptr) {
176 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
177 mi.low_pc_, mi.high_pc_ - mi.low_pc_,
178 opcodes, eh_frame, eh_frame_patches);
179 }
David Srbecky8dc73242015-04-12 11:40:39 +0100180 }
181 }
David Srbecky527c9c72015-04-17 21:14:10 +0100182
183 // Write .eh_frame_hdr section.
184 Writer<> header(eh_frame_hdr);
185 header.PushUint8(1); // Version.
186 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // Encoding of .eh_frame pointer.
187 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table size.
188 header.PushUint8(DW_EH_PE_omit); // Encoding of binary search table addresses.
189 // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section, and need to encode
190 // relative to this location as libunwind doesn't honor datarel for eh_frame_hdr correctly.
191 header.PushInt32(-static_cast<int32_t>(eh_frame->size() + 4U));
192 // Omit binary search table size (number of entries).
193 // Omit binary search table.
David Srbecky8dc73242015-04-12 11:40:39 +0100194}
195
David Srbecky3b9d57a2015-04-10 00:22:14 +0100196/*
197 * @brief Generate the DWARF sections.
198 * @param oat_writer The Oat file Writer.
199 * @param eh_frame Call Frame Information.
200 * @param debug_info Compilation unit information.
David Srbecky527c9c72015-04-17 21:14:10 +0100201 * @param debug_info_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100202 * @param debug_abbrev Abbreviations used to generate dbg_info.
203 * @param debug_str Debug strings.
204 * @param debug_line Line number table.
David Srbecky527c9c72015-04-17 21:14:10 +0100205 * @param debug_line_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100206 */
207void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100208 const OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100209 std::vector<uint8_t>* debug_info,
David Srbecky527c9c72015-04-17 21:14:10 +0100210 std::vector<uintptr_t>* debug_info_patches,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100211 std::vector<uint8_t>* debug_abbrev,
212 std::vector<uint8_t>* debug_str,
David Srbecky527c9c72015-04-17 21:14:10 +0100213 std::vector<uint8_t>* debug_line,
214 std::vector<uintptr_t>* debug_line_patches) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100215 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
216 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100217
David Srbecky626a1662015-04-12 13:12:26 +0100218 // Find all addresses (low_pc) which contain deduped methods.
219 // The first instance of method is not marked deduped_, but the rest is.
220 std::unordered_set<uint32_t> deduped_addresses;
221 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
222 if (it->deduped_) {
223 deduped_addresses.insert(it->low_pc_);
224 }
225 }
226
David Srbecky799b8c42015-04-14 01:57:43 +0100227 // Group the methods into compilation units based on source file.
228 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
229 const char* last_source_file = nullptr;
230 for (const auto& mi : method_infos) {
231 // Attribute given instruction range only to single method.
232 // Otherwise the debugger might get really confused.
233 if (!mi.deduped_) {
234 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
235 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
236 if (compilation_units.empty() || source_file != last_source_file) {
237 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
238 }
239 compilation_units.back().push_back(&mi);
240 last_source_file = source_file;
241 }
242 }
243
David Srbecky3b9d57a2015-04-10 00:22:14 +0100244 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100245 for (const auto& compilation_unit : compilation_units) {
246 uint32_t cunit_low_pc = 0xFFFFFFFFU;
247 uint32_t cunit_high_pc = 0;
248 for (auto method_info : compilation_unit) {
249 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
250 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100251 }
252
David Srbecky799b8c42015-04-14 01:57:43 +0100253 size_t debug_abbrev_offset = debug_abbrev->size();
254 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
255 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
256 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
257 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky527c9c72015-04-17 21:14:10 +0100258 info.WriteAddr(DW_AT_low_pc, cunit_low_pc);
259 info.WriteAddr(DW_AT_high_pc, cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100260 info.WriteData4(DW_AT_stmt_list, debug_line->size());
261 for (auto method_info : compilation_unit) {
262 std::string method_name = PrettyMethod(method_info->dex_method_index_,
263 *method_info->dex_file_, true);
264 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
265 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100266 }
David Srbecky799b8c42015-04-14 01:57:43 +0100267 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
268 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
David Srbecky527c9c72015-04-17 21:14:10 +0100269 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_);
270 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100271 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100272 }
David Srbecky799b8c42015-04-14 01:57:43 +0100273 info.EndTag(); // DW_TAG_compile_unit
David Srbecky799b8c42015-04-14 01:57:43 +0100274 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100275
David Srbecky799b8c42015-04-14 01:57:43 +0100276 // Write .debug_line section.
277 std::vector<FileEntry> files;
278 std::unordered_map<std::string, size_t> files_map;
279 std::vector<std::string> directories;
280 std::unordered_map<std::string, size_t> directories_map;
281 int code_factor_bits_ = 0;
282 int dwarf_isa = -1;
283 switch (isa) {
284 case kArm: // arm actually means thumb2.
285 case kThumb2:
286 code_factor_bits_ = 1; // 16-bit instuctions
287 dwarf_isa = 1; // DW_ISA_ARM_thumb.
288 break;
289 case kArm64:
290 case kMips:
291 case kMips64:
292 code_factor_bits_ = 2; // 32-bit instructions
293 break;
294 case kNone:
295 case kX86:
296 case kX86_64:
297 break;
298 }
299 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
David Srbecky527c9c72015-04-17 21:14:10 +0100300 opcodes.SetAddress(cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100301 if (dwarf_isa != -1) {
302 opcodes.SetISA(dwarf_isa);
303 }
304 for (const OatWriter::DebugInfo* mi : compilation_unit) {
305 struct DebugInfoCallbacks {
306 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
307 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
308 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
309 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100310 }
David Srbecky799b8c42015-04-14 01:57:43 +0100311 DefaultSrcMap dex2line_;
312 } debug_info_callbacks;
313
314 const DexFile* dex = mi->dex_file_;
315 if (mi->code_item_ != nullptr) {
316 dex->DecodeDebugInfo(mi->code_item_,
317 (mi->access_flags_ & kAccStatic) != 0,
318 mi->dex_method_index_,
319 DebugInfoCallbacks::NewPosition,
320 nullptr,
321 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100322 }
323
David Srbecky799b8c42015-04-14 01:57:43 +0100324 // Get and deduplicate directory and filename.
325 int file_index = 0; // 0 - primary source file of the compilation.
326 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
327 const char* source_file = dex->GetSourceFile(dex_class_def);
328 if (source_file != nullptr) {
329 std::string file_name(source_file);
330 size_t file_name_slash = file_name.find_last_of('/');
331 std::string class_name(dex->GetClassDescriptor(dex_class_def));
332 size_t class_name_slash = class_name.find_last_of('/');
333 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100334
David Srbecky799b8c42015-04-14 01:57:43 +0100335 // Guess directory from package name.
336 int directory_index = 0; // 0 - current directory of the compilation.
337 if (file_name_slash == std::string::npos && // Just filename.
338 class_name.front() == 'L' && // Type descriptor for a class.
339 class_name_slash != std::string::npos) { // Has package name.
340 std::string package_name = class_name.substr(1, class_name_slash - 1);
341 auto it = directories_map.find(package_name);
342 if (it == directories_map.end()) {
343 directory_index = 1 + directories.size();
344 directories_map.emplace(package_name, directory_index);
345 directories.push_back(package_name);
346 } else {
347 directory_index = it->second;
348 }
349 full_path = package_name + "/" + file_name;
350 }
351
352 // Add file entry.
353 auto it2 = files_map.find(full_path);
354 if (it2 == files_map.end()) {
355 file_index = 1 + files.size();
356 files_map.emplace(full_path, file_index);
357 files.push_back(FileEntry {
358 file_name,
359 directory_index,
360 0, // Modification time - NA.
361 0, // File size - NA.
362 });
363 } else {
364 file_index = it2->second;
365 }
366 }
367 opcodes.SetFile(file_index);
368
369 // Generate mapping opcodes from PC to Java lines.
370 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100371 if (file_index != 0 && !dex2line_map.empty()) {
372 bool first = true;
373 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
374 uint32_t pc = pc2dex.from_;
375 int dex_pc = pc2dex.to_;
376 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
377 if (dex2line.first) {
378 int line = dex2line.second;
379 if (first) {
380 first = false;
381 if (pc > 0) {
382 // Assume that any preceding code is prologue.
383 int first_line = dex2line_map.front().to_;
384 // Prologue is not a sensible place for a breakpoint.
385 opcodes.NegateStmt();
David Srbecky527c9c72015-04-17 21:14:10 +0100386 opcodes.AddRow(mi->low_pc_, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100387 opcodes.NegateStmt();
388 opcodes.SetPrologueEnd();
389 }
David Srbecky527c9c72015-04-17 21:14:10 +0100390 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100391 } else if (line != opcodes.CurrentLine()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100392 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100393 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100394 }
395 }
David Srbecky799b8c42015-04-14 01:57:43 +0100396 } else {
397 // line 0 - instruction cannot be attributed to any source line.
David Srbecky527c9c72015-04-17 21:14:10 +0100398 opcodes.AddRow(mi->low_pc_, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100399 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100400 }
David Srbecky527c9c72015-04-17 21:14:10 +0100401 opcodes.AdvancePC(cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100402 opcodes.EndSequence();
David Srbecky799b8c42015-04-14 01:57:43 +0100403 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100404 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100405}
406
407} // namespace dwarf
408} // namespace art