blob: 94e5d76962dc2f7115b21b0bac94d2a75cd6be03 [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>
Vladimir Marko10c13562015-11-25 14:33:36 +000020#include <vector>
David Srbecky9b3607d2016-01-14 18:15:54 +000021#include <cstdio>
David Srbecky626a1662015-04-12 13:12:26 +010022
Andreas Gampee3d623e2015-05-01 16:11:04 -070023#include "base/casts.h"
David Srbecky04b05262015-11-09 18:05:48 +000024#include "base/stl_util.h"
David Srbecky9b3607d2016-01-14 18:15:54 +000025#include "linear_alloc.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010026#include "compiled_method.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010027#include "dex_file-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000028#include "driver/compiler_driver.h"
David Srbecky04b05262015-11-09 18:05:48 +000029#include "dwarf/dedup_vector.h"
David Srbecky91cb54e2016-01-15 13:47:59 +000030#include "dwarf/expression.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010031#include "dwarf/headers.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000032#include "dwarf/method_debug_info.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010033#include "dwarf/register.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000034#include "elf_builder.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000035#include "linker/vector_output_stream.h"
Tamas Berghammer86e42782016-01-05 14:29:02 +000036#include "mirror/array.h"
37#include "mirror/class-inl.h"
38#include "mirror/class.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010039#include "oat_writer.h"
David Srbecky0fd295f2015-11-16 16:39:10 +000040#include "stack_map.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000041#include "utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010042
David Srbecky5fdcc3c2016-01-19 14:33:43 +000043// liblzma.
44#include "XzEnc.h"
45#include "7zCrc.h"
46#include "XzCrc64.h"
47
David Srbecky3b9d57a2015-04-10 00:22:14 +010048namespace art {
49namespace dwarf {
50
David Srbeckye0febdf2015-12-17 20:53:07 +000051// The ARM specification defines three special mapping symbols
52// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
53// These symbols can be used by tools, for example, to pretty
54// print instructions correctly. Objdump will use them if they
55// exist, but it will still work well without them.
56// However, these extra symbols take space, so let's just generate
57// one symbol which marks the whole .text section as code.
58constexpr bool kGenerateSingleArmMappingSymbol = true;
59
David Srbecky0fd295f2015-11-16 16:39:10 +000060static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
61 switch (isa) {
62 case kArm:
63 case kThumb2:
64 return Reg::ArmCore(machine_reg);
65 case kArm64:
66 return Reg::Arm64Core(machine_reg);
67 case kX86:
68 return Reg::X86Core(machine_reg);
69 case kX86_64:
70 return Reg::X86_64Core(machine_reg);
71 case kMips:
72 return Reg::MipsCore(machine_reg);
73 case kMips64:
74 return Reg::Mips64Core(machine_reg);
75 default:
76 LOG(FATAL) << "Unknown instruction set: " << isa;
77 UNREACHABLE();
78 }
79}
80
81static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
82 switch (isa) {
83 case kArm:
84 case kThumb2:
85 return Reg::ArmFp(machine_reg);
86 case kArm64:
87 return Reg::Arm64Fp(machine_reg);
88 case kX86:
89 return Reg::X86Fp(machine_reg);
90 case kX86_64:
91 return Reg::X86_64Fp(machine_reg);
92 default:
93 LOG(FATAL) << "Unknown instruction set: " << isa;
94 UNREACHABLE();
95 }
96}
97
David Srbecky6d8c8f02015-10-26 10:57:09 +000098static void WriteCIE(InstructionSet isa,
99 CFIFormat format,
100 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100101 // Scratch registers should be marked as undefined. This tells the
102 // debugger that its value in the previous frame is not recoverable.
103 bool is64bit = Is64BitInstructionSet(isa);
104 switch (isa) {
105 case kArm:
106 case kThumb2: {
107 DebugFrameOpCodeWriter<> opcodes;
108 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
109 // core registers.
110 for (int reg = 0; reg < 13; reg++) {
111 if (reg < 4 || reg == 12) {
112 opcodes.Undefined(Reg::ArmCore(reg));
113 } else {
114 opcodes.SameValue(Reg::ArmCore(reg));
115 }
116 }
117 // fp registers.
118 for (int reg = 0; reg < 32; reg++) {
119 if (reg < 16) {
120 opcodes.Undefined(Reg::ArmFp(reg));
121 } else {
122 opcodes.SameValue(Reg::ArmFp(reg));
123 }
124 }
David Srbecky527c9c72015-04-17 21:14:10 +0100125 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100127 return;
128 }
129 case kArm64: {
130 DebugFrameOpCodeWriter<> opcodes;
131 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
132 // core registers.
133 for (int reg = 0; reg < 30; reg++) {
134 if (reg < 8 || reg == 16 || reg == 17) {
135 opcodes.Undefined(Reg::Arm64Core(reg));
136 } else {
137 opcodes.SameValue(Reg::Arm64Core(reg));
138 }
139 }
140 // fp registers.
141 for (int reg = 0; reg < 32; reg++) {
142 if (reg < 8 || reg >= 16) {
143 opcodes.Undefined(Reg::Arm64Fp(reg));
144 } else {
145 opcodes.SameValue(Reg::Arm64Fp(reg));
146 }
147 }
David Srbecky527c9c72015-04-17 21:14:10 +0100148 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100150 return;
151 }
152 case kMips:
153 case kMips64: {
154 DebugFrameOpCodeWriter<> opcodes;
155 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
156 // core registers.
157 for (int reg = 1; reg < 26; reg++) {
158 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
159 opcodes.Undefined(Reg::MipsCore(reg));
160 } else {
161 opcodes.SameValue(Reg::MipsCore(reg));
162 }
163 }
David Srbecky527c9c72015-04-17 21:14:10 +0100164 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100166 return;
167 }
168 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100169 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
170 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100171 DebugFrameOpCodeWriter<> opcodes;
172 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
173 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
174 // core registers.
175 for (int reg = 0; reg < 8; reg++) {
176 if (reg <= 3) {
177 opcodes.Undefined(Reg::X86Core(reg));
178 } else if (reg == 4) {
179 // Stack pointer.
180 } else {
181 opcodes.SameValue(Reg::X86Core(reg));
182 }
183 }
184 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100185 if (generate_opcodes_for_x86_fp) {
186 for (int reg = 0; reg < 8; reg++) {
187 opcodes.Undefined(Reg::X86Fp(reg));
188 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100189 }
David Srbecky527c9c72015-04-17 21:14:10 +0100190 auto return_reg = Reg::X86Core(8); // R8(EIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000191 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100192 return;
193 }
194 case kX86_64: {
195 DebugFrameOpCodeWriter<> opcodes;
196 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
197 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
198 // core registers.
199 for (int reg = 0; reg < 16; reg++) {
200 if (reg == 4) {
201 // Stack pointer.
202 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
203 opcodes.Undefined(Reg::X86_64Core(reg));
204 } else {
205 opcodes.SameValue(Reg::X86_64Core(reg));
206 }
207 }
208 // fp registers.
209 for (int reg = 0; reg < 16; reg++) {
210 if (reg < 12) {
211 opcodes.Undefined(Reg::X86_64Fp(reg));
212 } else {
213 opcodes.SameValue(Reg::X86_64Fp(reg));
214 }
215 }
David Srbecky527c9c72015-04-17 21:14:10 +0100216 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000217 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100218 return;
219 }
220 case kNone:
221 break;
222 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000223 LOG(FATAL) << "Cannot write CIE frame for ISA " << isa;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100224 UNREACHABLE();
225}
226
David Srbecky6d8c8f02015-10-26 10:57:09 +0000227template<typename ElfTypes>
228void WriteCFISection(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000229 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000230 CFIFormat format) {
David Srbeckye0febdf2015-12-17 20:53:07 +0000231 CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000232 typedef typename ElfTypes::Addr Elf_Addr;
233
Tamas Berghammer86e42782016-01-05 14:29:02 +0000234 if (method_infos.empty()) {
235 return;
236 }
237
David Srbecky6d8c8f02015-10-26 10:57:09 +0000238 std::vector<uint32_t> binary_search_table;
239 std::vector<uintptr_t> patch_locations;
240 if (format == DW_EH_FRAME_FORMAT) {
241 binary_search_table.reserve(2 * method_infos.size());
242 } else {
243 patch_locations.reserve(method_infos.size());
244 }
David Srbecky527c9c72015-04-17 21:14:10 +0100245
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100246 // Write .eh_frame/.debug_frame section.
David Srbeckye0febdf2015-12-17 20:53:07 +0000247 auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000248 ? builder->GetDebugFrame()
249 : builder->GetEhFrame());
250 {
251 cfi_section->Start();
252 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +0000253 const Elf_Addr text_address = builder->GetText()->Exists()
254 ? builder->GetText()->GetAddress()
255 : 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000256 const Elf_Addr cfi_address = cfi_section->GetAddress();
257 const Elf_Addr cie_address = cfi_address;
258 Elf_Addr buffer_address = cfi_address;
259 std::vector<uint8_t> buffer; // Small temporary buffer.
260 WriteCIE(builder->GetIsa(), format, &buffer);
261 cfi_section->WriteFully(buffer.data(), buffer.size());
262 buffer_address += buffer.size();
263 buffer.clear();
Vladimir Marko10c13562015-11-25 14:33:36 +0000264 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000265 if (!mi.deduped_) { // Only one FDE per unique address.
266 ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo();
267 if (!opcodes.empty()) {
268 const Elf_Addr code_address = text_address + mi.low_pc_;
269 if (format == DW_EH_FRAME_FORMAT) {
270 binary_search_table.push_back(
271 dchecked_integral_cast<uint32_t>(code_address));
272 binary_search_table.push_back(
273 dchecked_integral_cast<uint32_t>(buffer_address));
274 }
275 WriteFDE(is64bit, cfi_address, cie_address,
276 code_address, mi.high_pc_ - mi.low_pc_,
277 opcodes, format, buffer_address, &buffer,
278 &patch_locations);
279 cfi_section->WriteFully(buffer.data(), buffer.size());
280 buffer_address += buffer.size();
281 buffer.clear();
282 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100283 }
David Srbecky8dc73242015-04-12 11:40:39 +0100284 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000285 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100286 }
David Srbecky527c9c72015-04-17 21:14:10 +0100287
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100288 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000289 auto* header_section = builder->GetEhFrameHdr();
290 header_section->Start();
291 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100292 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000293 std::vector<uint8_t> buffer;
294 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100295 header.PushUint8(1); // Version.
296 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
297 // so we have to use pcrel which means relative to the pointer's location.
298 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
299 // Encoding of binary search table size.
300 header.PushUint8(DW_EH_PE_udata4);
301 // Encoding of binary search table addresses - libunwind supports only this
302 // specific combination, which means relative to the start of .eh_frame_hdr.
303 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000304 // .eh_frame pointer
305 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100306 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000307 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
308 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100309 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000310 for (size_t i = 0; i < binary_search_table.size(); i++) {
311 // Make addresses section-relative since we know the header address now.
312 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100313 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000314 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
315 header_section->End();
316 } else {
Vladimir Marko10c13562015-11-25 14:33:36 +0000317 builder->WritePatches(".debug_frame.oat_patches",
318 ArrayRef<const uintptr_t>(patch_locations));
David Srbecky033d7452015-04-30 19:57:35 +0100319 }
David Srbecky8dc73242015-04-12 11:40:39 +0100320}
321
David Srbecky996ed0b2015-11-27 10:27:11 +0000322namespace {
323 struct CompilationUnit {
324 std::vector<const MethodDebugInfo*> methods_;
325 size_t debug_line_offset_ = 0;
David Srbecky5cc349f2015-12-18 15:04:48 +0000326 uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max();
327 uintptr_t high_pc_ = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000328 };
329
David Srbeckyb06e28e2015-12-10 13:15:00 +0000330 typedef std::vector<DexFile::LocalInfo> LocalInfos;
David Srbecky996ed0b2015-11-27 10:27:11 +0000331
David Srbeckyb06e28e2015-12-10 13:15:00 +0000332 void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
333 static_cast<LocalInfos*>(ctx)->push_back(entry);
334 }
335
336 typedef std::vector<DexFile::PositionInfo> PositionInfos;
337
338 bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
339 static_cast<PositionInfos*>(ctx)->push_back(entry);
340 return false;
341 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000342
343 std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
344 std::vector<const char*> names;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000345 if (mi->code_item_ != nullptr) {
346 const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_);
347 if (stream != nullptr) {
348 DecodeUnsignedLeb128(&stream); // line.
349 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
350 for (uint32_t i = 0; i < parameters_size; ++i) {
351 uint32_t id = DecodeUnsignedLeb128P1(&stream);
352 names.push_back(mi->dex_file_->StringDataByIdx(id));
353 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000354 }
355 }
356 return names;
357 }
358
359 struct VariableLocation {
360 uint32_t low_pc;
361 uint32_t high_pc;
362 DexRegisterLocation reg_lo; // May be None if the location is unknown.
363 DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
364 };
365
366 // Get the location of given dex register (e.g. stack or machine register).
367 // Note that the location might be different based on the current pc.
368 // The result will cover all ranges where the variable is in scope.
369 std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info,
370 uint16_t vreg,
371 bool is64bitValue,
372 uint32_t dex_pc_low,
373 uint32_t dex_pc_high) {
374 std::vector<VariableLocation> variable_locations;
375
376 // Get stack maps sorted by pc (they might not be sorted internally).
377 const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
378 const StackMapEncoding encoding = code_info.ExtractEncoding();
379 std::map<uint32_t, StackMap> stack_maps;
380 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
381 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
382 DCHECK(stack_map.IsValid());
383 const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
384 DCHECK_LE(low_pc, method_info->high_pc_);
385 stack_maps.emplace(low_pc, stack_map);
386 }
387
388 // Create entries for the requested register based on stack map data.
389 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
390 const StackMap& stack_map = it->second;
391 const uint32_t low_pc = it->first;
392 auto next_it = it;
393 next_it++;
394 const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
395 : method_info->high_pc_;
396 DCHECK_LE(low_pc, high_pc);
397 if (low_pc == high_pc) {
398 continue; // Ignore if the address range is empty.
399 }
400
401 // Check that the stack map is in the requested range.
402 uint32_t dex_pc = stack_map.GetDexPc(encoding);
403 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
404 continue;
405 }
406
407 // Find the location of the dex register.
408 DexRegisterLocation reg_lo = DexRegisterLocation::None();
409 DexRegisterLocation reg_hi = DexRegisterLocation::None();
410 if (stack_map.HasDexRegisterMap(encoding)) {
411 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
412 stack_map, encoding, method_info->code_item_->registers_size_);
413 reg_lo = dex_register_map.GetDexRegisterLocation(
414 vreg, method_info->code_item_->registers_size_, code_info, encoding);
415 if (is64bitValue) {
416 reg_hi = dex_register_map.GetDexRegisterLocation(
417 vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
418 }
419 }
420
421 // Add location entry for this address range.
422 if (!variable_locations.empty() &&
423 variable_locations.back().reg_lo == reg_lo &&
424 variable_locations.back().reg_hi == reg_hi &&
425 variable_locations.back().high_pc == low_pc) {
426 // Merge with the previous entry (extend its range).
427 variable_locations.back().high_pc = high_pc;
428 } else {
429 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
430 }
431 }
432
433 return variable_locations;
434 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000435
436 bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) {
437 return method_info->compiled_method_->GetQuickCode().size() > 0 &&
438 method_info->compiled_method_->GetVmapTable().size() > 0 &&
439 method_info->compiled_method_->GetGcMap().size() == 0 &&
440 method_info->code_item_ != nullptr;
441 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000442} // namespace
David Srbecky04b05262015-11-09 18:05:48 +0000443
444// Helper class to write .debug_info and its supporting sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000445template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000446class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000447 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100448
David Srbecky04b05262015-11-09 18:05:48 +0000449 // Helper class to write one compilation unit.
450 // It holds helper methods and temporary state.
451 class CompilationUnitWriter {
452 public:
453 explicit CompilationUnitWriter(DebugInfoWriter* owner)
454 : owner_(owner),
455 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) {
456 }
457
458 void Write(const CompilationUnit& compilation_unit) {
459 CHECK(!compilation_unit.methods_.empty());
David Srbecky5cc349f2015-12-18 15:04:48 +0000460 const Elf_Addr text_address = owner_->builder_->GetText()->Exists()
461 ? owner_->builder_->GetText()->GetAddress()
462 : 0;
463 const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_;
David Srbecky04b05262015-11-09 18:05:48 +0000464
465 info_.StartTag(DW_TAG_compile_unit);
466 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
467 info_.WriteData1(DW_AT_language, DW_LANG_Java);
Tamas Berghammer8c557122015-12-10 15:06:25 +0000468 info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT"));
David Srbecky04b05262015-11-09 18:05:48 +0000469 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000470 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size));
David Srbecky0fd295f2015-11-16 16:39:10 +0000471 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
David Srbecky04b05262015-11-09 18:05:48 +0000472
473 const char* last_dex_class_desc = nullptr;
474 for (auto mi : compilation_unit.methods_) {
475 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000476 const DexFile::CodeItem* dex_code = mi->code_item_;
David Srbecky04b05262015-11-09 18:05:48 +0000477 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
478 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
479 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
480 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
David Srbecky996ed0b2015-11-27 10:27:11 +0000481 const bool is_static = (mi->access_flags_ & kAccStatic) != 0;
David Srbecky04b05262015-11-09 18:05:48 +0000482
483 // Enclose the method in correct class definition.
484 if (last_dex_class_desc != dex_class_desc) {
485 if (last_dex_class_desc != nullptr) {
486 EndClassTag(last_dex_class_desc);
487 }
Tamas Berghammer86e42782016-01-05 14:29:02 +0000488 // Write reference tag for the class we are about to declare.
489 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
490 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
491 size_t type_attrib_offset = info_.size();
492 info_.WriteRef4(DW_AT_type, 0);
493 info_.EndTag();
494 // Declare the class that owns this method.
495 size_t class_offset = StartClassTag(dex_class_desc);
496 info_.UpdateUint32(type_attrib_offset, class_offset);
497 info_.WriteFlag(DW_AT_declaration, true);
David Srbecky04b05262015-11-09 18:05:48 +0000498 // Check that each class is defined only once.
499 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
500 CHECK(unique) << "Redefinition of " << dex_class_desc;
501 last_dex_class_desc = dex_class_desc;
502 }
503
David Srbecky04b05262015-11-09 18:05:48 +0000504 int start_depth = info_.Depth();
505 info_.StartTag(DW_TAG_subprogram);
506 WriteName(dex->GetMethodName(dex_method));
507 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000508 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_));
David Srbecky91cb54e2016-01-15 13:47:59 +0000509 std::vector<uint8_t> expr_buffer;
510 Expression expr(&expr_buffer);
511 expr.WriteOpCallFrameCfa();
512 info_.WriteExprLoc(DW_AT_frame_base, expr);
David Srbecky04b05262015-11-09 18:05:48 +0000513 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
David Srbeckyb06e28e2015-12-10 13:15:00 +0000514
515 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
516 // guarantee order or uniqueness so it is safer to iterate over them manually.
517 // DecodeDebugLocalInfo might not also be available if there is no debug info.
518 std::vector<const char*> param_names = GetParamNames(mi);
519 uint32_t arg_reg = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000520 if (!is_static) {
521 info_.StartTag(DW_TAG_formal_parameter);
522 WriteName("this");
523 info_.WriteFlag(DW_AT_artificial, true);
524 WriteLazyType(dex_class_desc);
David Srbeckyb06e28e2015-12-10 13:15:00 +0000525 if (dex_code != nullptr) {
526 // Write the stack location of the parameter.
527 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
528 const bool is64bitValue = false;
529 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
530 }
531 arg_reg++;
David Srbecky996ed0b2015-11-27 10:27:11 +0000532 info_.EndTag();
533 }
David Srbecky04b05262015-11-09 18:05:48 +0000534 if (dex_params != nullptr) {
535 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
536 info_.StartTag(DW_TAG_formal_parameter);
537 // Parameter names may not be always available.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000538 if (i < param_names.size()) {
David Srbecky04b05262015-11-09 18:05:48 +0000539 WriteName(param_names[i]);
540 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000541 // Write the type.
542 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
543 WriteLazyType(type_desc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000544 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
David Srbeckyb06e28e2015-12-10 13:15:00 +0000545 if (dex_code != nullptr) {
546 // Write the stack location of the parameter.
547 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
548 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
549 }
550 arg_reg += is64bitValue ? 2 : 1;
David Srbecky04b05262015-11-09 18:05:48 +0000551 info_.EndTag();
552 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000553 if (dex_code != nullptr) {
554 DCHECK_EQ(arg_reg, dex_code->ins_size_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000555 }
David Srbecky04b05262015-11-09 18:05:48 +0000556 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000557
558 // Write local variables.
559 LocalInfos local_infos;
560 if (dex->DecodeDebugLocalInfo(dex_code,
561 is_static,
562 mi->dex_method_index_,
563 LocalInfoCallback,
564 &local_infos)) {
565 for (const DexFile::LocalInfo& var : local_infos) {
566 if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) {
567 info_.StartTag(DW_TAG_variable);
568 WriteName(var.name_);
569 WriteLazyType(var.descriptor_);
570 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
571 WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_,
572 var.start_address_, var.end_address_);
573 info_.EndTag();
574 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000575 }
576 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000577
David Srbecky04b05262015-11-09 18:05:48 +0000578 info_.EndTag();
579 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
580 }
581 if (last_dex_class_desc != nullptr) {
582 EndClassTag(last_dex_class_desc);
583 }
584 CHECK_EQ(info_.Depth(), 1);
585 FinishLazyTypes();
586 info_.EndTag(); // DW_TAG_compile_unit
587 std::vector<uint8_t> buffer;
588 buffer.reserve(info_.data()->size() + KB);
589 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
590 const size_t debug_abbrev_offset =
591 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
592 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
593 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
594 }
595
Tamas Berghammer86e42782016-01-05 14:29:02 +0000596 void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
597 info_.StartTag(DW_TAG_compile_unit);
598 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
599 info_.WriteData1(DW_AT_language, DW_LANG_Java);
600
David Srbecky9b3607d2016-01-14 18:15:54 +0000601 std::vector<uint8_t> expr_buffer;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000602 for (mirror::Class* type : types) {
603 if (type->IsPrimitive()) {
604 // For primitive types the definition and the declaration is the same.
605 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
606 WriteTypeDeclaration(type->GetDescriptor(nullptr));
607 }
608 } else if (type->IsArrayClass()) {
609 mirror::Class* element_type = type->GetComponentType();
610 uint32_t component_size = type->GetComponentSize();
611 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
612 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
613
614 info_.StartTag(DW_TAG_array_type);
615 std::string descriptor_string;
616 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
David Srbecky9b3607d2016-01-14 18:15:54 +0000617 WriteLinkageName(type);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000618 info_.WriteUdata(DW_AT_data_member_location, data_offset);
619 info_.StartTag(DW_TAG_subrange_type);
David Srbecky9b3607d2016-01-14 18:15:54 +0000620 Expression count_expr(&expr_buffer);
David Srbecky91cb54e2016-01-15 13:47:59 +0000621 count_expr.WriteOpPushObjectAddress();
622 count_expr.WriteOpPlusUconst(length_offset);
623 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
624 info_.WriteExprLoc(DW_AT_count, count_expr);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000625 info_.EndTag(); // DW_TAG_subrange_type.
626 info_.EndTag(); // DW_TAG_array_type.
David Srbecky9b3607d2016-01-14 18:15:54 +0000627 } else if (type->IsInterface()) {
628 // Skip. Variables cannot have an interface as a dynamic type.
629 // We do not expose the interface information to the debugger in any way.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000630 } else {
David Srbecky9b3607d2016-01-14 18:15:54 +0000631 // Declare base class. We can not use the standard WriteLazyType
632 // since we want to avoid the DW_TAG_reference_tag wrapping.
633 mirror::Class* base_class = type->GetSuperClass();
634 size_t base_class_declaration_offset = 0;
635 if (base_class != nullptr) {
636 std::string tmp_storage;
637 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
638 base_class_declaration_offset = StartClassTag(base_class_desc);
639 info_.WriteFlag(DW_AT_declaration, true);
640 WriteLinkageName(base_class);
641 EndClassTag(base_class_desc);
642 }
643
Tamas Berghammer86e42782016-01-05 14:29:02 +0000644 std::string descriptor_string;
645 const char* desc = type->GetDescriptor(&descriptor_string);
646 StartClassTag(desc);
647
648 if (!type->IsVariableSize()) {
649 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
650 }
651
David Srbecky9b3607d2016-01-14 18:15:54 +0000652 WriteLinkageName(type);
653
654 if (type->IsObjectClass()) {
655 // Generate artificial member which is used to get the dynamic type of variable.
656 // The run-time value of this field will correspond to linkage name of some type.
657 // We need to do it only once in j.l.Object since all other types inherit it.
658 info_.StartTag(DW_TAG_member);
659 WriteName(".dynamic_type");
660 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
661 info_.WriteFlag(DW_AT_artificial, true);
662 // Create DWARF expression to get the value of the methods_ field.
663 Expression expr(&expr_buffer);
664 // The address of the object has been implicitly pushed on the stack.
665 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
666 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
667 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
668 expr.WriteOpDerefSize(4);
669 if (kPoisonHeapReferences) {
670 expr.WriteOpNeg();
671 // DWARF stack is pointer sized. Ensure that the high bits are clear.
672 expr.WriteOpConstu(0xFFFFFFFF);
673 expr.WriteOpAnd();
674 }
675 // Add offset to the methods_ field.
676 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
677 // Top of stack holds the location of the field now.
678 info_.WriteExprLoc(DW_AT_data_member_location, expr);
679 info_.EndTag(); // DW_TAG_member.
680 }
681
Tamas Berghammer86e42782016-01-05 14:29:02 +0000682 // Base class.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000683 if (base_class != nullptr) {
684 info_.StartTag(DW_TAG_inheritance);
David Srbecky9b3607d2016-01-14 18:15:54 +0000685 info_.WriteRef4(DW_AT_type, base_class_declaration_offset);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000686 info_.WriteUdata(DW_AT_data_member_location, 0);
687 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
688 info_.EndTag(); // DW_TAG_inheritance.
689 }
690
691 // Member variables.
692 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
693 ArtField* field = type->GetInstanceField(i);
694 info_.StartTag(DW_TAG_member);
695 WriteName(field->GetName());
696 WriteLazyType(field->GetTypeDescriptor());
697 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
698 uint32_t access_flags = field->GetAccessFlags();
699 if (access_flags & kAccPublic) {
700 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
701 } else if (access_flags & kAccProtected) {
702 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
703 } else if (access_flags & kAccPrivate) {
704 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
705 }
706 info_.EndTag(); // DW_TAG_member.
707 }
708
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000709 if (type->IsStringClass()) {
710 // Emit debug info about an artifical class member for java.lang.String which represents
711 // the first element of the data stored in a string instance. Consumers of the debug
712 // info will be able to read the content of java.lang.String based on the count (real
713 // field) and based on the location of this data member.
714 info_.StartTag(DW_TAG_member);
715 WriteName("value");
716 // We don't support fields with C like array types so we just say its type is java char.
717 WriteLazyType("C"); // char.
718 info_.WriteUdata(DW_AT_data_member_location,
719 mirror::String::ValueOffset().Uint32Value());
720 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
721 info_.EndTag(); // DW_TAG_member.
722 }
723
Tamas Berghammer86e42782016-01-05 14:29:02 +0000724 EndClassTag(desc);
725 }
726 }
727
728 CHECK_EQ(info_.Depth(), 1);
729 FinishLazyTypes();
730 info_.EndTag(); // DW_TAG_compile_unit.
731 std::vector<uint8_t> buffer;
732 buffer.reserve(info_.data()->size() + KB);
733 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
734 const size_t debug_abbrev_offset =
735 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
736 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
737 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
738 }
739
David Srbecky9b3607d2016-01-14 18:15:54 +0000740 // Linkage name uniquely identifies type.
741 // It is used to determine the dynamic type of objects.
742 // We use the methods_ field of class since it is unique and it is not moved by the GC.
743 void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
744 auto* methods_ptr = type->GetMethodsPtr();
745 if (methods_ptr == nullptr) {
746 // Some types might have no methods. Allocate empty array instead.
747 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
748 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
749 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
750 type->SetMethodsPtr(methods_ptr, 0, 0);
751 DCHECK(type->GetMethodsPtr() != nullptr);
752 }
753 char name[32];
754 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
755 info_.WriteString(DW_AT_linkage_name, name);
756 }
757
David Srbecky0fd295f2015-11-16 16:39:10 +0000758 // Write table into .debug_loc which describes location of dex register.
759 // The dex register might be valid only at some points and it might
760 // move between machine registers and stack.
David Srbecky996ed0b2015-11-27 10:27:11 +0000761 void WriteRegLocation(const MethodDebugInfo* method_info,
762 uint16_t vreg,
763 bool is64bitValue,
764 uint32_t compilation_unit_low_pc,
765 uint32_t dex_pc_low = 0,
766 uint32_t dex_pc_high = 0xFFFFFFFF) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000767 using Kind = DexRegisterLocation::Kind;
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000768 if (!IsFromOptimizingCompiler(method_info)) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000769 return;
770 }
771
David Srbecky996ed0b2015-11-27 10:27:11 +0000772 Writer<> debug_loc(&owner_->debug_loc_);
773 Writer<> debug_ranges(&owner_->debug_ranges_);
774 info_.WriteSecOffset(DW_AT_location, debug_loc.size());
775 info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size());
David Srbecky0fd295f2015-11-16 16:39:10 +0000776
David Srbecky996ed0b2015-11-27 10:27:11 +0000777 std::vector<VariableLocation> variable_locations = GetVariableLocations(
778 method_info,
779 vreg,
780 is64bitValue,
781 dex_pc_low,
782 dex_pc_high);
783
784 // Write .debug_loc entries.
David Srbecky0fd295f2015-11-16 16:39:10 +0000785 const InstructionSet isa = owner_->builder_->GetIsa();
786 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky91cb54e2016-01-15 13:47:59 +0000787 std::vector<uint8_t> expr_buffer;
David Srbecky996ed0b2015-11-27 10:27:11 +0000788 for (const VariableLocation& variable_location : variable_locations) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000789 // Translate dex register location to DWARF expression.
790 // Note that 64-bit value might be split to two distinct locations.
791 // (for example, two 32-bit machine registers, or even stack and register)
David Srbecky91cb54e2016-01-15 13:47:59 +0000792 Expression expr(&expr_buffer);
David Srbecky996ed0b2015-11-27 10:27:11 +0000793 DexRegisterLocation reg_lo = variable_location.reg_lo;
794 DexRegisterLocation reg_hi = variable_location.reg_hi;
David Srbecky0fd295f2015-11-16 16:39:10 +0000795 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
796 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
797 const Kind kind = reg_loc.GetKind();
798 const int32_t value = reg_loc.GetValue();
799 if (kind == Kind::kInStack) {
800 const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
David Srbecky0fd295f2015-11-16 16:39:10 +0000801 // The stack offset is relative to SP. Make it relative to CFA.
David Srbecky91cb54e2016-01-15 13:47:59 +0000802 expr.WriteOpFbreg(value - frame_size);
David Srbecky0fd295f2015-11-16 16:39:10 +0000803 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
804 reg_hi.GetValue() == value + 4) {
805 break; // the high word is correctly implied by the low word.
806 }
807 } else if (kind == Kind::kInRegister) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000808 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000809 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
810 reg_hi.GetValue() == value) {
811 break; // the high word is correctly implied by the low word.
812 }
813 } else if (kind == Kind::kInFpuRegister) {
814 if ((isa == kArm || isa == kThumb2) &&
815 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
816 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
817 // Translate S register pair to D register (e.g. S4+S5 to D2).
David Srbecky91cb54e2016-01-15 13:47:59 +0000818 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000819 break;
820 }
David Srbecky3dd7e5a2015-11-27 13:31:16 +0000821 if (isa == kMips || isa == kMips64) {
822 // TODO: Find what the DWARF floating point register numbers are on MIPS.
823 break;
824 }
David Srbecky91cb54e2016-01-15 13:47:59 +0000825 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000826 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
827 reg_hi.GetValue() == reg_lo.GetValue()) {
828 break; // the high word is correctly implied by the low word.
829 }
830 } else if (kind == Kind::kConstant) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000831 expr.WriteOpConsts(value);
832 expr.WriteOpStackValue();
David Srbecky0fd295f2015-11-16 16:39:10 +0000833 } else if (kind == Kind::kNone) {
834 break;
835 } else {
836 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
837 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
838 // the special cases above and they should not occur alone.
839 LOG(ERROR) << "Unexpected register location kind: "
840 << DexRegisterLocation::PrettyDescriptor(kind);
841 break;
842 }
843 if (is64bitValue) {
844 // Write the marker which is needed by split 64-bit values.
845 // This code is skipped by the special cases.
David Srbecky91cb54e2016-01-15 13:47:59 +0000846 expr.WriteOpPiece(4);
David Srbecky0fd295f2015-11-16 16:39:10 +0000847 }
848 }
849
David Srbecky91cb54e2016-01-15 13:47:59 +0000850 if (expr.size() > 0) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000851 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000852 debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
853 debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000854 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000855 debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
856 debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000857 }
858 // Write the expression.
David Srbecky91cb54e2016-01-15 13:47:59 +0000859 debug_loc.PushUint16(expr.size());
860 debug_loc.PushData(expr.data());
David Srbecky0fd295f2015-11-16 16:39:10 +0000861 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000862 // Do not generate .debug_loc if the location is not known.
David Srbecky0fd295f2015-11-16 16:39:10 +0000863 }
864 }
865 // Write end-of-list entry.
866 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000867 debug_loc.PushUint64(0);
868 debug_loc.PushUint64(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000869 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000870 debug_loc.PushUint32(0);
871 debug_loc.PushUint32(0);
872 }
873
874 // Write .debug_ranges entries.
875 // This includes ranges where the variable is in scope but the location is not known.
876 for (size_t i = 0; i < variable_locations.size(); i++) {
877 uint32_t low_pc = variable_locations[i].low_pc;
878 uint32_t high_pc = variable_locations[i].high_pc;
879 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
880 // Merge address range with the next entry.
881 high_pc = variable_locations[++i].high_pc;
882 }
883 if (is64bit) {
884 debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
885 debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
886 } else {
887 debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
888 debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
889 }
890 }
891 // Write end-of-list entry.
892 if (is64bit) {
893 debug_ranges.PushUint64(0);
894 debug_ranges.PushUint64(0);
895 } else {
896 debug_ranges.PushUint32(0);
897 debug_ranges.PushUint32(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000898 }
899 }
900
David Srbecky04b05262015-11-09 18:05:48 +0000901 // Some types are difficult to define as we go since they need
902 // to be enclosed in the right set of namespaces. Therefore we
903 // just define all types lazily at the end of compilation unit.
904 void WriteLazyType(const char* type_descriptor) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000905 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000906 lazy_types_.emplace(std::string(type_descriptor), info_.size());
David Srbecky04b05262015-11-09 18:05:48 +0000907 info_.WriteRef4(DW_AT_type, 0);
908 }
909 }
910
911 void FinishLazyTypes() {
912 for (const auto& lazy_type : lazy_types_) {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000913 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
David Srbecky04b05262015-11-09 18:05:48 +0000914 }
915 lazy_types_.clear();
916 }
917
918 private:
919 void WriteName(const char* name) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000920 if (name != nullptr) {
921 info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
922 }
David Srbecky04b05262015-11-09 18:05:48 +0000923 }
924
925 // Convert dex type descriptor to DWARF.
926 // Returns offset in the compilation unit.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000927 size_t WriteTypeDeclaration(const std::string& desc) {
928 DCHECK(!desc.empty());
David Srbecky04b05262015-11-09 18:05:48 +0000929 const auto& it = type_cache_.find(desc);
930 if (it != type_cache_.end()) {
931 return it->second;
932 }
933
934 size_t offset;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000935 if (desc[0] == 'L') {
David Srbecky04b05262015-11-09 18:05:48 +0000936 // Class type. For example: Lpackage/name;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000937 size_t class_offset = StartClassTag(desc.c_str());
David Srbecky04b05262015-11-09 18:05:48 +0000938 info_.WriteFlag(DW_AT_declaration, true);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000939 EndClassTag(desc.c_str());
940 // Reference to the class type.
941 offset = info_.StartTag(DW_TAG_reference_type);
942 info_.WriteRef(DW_AT_type, class_offset);
943 info_.EndTag();
944 } else if (desc[0] == '[') {
David Srbecky04b05262015-11-09 18:05:48 +0000945 // Array type.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000946 size_t element_type = WriteTypeDeclaration(desc.substr(1));
947 size_t array_type = info_.StartTag(DW_TAG_array_type);
948 info_.WriteFlag(DW_AT_declaration, true);
David Srbecky04b05262015-11-09 18:05:48 +0000949 info_.WriteRef(DW_AT_type, element_type);
950 info_.EndTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000951 offset = info_.StartTag(DW_TAG_reference_type);
952 info_.WriteRef4(DW_AT_type, array_type);
953 info_.EndTag();
David Srbecky04b05262015-11-09 18:05:48 +0000954 } else {
955 // Primitive types.
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000956 DCHECK_EQ(desc.size(), 1u);
957
David Srbecky04b05262015-11-09 18:05:48 +0000958 const char* name;
David Srbecky0fd295f2015-11-16 16:39:10 +0000959 uint32_t encoding;
960 uint32_t byte_size;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000961 switch (desc[0]) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000962 case 'B':
963 name = "byte";
964 encoding = DW_ATE_signed;
965 byte_size = 1;
966 break;
967 case 'C':
968 name = "char";
969 encoding = DW_ATE_UTF;
970 byte_size = 2;
971 break;
972 case 'D':
973 name = "double";
974 encoding = DW_ATE_float;
975 byte_size = 8;
976 break;
977 case 'F':
978 name = "float";
979 encoding = DW_ATE_float;
980 byte_size = 4;
981 break;
982 case 'I':
983 name = "int";
984 encoding = DW_ATE_signed;
985 byte_size = 4;
986 break;
987 case 'J':
988 name = "long";
989 encoding = DW_ATE_signed;
990 byte_size = 8;
991 break;
992 case 'S':
993 name = "short";
994 encoding = DW_ATE_signed;
995 byte_size = 2;
996 break;
997 case 'Z':
998 name = "boolean";
999 encoding = DW_ATE_boolean;
1000 byte_size = 1;
1001 break;
1002 case 'V':
1003 LOG(FATAL) << "Void type should not be encoded";
1004 UNREACHABLE();
David Srbecky04b05262015-11-09 18:05:48 +00001005 default:
Tamas Berghammer86e42782016-01-05 14:29:02 +00001006 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
David Srbecky04b05262015-11-09 18:05:48 +00001007 UNREACHABLE();
1008 }
1009 offset = info_.StartTag(DW_TAG_base_type);
1010 WriteName(name);
David Srbecky0fd295f2015-11-16 16:39:10 +00001011 info_.WriteData1(DW_AT_encoding, encoding);
1012 info_.WriteData1(DW_AT_byte_size, byte_size);
David Srbecky04b05262015-11-09 18:05:48 +00001013 info_.EndTag();
1014 }
1015
1016 type_cache_.emplace(desc, offset);
1017 return offset;
1018 }
1019
1020 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
1021 // Returns offset of the class tag in the compilation unit.
1022 size_t StartClassTag(const char* desc) {
1023 DCHECK(desc != nullptr && desc[0] == 'L');
1024 // Enclose the type in namespace tags.
1025 const char* end;
1026 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
1027 info_.StartTag(DW_TAG_namespace);
1028 WriteName(std::string(desc, end - desc).c_str());
1029 }
1030 // Start the class tag.
1031 size_t offset = info_.StartTag(DW_TAG_class_type);
1032 end = strchr(desc, ';');
1033 CHECK(end != nullptr);
1034 WriteName(std::string(desc, end - desc).c_str());
1035 return offset;
1036 }
1037
1038 void EndClassTag(const char* desc) {
1039 DCHECK(desc != nullptr && desc[0] == 'L');
1040 // End the class tag.
1041 info_.EndTag();
1042 // Close namespace tags.
1043 const char* end;
1044 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
1045 info_.EndTag();
1046 }
1047 }
1048
1049 // For access to the ELF sections.
1050 DebugInfoWriter<ElfTypes>* owner_;
1051 // Debug abbrevs for this compilation unit only.
1052 std::vector<uint8_t> debug_abbrev_;
1053 // Temporary buffer to create and store the entries.
1054 DebugInfoEntryWriter<> info_;
1055 // Cache of already translated type descriptors.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001056 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001057 // 32-bit references which need to be resolved to a type later.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001058 // Given type may be used multiple times. Therefore we need a multimap.
1059 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001060 };
1061
David Srbeckyb851b492015-11-11 20:19:38 +00001062 public:
1063 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbecky626a1662015-04-12 13:12:26 +01001064 }
1065
David Srbeckyb851b492015-11-11 20:19:38 +00001066 void Start() {
1067 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +01001068 }
1069
David Srbecky04b05262015-11-09 18:05:48 +00001070 void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
1071 CompilationUnitWriter writer(this);
1072 writer.Write(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001073 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001074
Tamas Berghammer86e42782016-01-05 14:29:02 +00001075 void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
1076 CompilationUnitWriter writer(this);
1077 writer.Write(types);
1078 }
1079
David Srbeckyb851b492015-11-11 20:19:38 +00001080 void End() {
1081 builder_->GetDebugInfo()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001082 builder_->WritePatches(".debug_info.oat_patches",
1083 ArrayRef<const uintptr_t>(debug_info_patches_));
David Srbecky04b05262015-11-09 18:05:48 +00001084 builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data());
1085 builder_->WriteSection(".debug_str", &debug_str_.Data());
David Srbecky0fd295f2015-11-16 16:39:10 +00001086 builder_->WriteSection(".debug_loc", &debug_loc_);
David Srbecky996ed0b2015-11-27 10:27:11 +00001087 builder_->WriteSection(".debug_ranges", &debug_ranges_);
David Srbeckyb851b492015-11-11 20:19:38 +00001088 }
1089
1090 private:
David Srbecky04b05262015-11-09 18:05:48 +00001091 size_t WriteString(const char* str) {
1092 return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
1093 }
1094
David Srbeckyb851b492015-11-11 20:19:38 +00001095 ElfBuilder<ElfTypes>* builder_;
1096 std::vector<uintptr_t> debug_info_patches_;
David Srbecky04b05262015-11-09 18:05:48 +00001097 DedupVector debug_abbrev_;
1098 DedupVector debug_str_;
David Srbecky0fd295f2015-11-16 16:39:10 +00001099 std::vector<uint8_t> debug_loc_;
David Srbecky996ed0b2015-11-27 10:27:11 +00001100 std::vector<uint8_t> debug_ranges_;
David Srbecky04b05262015-11-09 18:05:48 +00001101
1102 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
David Srbeckyb851b492015-11-11 20:19:38 +00001103};
1104
1105template<typename ElfTypes>
1106class DebugLineWriter {
1107 typedef typename ElfTypes::Addr Elf_Addr;
1108
1109 public:
1110 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
1111 }
1112
1113 void Start() {
1114 builder_->GetDebugLine()->Start();
1115 }
1116
1117 // Write line table for given set of methods.
1118 // Returns the number of bytes written.
David Srbecky04b05262015-11-09 18:05:48 +00001119 size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
David Srbeckyb851b492015-11-11 20:19:38 +00001120 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +00001121 const Elf_Addr text_address = builder_->GetText()->Exists()
1122 ? builder_->GetText()->GetAddress()
1123 : 0;
David Srbecky04b05262015-11-09 18:05:48 +00001124
1125 compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
David Srbeckyb851b492015-11-11 20:19:38 +00001126
David Srbecky799b8c42015-04-14 01:57:43 +01001127 std::vector<FileEntry> files;
1128 std::unordered_map<std::string, size_t> files_map;
1129 std::vector<std::string> directories;
1130 std::unordered_map<std::string, size_t> directories_map;
1131 int code_factor_bits_ = 0;
1132 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +00001133 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +01001134 case kArm: // arm actually means thumb2.
1135 case kThumb2:
1136 code_factor_bits_ = 1; // 16-bit instuctions
1137 dwarf_isa = 1; // DW_ISA_ARM_thumb.
1138 break;
1139 case kArm64:
1140 case kMips:
1141 case kMips64:
1142 code_factor_bits_ = 2; // 32-bit instructions
1143 break;
1144 case kNone:
1145 case kX86:
1146 case kX86_64:
1147 break;
1148 }
David Srbecky297ed222015-04-15 01:18:12 +01001149 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
Vladimir Marko10c13562015-11-25 14:33:36 +00001150 for (const MethodDebugInfo* mi : compilation_unit.methods_) {
David Srbecky04b05262015-11-09 18:05:48 +00001151 // Ignore function if we have already generated line table for the same address.
1152 // It would confuse the debugger and the DWARF specification forbids it.
1153 if (mi->deduped_) {
1154 continue;
1155 }
1156
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001157 ArrayRef<const SrcMapElem> src_mapping_table;
1158 std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
1159 if (IsFromOptimizingCompiler(mi)) {
1160 // Use stack maps to create mapping table from pc to dex.
1161 const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data());
1162 const StackMapEncoding encoding = code_info.ExtractEncoding();
1163 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
1164 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
1165 DCHECK(stack_map.IsValid());
1166 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
1167 const int32_t dex = stack_map.GetDexPc(encoding);
1168 src_mapping_table_from_stack_maps.push_back({pc, dex});
1169 }
1170 std::sort(src_mapping_table_from_stack_maps.begin(),
1171 src_mapping_table_from_stack_maps.end());
1172 src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
1173 } else {
1174 // Use the mapping table provided by the quick compiler.
1175 src_mapping_table = mi->compiled_method_->GetSrcMappingTable();
1176 }
1177
1178 if (src_mapping_table.empty()) {
1179 continue;
1180 }
1181
David Srbecky6d8c8f02015-10-26 10:57:09 +00001182 Elf_Addr method_address = text_address + mi->low_pc_;
1183
David Srbeckyb06e28e2015-12-10 13:15:00 +00001184 PositionInfos position_infos;
David Srbecky799b8c42015-04-14 01:57:43 +01001185 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001186 if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) {
1187 continue;
David Srbecky3b9d57a2015-04-10 00:22:14 +01001188 }
1189
David Srbeckyb06e28e2015-12-10 13:15:00 +00001190 if (position_infos.empty()) {
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001191 continue;
1192 }
1193
1194 opcodes.SetAddress(method_address);
1195 if (dwarf_isa != -1) {
1196 opcodes.SetISA(dwarf_isa);
1197 }
1198
David Srbecky799b8c42015-04-14 01:57:43 +01001199 // Get and deduplicate directory and filename.
1200 int file_index = 0; // 0 - primary source file of the compilation.
1201 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
1202 const char* source_file = dex->GetSourceFile(dex_class_def);
1203 if (source_file != nullptr) {
1204 std::string file_name(source_file);
1205 size_t file_name_slash = file_name.find_last_of('/');
1206 std::string class_name(dex->GetClassDescriptor(dex_class_def));
1207 size_t class_name_slash = class_name.find_last_of('/');
1208 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001209
David Srbecky799b8c42015-04-14 01:57:43 +01001210 // Guess directory from package name.
1211 int directory_index = 0; // 0 - current directory of the compilation.
1212 if (file_name_slash == std::string::npos && // Just filename.
1213 class_name.front() == 'L' && // Type descriptor for a class.
1214 class_name_slash != std::string::npos) { // Has package name.
1215 std::string package_name = class_name.substr(1, class_name_slash - 1);
1216 auto it = directories_map.find(package_name);
1217 if (it == directories_map.end()) {
1218 directory_index = 1 + directories.size();
1219 directories_map.emplace(package_name, directory_index);
1220 directories.push_back(package_name);
1221 } else {
1222 directory_index = it->second;
1223 }
1224 full_path = package_name + "/" + file_name;
1225 }
1226
1227 // Add file entry.
1228 auto it2 = files_map.find(full_path);
1229 if (it2 == files_map.end()) {
1230 file_index = 1 + files.size();
1231 files_map.emplace(full_path, file_index);
1232 files.push_back(FileEntry {
1233 file_name,
1234 directory_index,
1235 0, // Modification time - NA.
1236 0, // File size - NA.
1237 });
1238 } else {
1239 file_index = it2->second;
1240 }
1241 }
1242 opcodes.SetFile(file_index);
1243
1244 // Generate mapping opcodes from PC to Java lines.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001245 if (file_index != 0) {
David Srbecky799b8c42015-04-14 01:57:43 +01001246 bool first = true;
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001247 for (SrcMapElem pc2dex : src_mapping_table) {
David Srbecky799b8c42015-04-14 01:57:43 +01001248 uint32_t pc = pc2dex.from_;
1249 int dex_pc = pc2dex.to_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001250 // Find mapping with address with is greater than our dex pc; then go back one step.
1251 auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc,
1252 [](uint32_t address, const DexFile::PositionInfo& entry) {
1253 return address < entry.address_;
1254 });
1255 if (ub != position_infos.begin()) {
1256 int line = (--ub)->line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001257 if (first) {
1258 first = false;
1259 if (pc > 0) {
1260 // Assume that any preceding code is prologue.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001261 int first_line = position_infos.front().line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001262 // Prologue is not a sensible place for a breakpoint.
1263 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +00001264 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +01001265 opcodes.NegateStmt();
1266 opcodes.SetPrologueEnd();
1267 }
David Srbecky6d8c8f02015-10-26 10:57:09 +00001268 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +01001269 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +00001270 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001271 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001272 }
1273 }
David Srbecky799b8c42015-04-14 01:57:43 +01001274 } else {
1275 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +00001276 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001277 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001278
1279 opcodes.AdvancePC(text_address + mi->high_pc_);
1280 opcodes.EndSequence();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001281 }
David Srbeckyb851b492015-11-11 20:19:38 +00001282 std::vector<uint8_t> buffer;
1283 buffer.reserve(opcodes.data()->size() + KB);
1284 size_t offset = builder_->GetDebugLine()->GetSize();
1285 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
1286 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
1287 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001288 }
David Srbeckyb851b492015-11-11 20:19:38 +00001289
1290 void End() {
1291 builder_->GetDebugLine()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001292 builder_->WritePatches(".debug_line.oat_patches",
1293 ArrayRef<const uintptr_t>(debug_line_patches));
David Srbeckyb851b492015-11-11 20:19:38 +00001294 }
1295
1296 private:
1297 ElfBuilder<ElfTypes>* builder_;
1298 std::vector<uintptr_t> debug_line_patches;
1299};
1300
1301template<typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001302static void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
Tamas Berghammer86e42782016-01-05 14:29:02 +00001303 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyb851b492015-11-11 20:19:38 +00001304 // Group the methods into compilation units based on source file.
1305 std::vector<CompilationUnit> compilation_units;
1306 const char* last_source_file = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +00001307 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky04b05262015-11-09 18:05:48 +00001308 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
1309 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
1310 if (compilation_units.empty() || source_file != last_source_file) {
1311 compilation_units.push_back(CompilationUnit());
David Srbeckyb851b492015-11-11 20:19:38 +00001312 }
David Srbecky04b05262015-11-09 18:05:48 +00001313 CompilationUnit& cu = compilation_units.back();
1314 cu.methods_.push_back(&mi);
1315 cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
1316 cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
1317 last_source_file = source_file;
David Srbeckyb851b492015-11-11 20:19:38 +00001318 }
1319
1320 // Write .debug_line section.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001321 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001322 DebugLineWriter<ElfTypes> line_writer(builder);
1323 line_writer.Start();
David Srbeckyb851b492015-11-11 20:19:38 +00001324 for (auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001325 line_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001326 }
1327 line_writer.End();
1328 }
1329
1330 // Write .debug_info section.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001331 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001332 DebugInfoWriter<ElfTypes> info_writer(builder);
1333 info_writer.Start();
1334 for (const auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001335 info_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001336 }
1337 info_writer.End();
1338 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001339}
1340
David Srbeckye0febdf2015-12-17 20:53:07 +00001341template <typename ElfTypes>
1342void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
1343 const ArrayRef<const MethodDebugInfo>& method_infos) {
1344 bool generated_mapping_symbol = false;
1345 auto* strtab = builder->GetStrTab();
1346 auto* symtab = builder->GetSymTab();
1347
1348 if (method_infos.empty()) {
1349 return;
1350 }
1351
1352 // Find all addresses (low_pc) which contain deduped methods.
1353 // The first instance of method is not marked deduped_, but the rest is.
1354 std::unordered_set<uint32_t> deduped_addresses;
1355 for (const MethodDebugInfo& info : method_infos) {
1356 if (info.deduped_) {
1357 deduped_addresses.insert(info.low_pc_);
1358 }
1359 }
1360
1361 strtab->Start();
1362 strtab->Write(""); // strtab should start with empty string.
1363 for (const MethodDebugInfo& info : method_infos) {
1364 if (info.deduped_) {
1365 continue; // Add symbol only for the first instance.
1366 }
1367 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, true);
1368 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
1369 name += " [DEDUPED]";
1370 }
1371
David Srbecky5cc349f2015-12-18 15:04:48 +00001372 const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr;
1373 const bool is_relative = (text != nullptr);
David Srbeckye0febdf2015-12-17 20:53:07 +00001374 uint32_t low_pc = info.low_pc_;
1375 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
1376 low_pc += info.compiled_method_->CodeDelta();
David Srbecky5cc349f2015-12-18 15:04:48 +00001377 symtab->Add(strtab->Write(name), text, low_pc,
1378 is_relative, info.high_pc_ - info.low_pc_, STB_GLOBAL, STT_FUNC);
David Srbeckye0febdf2015-12-17 20:53:07 +00001379
1380 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
1381 // instructions, so that disassembler tools can correctly disassemble.
1382 // Note that even if we generate just a single mapping symbol, ARM's Streamline
1383 // requires it to match function symbol. Just address 0 does not work.
1384 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
1385 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001386 symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1,
1387 is_relative, 0, STB_LOCAL, STT_NOTYPE);
David Srbeckye0febdf2015-12-17 20:53:07 +00001388 generated_mapping_symbol = true;
1389 }
1390 }
1391 }
1392 strtab->End();
1393
1394 // Symbols are buffered and written after names (because they are smaller).
1395 // We could also do two passes in this function to avoid the buffering.
1396 symtab->Start();
1397 symtab->Write();
1398 symtab->End();
1399}
1400
1401template <typename ElfTypes>
1402void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
1403 const ArrayRef<const MethodDebugInfo>& method_infos,
1404 CFIFormat cfi_format) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001405 // Add methods to .symtab.
1406 WriteDebugSymbols(builder, method_infos);
1407 // Generate CFI (stack unwinding information).
1408 WriteCFISection(builder, method_infos, cfi_format);
1409 // Write DWARF .debug_* sections.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001410 WriteDebugSections(builder, method_infos);
David Srbeckye0febdf2015-12-17 20:53:07 +00001411}
1412
David Srbecky5fdcc3c2016-01-19 14:33:43 +00001413static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) {
1414 // Configure the compression library.
1415 CrcGenerateTable();
1416 Crc64GenerateTable();
1417 CLzma2EncProps lzma2Props;
1418 Lzma2EncProps_Init(&lzma2Props);
1419 lzma2Props.lzmaProps.level = 1; // Fast compression.
1420 Lzma2EncProps_Normalize(&lzma2Props);
1421 CXzProps props;
1422 XzProps_Init(&props);
1423 props.lzma2Props = &lzma2Props;
1424 // Implement the required interface for communication (written in C so no virtual methods).
1425 struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress {
1426 static SRes ReadImpl(void* p, void* buf, size_t* size) {
1427 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p));
1428 *size = std::min(*size, ctx->src_->size() - ctx->src_pos_);
1429 memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size);
1430 ctx->src_pos_ += *size;
1431 return SZ_OK;
1432 }
1433 static size_t WriteImpl(void* p, const void* buf, size_t size) {
1434 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p));
1435 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
1436 ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size);
1437 return size;
1438 }
1439 static SRes ProgressImpl(void* , UInt64, UInt64) {
1440 return SZ_OK;
1441 }
1442 size_t src_pos_;
1443 const std::vector<uint8_t>* src_;
1444 std::vector<uint8_t>* dst_;
1445 };
1446 XzCallbacks callbacks;
1447 callbacks.Read = XzCallbacks::ReadImpl;
1448 callbacks.Write = XzCallbacks::WriteImpl;
1449 callbacks.Progress = XzCallbacks::ProgressImpl;
1450 callbacks.src_pos_ = 0;
1451 callbacks.src_ = src;
1452 callbacks.dst_ = dst;
1453 // Compress.
1454 SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
1455 CHECK_EQ(res, SZ_OK);
1456}
1457
1458template <typename ElfTypes>
1459void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder,
1460 const ArrayRef<const MethodDebugInfo>& method_infos) {
1461 const InstructionSet isa = parent_builder->GetIsa();
1462 std::vector<uint8_t> buffer;
1463 buffer.reserve(KB);
1464 VectorOutputStream out("Mini-debug-info ELF file", &buffer);
1465 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1466 builder->Start();
1467 // Write .rodata and .text as NOBITS sections.
1468 // This allows tools to detect virtual address relocation of the parent ELF file.
1469 builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress());
1470 builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize());
1471 builder->SetVirtualAddress(parent_builder->GetText()->GetAddress());
1472 builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize());
1473 WriteDebugSymbols(builder.get(), method_infos);
1474 WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT);
1475 builder->End();
1476 CHECK(builder->Good());
1477 std::vector<uint8_t> compressed_buffer;
1478 compressed_buffer.reserve(buffer.size() / 4);
1479 XzCompress(&buffer, &compressed_buffer);
1480 parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer);
1481}
1482
David Srbecky5cc349f2015-12-18 15:04:48 +00001483template <typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001484static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal(
David Srbecky5cc349f2015-12-18 15:04:48 +00001485 const dwarf::MethodDebugInfo& method_info) {
1486 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1487 std::vector<uint8_t> buffer;
1488 buffer.reserve(KB);
1489 VectorOutputStream out("Debug ELF file", &buffer);
1490 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1491 builder->Start();
1492 WriteDebugInfo(builder.get(),
1493 ArrayRef<const MethodDebugInfo>(&method_info, 1),
1494 DW_DEBUG_FRAME_FORMAT);
1495 builder->End();
1496 CHECK(builder->Good());
1497 // Make a copy of the buffer. We want to shrink it anyway.
1498 uint8_t* result = new uint8_t[buffer.size()];
1499 CHECK(result != nullptr);
1500 memcpy(result, buffer.data(), buffer.size());
1501 return ArrayRef<const uint8_t>(result, buffer.size());
1502}
1503
Tamas Berghammer86e42782016-01-05 14:29:02 +00001504ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001505 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1506 if (Is64BitInstructionSet(isa)) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001507 return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info);
David Srbecky5cc349f2015-12-18 15:04:48 +00001508 } else {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001509 return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info);
1510 }
1511}
1512
1513template <typename ElfTypes>
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001514static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
1515 const InstructionSet isa, const ArrayRef<mirror::Class*>& types)
Tamas Berghammer86e42782016-01-05 14:29:02 +00001516 SHARED_REQUIRES(Locks::mutator_lock_) {
1517 std::vector<uint8_t> buffer;
1518 buffer.reserve(KB);
1519 VectorOutputStream out("Debug ELF file", &buffer);
1520 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1521 builder->Start();
1522
1523 DebugInfoWriter<ElfTypes> info_writer(builder.get());
1524 info_writer.Start();
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001525 info_writer.WriteTypes(types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001526 info_writer.End();
1527
1528 builder->End();
1529 CHECK(builder->Good());
1530 // Make a copy of the buffer. We want to shrink it anyway.
1531 uint8_t* result = new uint8_t[buffer.size()];
1532 CHECK(result != nullptr);
1533 memcpy(result, buffer.data(), buffer.size());
1534 return ArrayRef<const uint8_t>(result, buffer.size());
1535}
1536
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001537ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa,
1538 const ArrayRef<mirror::Class*>& types) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001539 if (Is64BitInstructionSet(isa)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001540 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001541 } else {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001542 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types);
David Srbecky5cc349f2015-12-18 15:04:48 +00001543 }
1544}
1545
David Srbecky6d8c8f02015-10-26 10:57:09 +00001546// Explicit instantiations
David Srbeckye0febdf2015-12-17 20:53:07 +00001547template void WriteDebugInfo<ElfTypes32>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001548 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001549 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001550 CFIFormat cfi_format);
1551template void WriteDebugInfo<ElfTypes64>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001552 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001553 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001554 CFIFormat cfi_format);
David Srbecky5fdcc3c2016-01-19 14:33:43 +00001555template void WriteMiniDebugInfo<ElfTypes32>(
1556 ElfBuilder<ElfTypes32>* builder,
1557 const ArrayRef<const MethodDebugInfo>& method_infos);
1558template void WriteMiniDebugInfo<ElfTypes64>(
1559 ElfBuilder<ElfTypes64>* builder,
1560 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +00001561
David Srbecky3b9d57a2015-04-10 00:22:14 +01001562} // namespace dwarf
1563} // namespace art