blob: db599c49491769a7e4bacb677d022808d4d3d628 [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,
David Srbecky033d7452015-04-30 19:57:35 +0100165 std::vector<uint8_t>* eh_frame_hdr,
166 std::vector<uintptr_t>* eh_frame_hdr_patches) {
David Srbecky8dc73242015-04-12 11:40:39 +0100167 const auto& method_infos = oat_writer->GetMethodDebugInfo();
168 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky527c9c72015-04-17 21:14:10 +0100169
170 // Write .eh_frame section.
David Srbecky033d7452015-04-30 19:57:35 +0100171 std::map<uint32_t, size_t> address_to_fde_offset_map;
David Srbecky8dc73242015-04-12 11:40:39 +0100172 size_t cie_offset = eh_frame->size();
David Srbecky527c9c72015-04-17 21:14:10 +0100173 WriteEhFrameCIE(isa, address_type, eh_frame);
David Srbecky8dc73242015-04-12 11:40:39 +0100174 for (const OatWriter::DebugInfo& mi : method_infos) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100175 if (!mi.deduped_) { // Only one FDE per unique address.
176 const SwapVector<uint8_t>* opcodes = mi.compiled_method_->GetCFIInfo();
177 if (opcodes != nullptr) {
David Srbecky033d7452015-04-30 19:57:35 +0100178 address_to_fde_offset_map.emplace(mi.low_pc_, eh_frame->size());
David Srbecky6d73c9d2015-05-01 15:00:40 +0100179 WriteEhFrameFDE(Is64BitInstructionSet(isa), cie_offset,
180 mi.low_pc_, mi.high_pc_ - mi.low_pc_,
181 opcodes, eh_frame, eh_frame_patches);
182 }
David Srbecky8dc73242015-04-12 11:40:39 +0100183 }
184 }
David Srbecky527c9c72015-04-17 21:14:10 +0100185
186 // Write .eh_frame_hdr section.
187 Writer<> header(eh_frame_hdr);
188 header.PushUint8(1); // Version.
David Srbecky033d7452015-04-30 19:57:35 +0100189 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
190 // so we have to use pcrel which means relative to the pointer's location.
191 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
192 // Encoding of binary search table size.
193 header.PushUint8(DW_EH_PE_udata4);
194 // Encoding of binary search table addresses - libunwind supports only this
195 // specific combination, which means relative to the start of .eh_frame_hdr.
196 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
197 // .eh_frame pointer - .eh_frame_hdr section is after .eh_frame section
198 const int32_t relative_eh_frame_begin = -static_cast<int32_t>(eh_frame->size());
199 header.PushInt32(relative_eh_frame_begin - 4U);
200 // Binary search table size (number of entries).
201 header.PushUint32(address_to_fde_offset_map.size());
202 // Binary search table.
203 for (const auto& address_to_fde_offset : address_to_fde_offset_map) {
204 u_int32_t code_address = address_to_fde_offset.first;
205 size_t fde_address = address_to_fde_offset.second;
206 eh_frame_hdr_patches->push_back(header.data()->size());
207 header.PushUint32(code_address);
208 // We know the exact layout (eh_frame is immediately before eh_frame_hdr)
209 // and the data is relative to the start of the eh_frame_hdr,
210 // so patching isn't necessary (in contrast to the code address above).
211 header.PushInt32(relative_eh_frame_begin + fde_address);
212 }
David Srbecky8dc73242015-04-12 11:40:39 +0100213}
214
David Srbecky3b9d57a2015-04-10 00:22:14 +0100215/*
216 * @brief Generate the DWARF sections.
217 * @param oat_writer The Oat file Writer.
218 * @param eh_frame Call Frame Information.
219 * @param debug_info Compilation unit information.
David Srbecky527c9c72015-04-17 21:14:10 +0100220 * @param debug_info_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100221 * @param debug_abbrev Abbreviations used to generate dbg_info.
222 * @param debug_str Debug strings.
223 * @param debug_line Line number table.
David Srbecky527c9c72015-04-17 21:14:10 +0100224 * @param debug_line_patches Address locations to be patched.
David Srbecky3b9d57a2015-04-10 00:22:14 +0100225 */
226void WriteDebugSections(const CompilerDriver* compiler,
David Srbecky527c9c72015-04-17 21:14:10 +0100227 const OatWriter* oat_writer,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100228 std::vector<uint8_t>* debug_info,
David Srbecky527c9c72015-04-17 21:14:10 +0100229 std::vector<uintptr_t>* debug_info_patches,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100230 std::vector<uint8_t>* debug_abbrev,
231 std::vector<uint8_t>* debug_str,
David Srbecky527c9c72015-04-17 21:14:10 +0100232 std::vector<uint8_t>* debug_line,
233 std::vector<uintptr_t>* debug_line_patches) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100234 const std::vector<OatWriter::DebugInfo>& method_infos = oat_writer->GetMethodDebugInfo();
235 const InstructionSet isa = compiler->GetInstructionSet();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100236
David Srbecky626a1662015-04-12 13:12:26 +0100237 // Find all addresses (low_pc) which contain deduped methods.
238 // The first instance of method is not marked deduped_, but the rest is.
239 std::unordered_set<uint32_t> deduped_addresses;
240 for (auto it = method_infos.begin(); it != method_infos.end(); ++it) {
241 if (it->deduped_) {
242 deduped_addresses.insert(it->low_pc_);
243 }
244 }
245
David Srbecky799b8c42015-04-14 01:57:43 +0100246 // Group the methods into compilation units based on source file.
247 std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units;
248 const char* last_source_file = nullptr;
249 for (const auto& mi : method_infos) {
250 // Attribute given instruction range only to single method.
251 // Otherwise the debugger might get really confused.
252 if (!mi.deduped_) {
253 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
254 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
255 if (compilation_units.empty() || source_file != last_source_file) {
256 compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>());
257 }
258 compilation_units.back().push_back(&mi);
259 last_source_file = source_file;
260 }
261 }
262
David Srbecky3b9d57a2015-04-10 00:22:14 +0100263 // Write .debug_info section.
David Srbecky799b8c42015-04-14 01:57:43 +0100264 for (const auto& compilation_unit : compilation_units) {
265 uint32_t cunit_low_pc = 0xFFFFFFFFU;
266 uint32_t cunit_high_pc = 0;
267 for (auto method_info : compilation_unit) {
268 cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_);
269 cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100270 }
271
David Srbecky799b8c42015-04-14 01:57:43 +0100272 size_t debug_abbrev_offset = debug_abbrev->size();
273 DebugInfoEntryWriter<> info(false /* 32 bit */, debug_abbrev);
274 info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes);
275 info.WriteStrp(DW_AT_producer, "Android dex2oat", debug_str);
276 info.WriteData1(DW_AT_language, DW_LANG_Java);
David Srbecky527c9c72015-04-17 21:14:10 +0100277 info.WriteAddr(DW_AT_low_pc, cunit_low_pc);
278 info.WriteAddr(DW_AT_high_pc, cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100279 info.WriteData4(DW_AT_stmt_list, debug_line->size());
280 for (auto method_info : compilation_unit) {
281 std::string method_name = PrettyMethod(method_info->dex_method_index_,
282 *method_info->dex_file_, true);
283 if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) {
284 method_name += " [DEDUPED]";
David Srbecky3b9d57a2015-04-10 00:22:14 +0100285 }
David Srbecky799b8c42015-04-14 01:57:43 +0100286 info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no);
287 info.WriteStrp(DW_AT_name, method_name.data(), debug_str);
David Srbecky527c9c72015-04-17 21:14:10 +0100288 info.WriteAddr(DW_AT_low_pc, method_info->low_pc_);
289 info.WriteAddr(DW_AT_high_pc, method_info->high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100290 info.EndTag(); // DW_TAG_subprogram
David Srbecky3b9d57a2015-04-10 00:22:14 +0100291 }
David Srbecky799b8c42015-04-14 01:57:43 +0100292 info.EndTag(); // DW_TAG_compile_unit
David Srbecky799b8c42015-04-14 01:57:43 +0100293 WriteDebugInfoCU(debug_abbrev_offset, info, debug_info, debug_info_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100294
David Srbecky799b8c42015-04-14 01:57:43 +0100295 // Write .debug_line section.
296 std::vector<FileEntry> files;
297 std::unordered_map<std::string, size_t> files_map;
298 std::vector<std::string> directories;
299 std::unordered_map<std::string, size_t> directories_map;
300 int code_factor_bits_ = 0;
301 int dwarf_isa = -1;
302 switch (isa) {
303 case kArm: // arm actually means thumb2.
304 case kThumb2:
305 code_factor_bits_ = 1; // 16-bit instuctions
306 dwarf_isa = 1; // DW_ISA_ARM_thumb.
307 break;
308 case kArm64:
309 case kMips:
310 case kMips64:
311 code_factor_bits_ = 2; // 32-bit instructions
312 break;
313 case kNone:
314 case kX86:
315 case kX86_64:
316 break;
317 }
318 DebugLineOpCodeWriter<> opcodes(false /* 32bit */, code_factor_bits_);
David Srbecky527c9c72015-04-17 21:14:10 +0100319 opcodes.SetAddress(cunit_low_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100320 if (dwarf_isa != -1) {
321 opcodes.SetISA(dwarf_isa);
322 }
323 for (const OatWriter::DebugInfo* mi : compilation_unit) {
324 struct DebugInfoCallbacks {
325 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
326 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
327 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
328 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100329 }
David Srbecky799b8c42015-04-14 01:57:43 +0100330 DefaultSrcMap dex2line_;
331 } debug_info_callbacks;
332
333 const DexFile* dex = mi->dex_file_;
334 if (mi->code_item_ != nullptr) {
335 dex->DecodeDebugInfo(mi->code_item_,
336 (mi->access_flags_ & kAccStatic) != 0,
337 mi->dex_method_index_,
338 DebugInfoCallbacks::NewPosition,
339 nullptr,
340 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100341 }
342
David Srbecky799b8c42015-04-14 01:57:43 +0100343 // Get and deduplicate directory and filename.
344 int file_index = 0; // 0 - primary source file of the compilation.
345 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
346 const char* source_file = dex->GetSourceFile(dex_class_def);
347 if (source_file != nullptr) {
348 std::string file_name(source_file);
349 size_t file_name_slash = file_name.find_last_of('/');
350 std::string class_name(dex->GetClassDescriptor(dex_class_def));
351 size_t class_name_slash = class_name.find_last_of('/');
352 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100353
David Srbecky799b8c42015-04-14 01:57:43 +0100354 // Guess directory from package name.
355 int directory_index = 0; // 0 - current directory of the compilation.
356 if (file_name_slash == std::string::npos && // Just filename.
357 class_name.front() == 'L' && // Type descriptor for a class.
358 class_name_slash != std::string::npos) { // Has package name.
359 std::string package_name = class_name.substr(1, class_name_slash - 1);
360 auto it = directories_map.find(package_name);
361 if (it == directories_map.end()) {
362 directory_index = 1 + directories.size();
363 directories_map.emplace(package_name, directory_index);
364 directories.push_back(package_name);
365 } else {
366 directory_index = it->second;
367 }
368 full_path = package_name + "/" + file_name;
369 }
370
371 // Add file entry.
372 auto it2 = files_map.find(full_path);
373 if (it2 == files_map.end()) {
374 file_index = 1 + files.size();
375 files_map.emplace(full_path, file_index);
376 files.push_back(FileEntry {
377 file_name,
378 directory_index,
379 0, // Modification time - NA.
380 0, // File size - NA.
381 });
382 } else {
383 file_index = it2->second;
384 }
385 }
386 opcodes.SetFile(file_index);
387
388 // Generate mapping opcodes from PC to Java lines.
389 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100390 if (file_index != 0 && !dex2line_map.empty()) {
391 bool first = true;
392 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
393 uint32_t pc = pc2dex.from_;
394 int dex_pc = pc2dex.to_;
395 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
396 if (dex2line.first) {
397 int line = dex2line.second;
398 if (first) {
399 first = false;
400 if (pc > 0) {
401 // Assume that any preceding code is prologue.
402 int first_line = dex2line_map.front().to_;
403 // Prologue is not a sensible place for a breakpoint.
404 opcodes.NegateStmt();
David Srbecky527c9c72015-04-17 21:14:10 +0100405 opcodes.AddRow(mi->low_pc_, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100406 opcodes.NegateStmt();
407 opcodes.SetPrologueEnd();
408 }
David Srbecky527c9c72015-04-17 21:14:10 +0100409 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100410 } else if (line != opcodes.CurrentLine()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100411 opcodes.AddRow(mi->low_pc_ + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100412 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100413 }
414 }
David Srbecky799b8c42015-04-14 01:57:43 +0100415 } else {
416 // line 0 - instruction cannot be attributed to any source line.
David Srbecky527c9c72015-04-17 21:14:10 +0100417 opcodes.AddRow(mi->low_pc_, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100418 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100419 }
David Srbecky527c9c72015-04-17 21:14:10 +0100420 opcodes.AdvancePC(cunit_high_pc);
David Srbecky799b8c42015-04-14 01:57:43 +0100421 opcodes.EndSequence();
David Srbecky799b8c42015-04-14 01:57:43 +0100422 WriteDebugLineTable(directories, files, opcodes, debug_line, debug_line_patches);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100423 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100424}
425
426} // namespace dwarf
427} // namespace art