blob: e1116fd6d208fe578c5c6bf67e06bbc1b694c19b [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 Srbecky5e974a62016-01-22 14:25:03 +000019#include <algorithm>
David Srbecky626a1662015-04-12 13:12:26 +010020#include <unordered_set>
Vladimir Marko10c13562015-11-25 14:33:36 +000021#include <vector>
David Srbecky9b3607d2016-01-14 18:15:54 +000022#include <cstdio>
David Srbecky626a1662015-04-12 13:12:26 +010023
Andreas Gampee3d623e2015-05-01 16:11:04 -070024#include "base/casts.h"
David Srbecky04b05262015-11-09 18:05:48 +000025#include "base/stl_util.h"
David Srbecky9b3607d2016-01-14 18:15:54 +000026#include "linear_alloc.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010027#include "compiled_method.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010028#include "dex_file-inl.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000029#include "driver/compiler_driver.h"
David Srbecky04b05262015-11-09 18:05:48 +000030#include "dwarf/dedup_vector.h"
David Srbecky91cb54e2016-01-15 13:47:59 +000031#include "dwarf/expression.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010032#include "dwarf/headers.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000033#include "dwarf/method_debug_info.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010034#include "dwarf/register.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000035#include "elf_builder.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000036#include "linker/vector_output_stream.h"
Tamas Berghammer86e42782016-01-05 14:29:02 +000037#include "mirror/array.h"
38#include "mirror/class-inl.h"
39#include "mirror/class.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010040#include "oat_writer.h"
David Srbecky0fd295f2015-11-16 16:39:10 +000041#include "stack_map.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000042#include "utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010043
David Srbecky5b1c2ca2016-01-25 17:32:41 +000044// liblzma.
45#include "XzEnc.h"
46#include "7zCrc.h"
47#include "XzCrc64.h"
48
David Srbecky3b9d57a2015-04-10 00:22:14 +010049namespace art {
50namespace dwarf {
51
David Srbeckye0febdf2015-12-17 20:53:07 +000052// The ARM specification defines three special mapping symbols
53// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
54// These symbols can be used by tools, for example, to pretty
55// print instructions correctly. Objdump will use them if they
56// exist, but it will still work well without them.
57// However, these extra symbols take space, so let's just generate
58// one symbol which marks the whole .text section as code.
59constexpr bool kGenerateSingleArmMappingSymbol = true;
60
David Srbecky0fd295f2015-11-16 16:39:10 +000061static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
62 switch (isa) {
63 case kArm:
64 case kThumb2:
65 return Reg::ArmCore(machine_reg);
66 case kArm64:
67 return Reg::Arm64Core(machine_reg);
68 case kX86:
69 return Reg::X86Core(machine_reg);
70 case kX86_64:
71 return Reg::X86_64Core(machine_reg);
72 case kMips:
73 return Reg::MipsCore(machine_reg);
74 case kMips64:
75 return Reg::Mips64Core(machine_reg);
76 default:
77 LOG(FATAL) << "Unknown instruction set: " << isa;
78 UNREACHABLE();
79 }
80}
81
82static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
83 switch (isa) {
84 case kArm:
85 case kThumb2:
86 return Reg::ArmFp(machine_reg);
87 case kArm64:
88 return Reg::Arm64Fp(machine_reg);
89 case kX86:
90 return Reg::X86Fp(machine_reg);
91 case kX86_64:
92 return Reg::X86_64Fp(machine_reg);
David Srbeckyba702002016-02-01 18:15:29 +000093 case kMips:
94 return Reg::MipsFp(machine_reg);
95 case kMips64:
96 return Reg::Mips64Fp(machine_reg);
David Srbecky0fd295f2015-11-16 16:39:10 +000097 default:
98 LOG(FATAL) << "Unknown instruction set: " << isa;
99 UNREACHABLE();
100 }
101}
102
David Srbecky6d8c8f02015-10-26 10:57:09 +0000103static void WriteCIE(InstructionSet isa,
104 CFIFormat format,
105 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100106 // Scratch registers should be marked as undefined. This tells the
107 // debugger that its value in the previous frame is not recoverable.
108 bool is64bit = Is64BitInstructionSet(isa);
109 switch (isa) {
110 case kArm:
111 case kThumb2: {
112 DebugFrameOpCodeWriter<> opcodes;
113 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
114 // core registers.
115 for (int reg = 0; reg < 13; reg++) {
116 if (reg < 4 || reg == 12) {
117 opcodes.Undefined(Reg::ArmCore(reg));
118 } else {
119 opcodes.SameValue(Reg::ArmCore(reg));
120 }
121 }
122 // fp registers.
123 for (int reg = 0; reg < 32; reg++) {
124 if (reg < 16) {
125 opcodes.Undefined(Reg::ArmFp(reg));
126 } else {
127 opcodes.SameValue(Reg::ArmFp(reg));
128 }
129 }
David Srbecky527c9c72015-04-17 21:14:10 +0100130 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000131 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100132 return;
133 }
134 case kArm64: {
135 DebugFrameOpCodeWriter<> opcodes;
136 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
137 // core registers.
138 for (int reg = 0; reg < 30; reg++) {
139 if (reg < 8 || reg == 16 || reg == 17) {
140 opcodes.Undefined(Reg::Arm64Core(reg));
141 } else {
142 opcodes.SameValue(Reg::Arm64Core(reg));
143 }
144 }
145 // fp registers.
146 for (int reg = 0; reg < 32; reg++) {
147 if (reg < 8 || reg >= 16) {
148 opcodes.Undefined(Reg::Arm64Fp(reg));
149 } else {
150 opcodes.SameValue(Reg::Arm64Fp(reg));
151 }
152 }
David Srbecky527c9c72015-04-17 21:14:10 +0100153 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100155 return;
156 }
157 case kMips:
158 case kMips64: {
159 DebugFrameOpCodeWriter<> opcodes;
160 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
161 // core registers.
162 for (int reg = 1; reg < 26; reg++) {
163 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
164 opcodes.Undefined(Reg::MipsCore(reg));
165 } else {
166 opcodes.SameValue(Reg::MipsCore(reg));
167 }
168 }
David Srbeckyba702002016-02-01 18:15:29 +0000169 // fp registers.
170 for (int reg = 0; reg < 32; reg++) {
171 if (reg < 24) {
172 opcodes.Undefined(Reg::Mips64Fp(reg));
173 } else {
174 opcodes.SameValue(Reg::Mips64Fp(reg));
175 }
176 }
David Srbecky527c9c72015-04-17 21:14:10 +0100177 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000178 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100179 return;
180 }
181 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100182 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
183 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100184 DebugFrameOpCodeWriter<> opcodes;
185 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
186 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
187 // core registers.
188 for (int reg = 0; reg < 8; reg++) {
189 if (reg <= 3) {
190 opcodes.Undefined(Reg::X86Core(reg));
191 } else if (reg == 4) {
192 // Stack pointer.
193 } else {
194 opcodes.SameValue(Reg::X86Core(reg));
195 }
196 }
197 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100198 if (generate_opcodes_for_x86_fp) {
199 for (int reg = 0; reg < 8; reg++) {
200 opcodes.Undefined(Reg::X86Fp(reg));
201 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100202 }
David Srbecky527c9c72015-04-17 21:14:10 +0100203 auto return_reg = Reg::X86Core(8); // R8(EIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000204 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100205 return;
206 }
207 case kX86_64: {
208 DebugFrameOpCodeWriter<> opcodes;
209 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
210 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
211 // core registers.
212 for (int reg = 0; reg < 16; reg++) {
213 if (reg == 4) {
214 // Stack pointer.
215 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
216 opcodes.Undefined(Reg::X86_64Core(reg));
217 } else {
218 opcodes.SameValue(Reg::X86_64Core(reg));
219 }
220 }
221 // fp registers.
222 for (int reg = 0; reg < 16; reg++) {
223 if (reg < 12) {
224 opcodes.Undefined(Reg::X86_64Fp(reg));
225 } else {
226 opcodes.SameValue(Reg::X86_64Fp(reg));
227 }
228 }
David Srbecky527c9c72015-04-17 21:14:10 +0100229 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000230 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100231 return;
232 }
233 case kNone:
234 break;
235 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000236 LOG(FATAL) << "Cannot write CIE frame for ISA " << isa;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100237 UNREACHABLE();
238}
239
David Srbecky6d8c8f02015-10-26 10:57:09 +0000240template<typename ElfTypes>
241void WriteCFISection(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000242 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky5e974a62016-01-22 14:25:03 +0000243 CFIFormat format,
244 bool write_oat_patches) {
David Srbeckye0febdf2015-12-17 20:53:07 +0000245 CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000246 typedef typename ElfTypes::Addr Elf_Addr;
247
Tamas Berghammer86e42782016-01-05 14:29:02 +0000248 if (method_infos.empty()) {
249 return;
250 }
251
David Srbecky6d8c8f02015-10-26 10:57:09 +0000252 std::vector<uint32_t> binary_search_table;
253 std::vector<uintptr_t> patch_locations;
254 if (format == DW_EH_FRAME_FORMAT) {
255 binary_search_table.reserve(2 * method_infos.size());
256 } else {
257 patch_locations.reserve(method_infos.size());
258 }
David Srbecky527c9c72015-04-17 21:14:10 +0100259
David Srbecky5e974a62016-01-22 14:25:03 +0000260 // The methods can be written any order.
261 // Let's therefore sort them in the lexicographical order of the opcodes.
262 // This has no effect on its own. However, if the final .debug_frame section is
263 // compressed it reduces the size since similar opcodes sequences are grouped.
264 std::vector<const MethodDebugInfo*> sorted_method_infos;
265 sorted_method_infos.reserve(method_infos.size());
266 for (size_t i = 0; i < method_infos.size(); i++) {
267 sorted_method_infos.push_back(&method_infos[i]);
268 }
269 std::sort(
270 sorted_method_infos.begin(),
271 sorted_method_infos.end(),
272 [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) {
273 ArrayRef<const uint8_t> l = lhs->compiled_method_->GetCFIInfo();
274 ArrayRef<const uint8_t> r = rhs->compiled_method_->GetCFIInfo();
275 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
276 });
277
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100278 // Write .eh_frame/.debug_frame section.
David Srbeckye0febdf2015-12-17 20:53:07 +0000279 auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000280 ? builder->GetDebugFrame()
281 : builder->GetEhFrame());
282 {
283 cfi_section->Start();
284 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +0000285 const Elf_Addr text_address = builder->GetText()->Exists()
286 ? builder->GetText()->GetAddress()
287 : 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000288 const Elf_Addr cfi_address = cfi_section->GetAddress();
289 const Elf_Addr cie_address = cfi_address;
290 Elf_Addr buffer_address = cfi_address;
291 std::vector<uint8_t> buffer; // Small temporary buffer.
292 WriteCIE(builder->GetIsa(), format, &buffer);
293 cfi_section->WriteFully(buffer.data(), buffer.size());
294 buffer_address += buffer.size();
295 buffer.clear();
David Srbecky5e974a62016-01-22 14:25:03 +0000296 for (const MethodDebugInfo* mi : sorted_method_infos) {
297 if (!mi->deduped_) { // Only one FDE per unique address.
298 ArrayRef<const uint8_t> opcodes = mi->compiled_method_->GetCFIInfo();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000299 if (!opcodes.empty()) {
David Srbecky5e974a62016-01-22 14:25:03 +0000300 const Elf_Addr code_address = text_address + mi->low_pc_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000301 if (format == DW_EH_FRAME_FORMAT) {
302 binary_search_table.push_back(
303 dchecked_integral_cast<uint32_t>(code_address));
304 binary_search_table.push_back(
305 dchecked_integral_cast<uint32_t>(buffer_address));
306 }
307 WriteFDE(is64bit, cfi_address, cie_address,
David Srbecky5e974a62016-01-22 14:25:03 +0000308 code_address, mi->high_pc_ - mi->low_pc_,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000309 opcodes, format, buffer_address, &buffer,
310 &patch_locations);
311 cfi_section->WriteFully(buffer.data(), buffer.size());
312 buffer_address += buffer.size();
313 buffer.clear();
314 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100315 }
David Srbecky8dc73242015-04-12 11:40:39 +0100316 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000317 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100318 }
David Srbecky527c9c72015-04-17 21:14:10 +0100319
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100320 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000321 auto* header_section = builder->GetEhFrameHdr();
322 header_section->Start();
323 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100324 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000325 std::vector<uint8_t> buffer;
326 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100327 header.PushUint8(1); // Version.
328 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
329 // so we have to use pcrel which means relative to the pointer's location.
330 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
331 // Encoding of binary search table size.
332 header.PushUint8(DW_EH_PE_udata4);
333 // Encoding of binary search table addresses - libunwind supports only this
334 // specific combination, which means relative to the start of .eh_frame_hdr.
335 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000336 // .eh_frame pointer
337 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100338 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000339 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
340 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100341 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000342 for (size_t i = 0; i < binary_search_table.size(); i++) {
343 // Make addresses section-relative since we know the header address now.
344 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100345 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000346 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
347 header_section->End();
348 } else {
David Srbecky5e974a62016-01-22 14:25:03 +0000349 if (write_oat_patches) {
350 builder->WritePatches(".debug_frame.oat_patches",
351 ArrayRef<const uintptr_t>(patch_locations));
352 }
David Srbecky033d7452015-04-30 19:57:35 +0100353 }
David Srbecky8dc73242015-04-12 11:40:39 +0100354}
355
David Srbecky996ed0b2015-11-27 10:27:11 +0000356namespace {
357 struct CompilationUnit {
358 std::vector<const MethodDebugInfo*> methods_;
359 size_t debug_line_offset_ = 0;
David Srbecky5cc349f2015-12-18 15:04:48 +0000360 uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max();
361 uintptr_t high_pc_ = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000362 };
363
David Srbeckyb06e28e2015-12-10 13:15:00 +0000364 typedef std::vector<DexFile::LocalInfo> LocalInfos;
David Srbecky996ed0b2015-11-27 10:27:11 +0000365
David Srbeckyb06e28e2015-12-10 13:15:00 +0000366 void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
367 static_cast<LocalInfos*>(ctx)->push_back(entry);
368 }
369
370 typedef std::vector<DexFile::PositionInfo> PositionInfos;
371
372 bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
373 static_cast<PositionInfos*>(ctx)->push_back(entry);
374 return false;
375 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000376
377 std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
378 std::vector<const char*> names;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000379 if (mi->code_item_ != nullptr) {
380 const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_);
381 if (stream != nullptr) {
382 DecodeUnsignedLeb128(&stream); // line.
383 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
384 for (uint32_t i = 0; i < parameters_size; ++i) {
385 uint32_t id = DecodeUnsignedLeb128P1(&stream);
386 names.push_back(mi->dex_file_->StringDataByIdx(id));
387 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000388 }
389 }
390 return names;
391 }
392
393 struct VariableLocation {
394 uint32_t low_pc;
395 uint32_t high_pc;
396 DexRegisterLocation reg_lo; // May be None if the location is unknown.
397 DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
398 };
399
400 // Get the location of given dex register (e.g. stack or machine register).
401 // Note that the location might be different based on the current pc.
402 // The result will cover all ranges where the variable is in scope.
403 std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info,
404 uint16_t vreg,
405 bool is64bitValue,
406 uint32_t dex_pc_low,
407 uint32_t dex_pc_high) {
408 std::vector<VariableLocation> variable_locations;
409
410 // Get stack maps sorted by pc (they might not be sorted internally).
411 const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
412 const StackMapEncoding encoding = code_info.ExtractEncoding();
413 std::map<uint32_t, StackMap> stack_maps;
414 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
415 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
416 DCHECK(stack_map.IsValid());
417 const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
418 DCHECK_LE(low_pc, method_info->high_pc_);
419 stack_maps.emplace(low_pc, stack_map);
420 }
421
422 // Create entries for the requested register based on stack map data.
423 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
424 const StackMap& stack_map = it->second;
425 const uint32_t low_pc = it->first;
426 auto next_it = it;
427 next_it++;
428 const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
429 : method_info->high_pc_;
430 DCHECK_LE(low_pc, high_pc);
431 if (low_pc == high_pc) {
432 continue; // Ignore if the address range is empty.
433 }
434
435 // Check that the stack map is in the requested range.
436 uint32_t dex_pc = stack_map.GetDexPc(encoding);
437 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
438 continue;
439 }
440
441 // Find the location of the dex register.
442 DexRegisterLocation reg_lo = DexRegisterLocation::None();
443 DexRegisterLocation reg_hi = DexRegisterLocation::None();
444 if (stack_map.HasDexRegisterMap(encoding)) {
445 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
446 stack_map, encoding, method_info->code_item_->registers_size_);
447 reg_lo = dex_register_map.GetDexRegisterLocation(
448 vreg, method_info->code_item_->registers_size_, code_info, encoding);
449 if (is64bitValue) {
450 reg_hi = dex_register_map.GetDexRegisterLocation(
451 vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
452 }
453 }
454
455 // Add location entry for this address range.
456 if (!variable_locations.empty() &&
457 variable_locations.back().reg_lo == reg_lo &&
458 variable_locations.back().reg_hi == reg_hi &&
459 variable_locations.back().high_pc == low_pc) {
460 // Merge with the previous entry (extend its range).
461 variable_locations.back().high_pc = high_pc;
462 } else {
463 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
464 }
465 }
466
467 return variable_locations;
468 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000469
470 bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) {
471 return method_info->compiled_method_->GetQuickCode().size() > 0 &&
472 method_info->compiled_method_->GetVmapTable().size() > 0 &&
473 method_info->compiled_method_->GetGcMap().size() == 0 &&
474 method_info->code_item_ != nullptr;
475 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000476} // namespace
David Srbecky04b05262015-11-09 18:05:48 +0000477
478// Helper class to write .debug_info and its supporting sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000479template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000480class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000481 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100482
David Srbecky04b05262015-11-09 18:05:48 +0000483 // Helper class to write one compilation unit.
484 // It holds helper methods and temporary state.
485 class CompilationUnitWriter {
486 public:
487 explicit CompilationUnitWriter(DebugInfoWriter* owner)
488 : owner_(owner),
David Srbecky24868a12016-01-29 18:59:56 +0000489 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) {
David Srbecky04b05262015-11-09 18:05:48 +0000490 }
491
492 void Write(const CompilationUnit& compilation_unit) {
493 CHECK(!compilation_unit.methods_.empty());
David Srbecky5cc349f2015-12-18 15:04:48 +0000494 const Elf_Addr text_address = owner_->builder_->GetText()->Exists()
495 ? owner_->builder_->GetText()->GetAddress()
496 : 0;
497 const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_;
David Srbecky04b05262015-11-09 18:05:48 +0000498
499 info_.StartTag(DW_TAG_compile_unit);
500 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
501 info_.WriteData1(DW_AT_language, DW_LANG_Java);
Tamas Berghammer8c557122015-12-10 15:06:25 +0000502 info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT"));
David Srbecky04b05262015-11-09 18:05:48 +0000503 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000504 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size));
David Srbecky0fd295f2015-11-16 16:39:10 +0000505 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
David Srbecky04b05262015-11-09 18:05:48 +0000506
507 const char* last_dex_class_desc = nullptr;
508 for (auto mi : compilation_unit.methods_) {
509 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000510 const DexFile::CodeItem* dex_code = mi->code_item_;
David Srbecky04b05262015-11-09 18:05:48 +0000511 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
512 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
513 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
514 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
David Srbecky996ed0b2015-11-27 10:27:11 +0000515 const bool is_static = (mi->access_flags_ & kAccStatic) != 0;
David Srbecky04b05262015-11-09 18:05:48 +0000516
517 // Enclose the method in correct class definition.
518 if (last_dex_class_desc != dex_class_desc) {
519 if (last_dex_class_desc != nullptr) {
David Srbecky7138d452016-01-29 15:02:44 +0000520 EndClassTag();
David Srbecky04b05262015-11-09 18:05:48 +0000521 }
Tamas Berghammer86e42782016-01-05 14:29:02 +0000522 // Write reference tag for the class we are about to declare.
523 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
524 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
525 size_t type_attrib_offset = info_.size();
526 info_.WriteRef4(DW_AT_type, 0);
527 info_.EndTag();
528 // Declare the class that owns this method.
529 size_t class_offset = StartClassTag(dex_class_desc);
530 info_.UpdateUint32(type_attrib_offset, class_offset);
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000531 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky04b05262015-11-09 18:05:48 +0000532 // Check that each class is defined only once.
533 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
534 CHECK(unique) << "Redefinition of " << dex_class_desc;
535 last_dex_class_desc = dex_class_desc;
536 }
537
David Srbecky04b05262015-11-09 18:05:48 +0000538 int start_depth = info_.Depth();
539 info_.StartTag(DW_TAG_subprogram);
540 WriteName(dex->GetMethodName(dex_method));
541 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000542 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_));
David Srbecky91cb54e2016-01-15 13:47:59 +0000543 std::vector<uint8_t> expr_buffer;
544 Expression expr(&expr_buffer);
545 expr.WriteOpCallFrameCfa();
546 info_.WriteExprLoc(DW_AT_frame_base, expr);
David Srbecky04b05262015-11-09 18:05:48 +0000547 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
David Srbeckyb06e28e2015-12-10 13:15:00 +0000548
549 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
550 // guarantee order or uniqueness so it is safer to iterate over them manually.
551 // DecodeDebugLocalInfo might not also be available if there is no debug info.
552 std::vector<const char*> param_names = GetParamNames(mi);
553 uint32_t arg_reg = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000554 if (!is_static) {
555 info_.StartTag(DW_TAG_formal_parameter);
556 WriteName("this");
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000557 info_.WriteFlagPresent(DW_AT_artificial);
David Srbecky996ed0b2015-11-27 10:27:11 +0000558 WriteLazyType(dex_class_desc);
David Srbeckyb06e28e2015-12-10 13:15:00 +0000559 if (dex_code != nullptr) {
560 // Write the stack location of the parameter.
561 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
562 const bool is64bitValue = false;
563 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
564 }
565 arg_reg++;
David Srbecky996ed0b2015-11-27 10:27:11 +0000566 info_.EndTag();
567 }
David Srbecky04b05262015-11-09 18:05:48 +0000568 if (dex_params != nullptr) {
569 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
570 info_.StartTag(DW_TAG_formal_parameter);
571 // Parameter names may not be always available.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000572 if (i < param_names.size()) {
David Srbecky04b05262015-11-09 18:05:48 +0000573 WriteName(param_names[i]);
574 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000575 // Write the type.
576 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
577 WriteLazyType(type_desc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000578 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
David Srbeckyb06e28e2015-12-10 13:15:00 +0000579 if (dex_code != nullptr) {
580 // Write the stack location of the parameter.
581 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
582 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
583 }
584 arg_reg += is64bitValue ? 2 : 1;
David Srbecky04b05262015-11-09 18:05:48 +0000585 info_.EndTag();
586 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000587 if (dex_code != nullptr) {
588 DCHECK_EQ(arg_reg, dex_code->ins_size_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000589 }
David Srbecky04b05262015-11-09 18:05:48 +0000590 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000591
592 // Write local variables.
593 LocalInfos local_infos;
594 if (dex->DecodeDebugLocalInfo(dex_code,
595 is_static,
596 mi->dex_method_index_,
597 LocalInfoCallback,
598 &local_infos)) {
599 for (const DexFile::LocalInfo& var : local_infos) {
600 if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) {
601 info_.StartTag(DW_TAG_variable);
602 WriteName(var.name_);
603 WriteLazyType(var.descriptor_);
604 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
605 WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_,
606 var.start_address_, var.end_address_);
607 info_.EndTag();
608 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000609 }
610 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000611
David Srbecky04b05262015-11-09 18:05:48 +0000612 info_.EndTag();
613 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
614 }
615 if (last_dex_class_desc != nullptr) {
David Srbecky7138d452016-01-29 15:02:44 +0000616 EndClassTag();
David Srbecky04b05262015-11-09 18:05:48 +0000617 }
David Srbecky04b05262015-11-09 18:05:48 +0000618 FinishLazyTypes();
David Srbecky7138d452016-01-29 15:02:44 +0000619 CloseNamespacesAboveDepth(0);
David Srbecky04b05262015-11-09 18:05:48 +0000620 info_.EndTag(); // DW_TAG_compile_unit
David Srbecky7138d452016-01-29 15:02:44 +0000621 CHECK_EQ(info_.Depth(), 0);
David Srbecky04b05262015-11-09 18:05:48 +0000622 std::vector<uint8_t> buffer;
623 buffer.reserve(info_.data()->size() + KB);
624 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
David Srbecky24868a12016-01-29 18:59:56 +0000625 // All compilation units share single table which is at the start of .debug_abbrev.
626 const size_t debug_abbrev_offset = 0;
David Srbecky04b05262015-11-09 18:05:48 +0000627 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
628 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
629 }
630
Tamas Berghammer86e42782016-01-05 14:29:02 +0000631 void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
632 info_.StartTag(DW_TAG_compile_unit);
633 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
634 info_.WriteData1(DW_AT_language, DW_LANG_Java);
635
David Srbeckyc7eecf92016-02-01 14:53:48 +0000636 // Base class references to be patched at the end.
637 std::map<size_t, mirror::Class*> base_class_references;
638
639 // Already written declarations or definitions.
640 std::map<mirror::Class*, size_t> class_declarations;
641
David Srbecky9b3607d2016-01-14 18:15:54 +0000642 std::vector<uint8_t> expr_buffer;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000643 for (mirror::Class* type : types) {
644 if (type->IsPrimitive()) {
645 // For primitive types the definition and the declaration is the same.
646 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
647 WriteTypeDeclaration(type->GetDescriptor(nullptr));
648 }
649 } else if (type->IsArrayClass()) {
650 mirror::Class* element_type = type->GetComponentType();
651 uint32_t component_size = type->GetComponentSize();
652 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
653 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
654
David Srbecky7138d452016-01-29 15:02:44 +0000655 CloseNamespacesAboveDepth(0); // Declare in root namespace.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000656 info_.StartTag(DW_TAG_array_type);
657 std::string descriptor_string;
658 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
David Srbecky9b3607d2016-01-14 18:15:54 +0000659 WriteLinkageName(type);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000660 info_.WriteUdata(DW_AT_data_member_location, data_offset);
661 info_.StartTag(DW_TAG_subrange_type);
David Srbecky9b3607d2016-01-14 18:15:54 +0000662 Expression count_expr(&expr_buffer);
David Srbecky91cb54e2016-01-15 13:47:59 +0000663 count_expr.WriteOpPushObjectAddress();
664 count_expr.WriteOpPlusUconst(length_offset);
665 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
666 info_.WriteExprLoc(DW_AT_count, count_expr);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000667 info_.EndTag(); // DW_TAG_subrange_type.
668 info_.EndTag(); // DW_TAG_array_type.
David Srbecky9b3607d2016-01-14 18:15:54 +0000669 } else if (type->IsInterface()) {
670 // Skip. Variables cannot have an interface as a dynamic type.
671 // We do not expose the interface information to the debugger in any way.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000672 } else {
673 std::string descriptor_string;
674 const char* desc = type->GetDescriptor(&descriptor_string);
David Srbeckyc7eecf92016-02-01 14:53:48 +0000675 size_t class_offset = StartClassTag(desc);
676 class_declarations.emplace(type, class_offset);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000677
678 if (!type->IsVariableSize()) {
679 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
680 }
681
David Srbecky9b3607d2016-01-14 18:15:54 +0000682 WriteLinkageName(type);
683
684 if (type->IsObjectClass()) {
685 // Generate artificial member which is used to get the dynamic type of variable.
686 // The run-time value of this field will correspond to linkage name of some type.
687 // We need to do it only once in j.l.Object since all other types inherit it.
688 info_.StartTag(DW_TAG_member);
689 WriteName(".dynamic_type");
690 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000691 info_.WriteFlagPresent(DW_AT_artificial);
David Srbecky9b3607d2016-01-14 18:15:54 +0000692 // Create DWARF expression to get the value of the methods_ field.
693 Expression expr(&expr_buffer);
694 // The address of the object has been implicitly pushed on the stack.
695 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
696 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
697 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
698 expr.WriteOpDerefSize(4);
699 if (kPoisonHeapReferences) {
700 expr.WriteOpNeg();
701 // DWARF stack is pointer sized. Ensure that the high bits are clear.
702 expr.WriteOpConstu(0xFFFFFFFF);
703 expr.WriteOpAnd();
704 }
705 // Add offset to the methods_ field.
706 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
707 // Top of stack holds the location of the field now.
708 info_.WriteExprLoc(DW_AT_data_member_location, expr);
709 info_.EndTag(); // DW_TAG_member.
710 }
711
Tamas Berghammer86e42782016-01-05 14:29:02 +0000712 // Base class.
David Srbeckyc7eecf92016-02-01 14:53:48 +0000713 mirror::Class* base_class = type->GetSuperClass();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000714 if (base_class != nullptr) {
715 info_.StartTag(DW_TAG_inheritance);
David Srbeckyc7eecf92016-02-01 14:53:48 +0000716 base_class_references.emplace(info_.size(), base_class);
717 info_.WriteRef4(DW_AT_type, 0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000718 info_.WriteUdata(DW_AT_data_member_location, 0);
719 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
720 info_.EndTag(); // DW_TAG_inheritance.
721 }
722
723 // Member variables.
724 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
725 ArtField* field = type->GetInstanceField(i);
726 info_.StartTag(DW_TAG_member);
727 WriteName(field->GetName());
728 WriteLazyType(field->GetTypeDescriptor());
729 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
730 uint32_t access_flags = field->GetAccessFlags();
731 if (access_flags & kAccPublic) {
732 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
733 } else if (access_flags & kAccProtected) {
734 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
735 } else if (access_flags & kAccPrivate) {
736 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
737 }
738 info_.EndTag(); // DW_TAG_member.
739 }
740
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000741 if (type->IsStringClass()) {
742 // Emit debug info about an artifical class member for java.lang.String which represents
743 // the first element of the data stored in a string instance. Consumers of the debug
744 // info will be able to read the content of java.lang.String based on the count (real
745 // field) and based on the location of this data member.
746 info_.StartTag(DW_TAG_member);
747 WriteName("value");
748 // We don't support fields with C like array types so we just say its type is java char.
749 WriteLazyType("C"); // char.
750 info_.WriteUdata(DW_AT_data_member_location,
751 mirror::String::ValueOffset().Uint32Value());
752 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
753 info_.EndTag(); // DW_TAG_member.
754 }
755
David Srbecky7138d452016-01-29 15:02:44 +0000756 EndClassTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000757 }
758 }
759
David Srbeckyc7eecf92016-02-01 14:53:48 +0000760 // Write base class declarations.
761 for (const auto& base_class_reference : base_class_references) {
762 size_t reference_offset = base_class_reference.first;
763 mirror::Class* base_class = base_class_reference.second;
764 const auto& it = class_declarations.find(base_class);
765 if (it != class_declarations.end()) {
766 info_.UpdateUint32(reference_offset, it->second);
767 } else {
768 // Declare base class. We can not use the standard WriteLazyType
769 // since we want to avoid the DW_TAG_reference_tag wrapping.
770 std::string tmp_storage;
771 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
772 size_t base_class_declaration_offset = StartClassTag(base_class_desc);
773 info_.WriteFlagPresent(DW_AT_declaration);
774 WriteLinkageName(base_class);
775 EndClassTag();
776 class_declarations.emplace(base_class, base_class_declaration_offset);
777 info_.UpdateUint32(reference_offset, base_class_declaration_offset);
778 }
779 }
780
Tamas Berghammer86e42782016-01-05 14:29:02 +0000781 FinishLazyTypes();
David Srbecky7138d452016-01-29 15:02:44 +0000782 CloseNamespacesAboveDepth(0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000783 info_.EndTag(); // DW_TAG_compile_unit.
David Srbecky7138d452016-01-29 15:02:44 +0000784 CHECK_EQ(info_.Depth(), 0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000785 std::vector<uint8_t> buffer;
786 buffer.reserve(info_.data()->size() + KB);
787 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
David Srbecky24868a12016-01-29 18:59:56 +0000788 // All compilation units share single table which is at the start of .debug_abbrev.
789 const size_t debug_abbrev_offset = 0;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000790 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
791 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
792 }
793
David Srbecky9b3607d2016-01-14 18:15:54 +0000794 // Linkage name uniquely identifies type.
795 // It is used to determine the dynamic type of objects.
796 // We use the methods_ field of class since it is unique and it is not moved by the GC.
797 void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
798 auto* methods_ptr = type->GetMethodsPtr();
799 if (methods_ptr == nullptr) {
800 // Some types might have no methods. Allocate empty array instead.
801 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
802 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
803 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
804 type->SetMethodsPtr(methods_ptr, 0, 0);
805 DCHECK(type->GetMethodsPtr() != nullptr);
806 }
807 char name[32];
808 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
809 info_.WriteString(DW_AT_linkage_name, name);
810 }
811
David Srbecky0fd295f2015-11-16 16:39:10 +0000812 // Write table into .debug_loc which describes location of dex register.
813 // The dex register might be valid only at some points and it might
814 // move between machine registers and stack.
David Srbecky996ed0b2015-11-27 10:27:11 +0000815 void WriteRegLocation(const MethodDebugInfo* method_info,
816 uint16_t vreg,
817 bool is64bitValue,
818 uint32_t compilation_unit_low_pc,
819 uint32_t dex_pc_low = 0,
820 uint32_t dex_pc_high = 0xFFFFFFFF) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000821 using Kind = DexRegisterLocation::Kind;
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000822 if (!IsFromOptimizingCompiler(method_info)) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000823 return;
824 }
825
David Srbecky996ed0b2015-11-27 10:27:11 +0000826 Writer<> debug_loc(&owner_->debug_loc_);
827 Writer<> debug_ranges(&owner_->debug_ranges_);
828 info_.WriteSecOffset(DW_AT_location, debug_loc.size());
829 info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size());
David Srbecky0fd295f2015-11-16 16:39:10 +0000830
David Srbecky996ed0b2015-11-27 10:27:11 +0000831 std::vector<VariableLocation> variable_locations = GetVariableLocations(
832 method_info,
833 vreg,
834 is64bitValue,
835 dex_pc_low,
836 dex_pc_high);
837
838 // Write .debug_loc entries.
David Srbecky0fd295f2015-11-16 16:39:10 +0000839 const InstructionSet isa = owner_->builder_->GetIsa();
840 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky91cb54e2016-01-15 13:47:59 +0000841 std::vector<uint8_t> expr_buffer;
David Srbecky996ed0b2015-11-27 10:27:11 +0000842 for (const VariableLocation& variable_location : variable_locations) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000843 // Translate dex register location to DWARF expression.
844 // Note that 64-bit value might be split to two distinct locations.
845 // (for example, two 32-bit machine registers, or even stack and register)
David Srbecky91cb54e2016-01-15 13:47:59 +0000846 Expression expr(&expr_buffer);
David Srbecky996ed0b2015-11-27 10:27:11 +0000847 DexRegisterLocation reg_lo = variable_location.reg_lo;
848 DexRegisterLocation reg_hi = variable_location.reg_hi;
David Srbecky0fd295f2015-11-16 16:39:10 +0000849 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
850 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
851 const Kind kind = reg_loc.GetKind();
852 const int32_t value = reg_loc.GetValue();
853 if (kind == Kind::kInStack) {
854 const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
David Srbecky0fd295f2015-11-16 16:39:10 +0000855 // The stack offset is relative to SP. Make it relative to CFA.
David Srbecky91cb54e2016-01-15 13:47:59 +0000856 expr.WriteOpFbreg(value - frame_size);
David Srbecky0fd295f2015-11-16 16:39:10 +0000857 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
858 reg_hi.GetValue() == value + 4) {
859 break; // the high word is correctly implied by the low word.
860 }
861 } else if (kind == Kind::kInRegister) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000862 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000863 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
864 reg_hi.GetValue() == value) {
865 break; // the high word is correctly implied by the low word.
866 }
867 } else if (kind == Kind::kInFpuRegister) {
868 if ((isa == kArm || isa == kThumb2) &&
869 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
870 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
871 // Translate S register pair to D register (e.g. S4+S5 to D2).
David Srbecky91cb54e2016-01-15 13:47:59 +0000872 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000873 break;
874 }
David Srbecky91cb54e2016-01-15 13:47:59 +0000875 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000876 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
877 reg_hi.GetValue() == reg_lo.GetValue()) {
878 break; // the high word is correctly implied by the low word.
879 }
880 } else if (kind == Kind::kConstant) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000881 expr.WriteOpConsts(value);
882 expr.WriteOpStackValue();
David Srbecky0fd295f2015-11-16 16:39:10 +0000883 } else if (kind == Kind::kNone) {
884 break;
885 } else {
886 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
887 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
888 // the special cases above and they should not occur alone.
889 LOG(ERROR) << "Unexpected register location kind: "
890 << DexRegisterLocation::PrettyDescriptor(kind);
891 break;
892 }
893 if (is64bitValue) {
894 // Write the marker which is needed by split 64-bit values.
895 // This code is skipped by the special cases.
David Srbecky91cb54e2016-01-15 13:47:59 +0000896 expr.WriteOpPiece(4);
David Srbecky0fd295f2015-11-16 16:39:10 +0000897 }
898 }
899
David Srbecky91cb54e2016-01-15 13:47:59 +0000900 if (expr.size() > 0) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000901 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000902 debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
903 debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000904 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000905 debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
906 debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000907 }
908 // Write the expression.
David Srbecky91cb54e2016-01-15 13:47:59 +0000909 debug_loc.PushUint16(expr.size());
910 debug_loc.PushData(expr.data());
David Srbecky0fd295f2015-11-16 16:39:10 +0000911 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000912 // Do not generate .debug_loc if the location is not known.
David Srbecky0fd295f2015-11-16 16:39:10 +0000913 }
914 }
915 // Write end-of-list entry.
916 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000917 debug_loc.PushUint64(0);
918 debug_loc.PushUint64(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000919 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000920 debug_loc.PushUint32(0);
921 debug_loc.PushUint32(0);
922 }
923
924 // Write .debug_ranges entries.
925 // This includes ranges where the variable is in scope but the location is not known.
926 for (size_t i = 0; i < variable_locations.size(); i++) {
927 uint32_t low_pc = variable_locations[i].low_pc;
928 uint32_t high_pc = variable_locations[i].high_pc;
929 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
930 // Merge address range with the next entry.
931 high_pc = variable_locations[++i].high_pc;
932 }
933 if (is64bit) {
934 debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
935 debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
936 } else {
937 debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
938 debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
939 }
940 }
941 // Write end-of-list entry.
942 if (is64bit) {
943 debug_ranges.PushUint64(0);
944 debug_ranges.PushUint64(0);
945 } else {
946 debug_ranges.PushUint32(0);
947 debug_ranges.PushUint32(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000948 }
949 }
950
David Srbecky04b05262015-11-09 18:05:48 +0000951 // Some types are difficult to define as we go since they need
952 // to be enclosed in the right set of namespaces. Therefore we
953 // just define all types lazily at the end of compilation unit.
954 void WriteLazyType(const char* type_descriptor) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000955 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000956 lazy_types_.emplace(std::string(type_descriptor), info_.size());
David Srbecky04b05262015-11-09 18:05:48 +0000957 info_.WriteRef4(DW_AT_type, 0);
958 }
959 }
960
961 void FinishLazyTypes() {
962 for (const auto& lazy_type : lazy_types_) {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000963 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
David Srbecky04b05262015-11-09 18:05:48 +0000964 }
965 lazy_types_.clear();
966 }
967
968 private:
969 void WriteName(const char* name) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000970 if (name != nullptr) {
971 info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
972 }
David Srbecky04b05262015-11-09 18:05:48 +0000973 }
974
975 // Convert dex type descriptor to DWARF.
976 // Returns offset in the compilation unit.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000977 size_t WriteTypeDeclaration(const std::string& desc) {
978 DCHECK(!desc.empty());
David Srbecky04b05262015-11-09 18:05:48 +0000979 const auto& it = type_cache_.find(desc);
980 if (it != type_cache_.end()) {
981 return it->second;
982 }
983
984 size_t offset;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000985 if (desc[0] == 'L') {
David Srbecky04b05262015-11-09 18:05:48 +0000986 // Class type. For example: Lpackage/name;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000987 size_t class_offset = StartClassTag(desc.c_str());
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000988 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky7138d452016-01-29 15:02:44 +0000989 EndClassTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000990 // Reference to the class type.
991 offset = info_.StartTag(DW_TAG_reference_type);
992 info_.WriteRef(DW_AT_type, class_offset);
993 info_.EndTag();
994 } else if (desc[0] == '[') {
David Srbecky04b05262015-11-09 18:05:48 +0000995 // Array type.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000996 size_t element_type = WriteTypeDeclaration(desc.substr(1));
David Srbecky7138d452016-01-29 15:02:44 +0000997 CloseNamespacesAboveDepth(0); // Declare in root namespace.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000998 size_t array_type = info_.StartTag(DW_TAG_array_type);
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000999 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky04b05262015-11-09 18:05:48 +00001000 info_.WriteRef(DW_AT_type, element_type);
1001 info_.EndTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +00001002 offset = info_.StartTag(DW_TAG_reference_type);
1003 info_.WriteRef4(DW_AT_type, array_type);
1004 info_.EndTag();
David Srbecky04b05262015-11-09 18:05:48 +00001005 } else {
1006 // Primitive types.
Tamas Berghammer03c941f2016-01-15 13:39:57 +00001007 DCHECK_EQ(desc.size(), 1u);
1008
David Srbecky04b05262015-11-09 18:05:48 +00001009 const char* name;
David Srbecky0fd295f2015-11-16 16:39:10 +00001010 uint32_t encoding;
1011 uint32_t byte_size;
Tamas Berghammer86e42782016-01-05 14:29:02 +00001012 switch (desc[0]) {
David Srbecky0fd295f2015-11-16 16:39:10 +00001013 case 'B':
1014 name = "byte";
1015 encoding = DW_ATE_signed;
1016 byte_size = 1;
1017 break;
1018 case 'C':
1019 name = "char";
1020 encoding = DW_ATE_UTF;
1021 byte_size = 2;
1022 break;
1023 case 'D':
1024 name = "double";
1025 encoding = DW_ATE_float;
1026 byte_size = 8;
1027 break;
1028 case 'F':
1029 name = "float";
1030 encoding = DW_ATE_float;
1031 byte_size = 4;
1032 break;
1033 case 'I':
1034 name = "int";
1035 encoding = DW_ATE_signed;
1036 byte_size = 4;
1037 break;
1038 case 'J':
1039 name = "long";
1040 encoding = DW_ATE_signed;
1041 byte_size = 8;
1042 break;
1043 case 'S':
1044 name = "short";
1045 encoding = DW_ATE_signed;
1046 byte_size = 2;
1047 break;
1048 case 'Z':
1049 name = "boolean";
1050 encoding = DW_ATE_boolean;
1051 byte_size = 1;
1052 break;
1053 case 'V':
1054 LOG(FATAL) << "Void type should not be encoded";
1055 UNREACHABLE();
David Srbecky04b05262015-11-09 18:05:48 +00001056 default:
Tamas Berghammer86e42782016-01-05 14:29:02 +00001057 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
David Srbecky04b05262015-11-09 18:05:48 +00001058 UNREACHABLE();
1059 }
David Srbecky7138d452016-01-29 15:02:44 +00001060 CloseNamespacesAboveDepth(0); // Declare in root namespace.
David Srbecky04b05262015-11-09 18:05:48 +00001061 offset = info_.StartTag(DW_TAG_base_type);
1062 WriteName(name);
David Srbecky0fd295f2015-11-16 16:39:10 +00001063 info_.WriteData1(DW_AT_encoding, encoding);
1064 info_.WriteData1(DW_AT_byte_size, byte_size);
David Srbecky04b05262015-11-09 18:05:48 +00001065 info_.EndTag();
1066 }
1067
1068 type_cache_.emplace(desc, offset);
1069 return offset;
1070 }
1071
1072 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
1073 // Returns offset of the class tag in the compilation unit.
1074 size_t StartClassTag(const char* desc) {
David Srbecky7138d452016-01-29 15:02:44 +00001075 std::string name = SetNamespaceForClass(desc);
David Srbecky04b05262015-11-09 18:05:48 +00001076 size_t offset = info_.StartTag(DW_TAG_class_type);
David Srbecky7138d452016-01-29 15:02:44 +00001077 WriteName(name.c_str());
David Srbecky04b05262015-11-09 18:05:48 +00001078 return offset;
1079 }
1080
David Srbecky7138d452016-01-29 15:02:44 +00001081 void EndClassTag() {
David Srbecky04b05262015-11-09 18:05:48 +00001082 info_.EndTag();
David Srbecky7138d452016-01-29 15:02:44 +00001083 }
1084
1085 // Set the current namespace nesting to one required by the given class.
1086 // Returns the class name with namespaces, 'L', and ';' stripped.
1087 std::string SetNamespaceForClass(const char* desc) {
1088 DCHECK(desc != nullptr && desc[0] == 'L');
1089 desc++; // Skip the initial 'L'.
1090 size_t depth = 0;
1091 for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) {
1092 // Check whether the name at this depth is already what we need.
1093 if (depth < current_namespace_.size()) {
1094 const std::string& name = current_namespace_[depth];
1095 if (name.compare(0, name.size(), desc, end - desc) == 0) {
1096 continue;
1097 }
1098 }
1099 // Otherwise we need to open a new namespace tag at this depth.
1100 CloseNamespacesAboveDepth(depth);
1101 info_.StartTag(DW_TAG_namespace);
1102 std::string name(desc, end - desc);
1103 WriteName(name.c_str());
1104 current_namespace_.push_back(std::move(name));
1105 }
1106 CloseNamespacesAboveDepth(depth);
1107 return std::string(desc, strchr(desc, ';') - desc);
1108 }
1109
1110 // Close namespace tags to reach the given nesting depth.
1111 void CloseNamespacesAboveDepth(size_t depth) {
1112 DCHECK_LE(depth, current_namespace_.size());
1113 while (current_namespace_.size() > depth) {
David Srbecky04b05262015-11-09 18:05:48 +00001114 info_.EndTag();
David Srbecky7138d452016-01-29 15:02:44 +00001115 current_namespace_.pop_back();
David Srbecky04b05262015-11-09 18:05:48 +00001116 }
1117 }
1118
1119 // For access to the ELF sections.
1120 DebugInfoWriter<ElfTypes>* owner_;
David Srbecky04b05262015-11-09 18:05:48 +00001121 // Temporary buffer to create and store the entries.
1122 DebugInfoEntryWriter<> info_;
1123 // Cache of already translated type descriptors.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001124 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001125 // 32-bit references which need to be resolved to a type later.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001126 // Given type may be used multiple times. Therefore we need a multimap.
1127 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
David Srbecky7138d452016-01-29 15:02:44 +00001128 // The current set of open namespace tags which are active and not closed yet.
1129 std::vector<std::string> current_namespace_;
David Srbecky04b05262015-11-09 18:05:48 +00001130 };
1131
David Srbeckyb851b492015-11-11 20:19:38 +00001132 public:
David Srbecky24868a12016-01-29 18:59:56 +00001133 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder)
1134 : builder_(builder),
1135 debug_abbrev_(&debug_abbrev_buffer_) {
David Srbecky626a1662015-04-12 13:12:26 +01001136 }
1137
David Srbeckyb851b492015-11-11 20:19:38 +00001138 void Start() {
1139 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +01001140 }
1141
David Srbecky04b05262015-11-09 18:05:48 +00001142 void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
1143 CompilationUnitWriter writer(this);
1144 writer.Write(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001145 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001146
Tamas Berghammer86e42782016-01-05 14:29:02 +00001147 void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
1148 CompilationUnitWriter writer(this);
1149 writer.Write(types);
1150 }
1151
David Srbeckyb851b492015-11-11 20:19:38 +00001152 void End() {
1153 builder_->GetDebugInfo()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001154 builder_->WritePatches(".debug_info.oat_patches",
1155 ArrayRef<const uintptr_t>(debug_info_patches_));
David Srbecky24868a12016-01-29 18:59:56 +00001156 builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_);
David Srbecky04b05262015-11-09 18:05:48 +00001157 builder_->WriteSection(".debug_str", &debug_str_.Data());
David Srbecky0fd295f2015-11-16 16:39:10 +00001158 builder_->WriteSection(".debug_loc", &debug_loc_);
David Srbecky996ed0b2015-11-27 10:27:11 +00001159 builder_->WriteSection(".debug_ranges", &debug_ranges_);
David Srbeckyb851b492015-11-11 20:19:38 +00001160 }
1161
1162 private:
David Srbecky04b05262015-11-09 18:05:48 +00001163 size_t WriteString(const char* str) {
1164 return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
1165 }
1166
David Srbeckyb851b492015-11-11 20:19:38 +00001167 ElfBuilder<ElfTypes>* builder_;
1168 std::vector<uintptr_t> debug_info_patches_;
David Srbecky24868a12016-01-29 18:59:56 +00001169 std::vector<uint8_t> debug_abbrev_buffer_;
1170 DebugAbbrevWriter<> debug_abbrev_;
David Srbecky04b05262015-11-09 18:05:48 +00001171 DedupVector debug_str_;
David Srbecky0fd295f2015-11-16 16:39:10 +00001172 std::vector<uint8_t> debug_loc_;
David Srbecky996ed0b2015-11-27 10:27:11 +00001173 std::vector<uint8_t> debug_ranges_;
David Srbecky04b05262015-11-09 18:05:48 +00001174
1175 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
David Srbeckyb851b492015-11-11 20:19:38 +00001176};
1177
1178template<typename ElfTypes>
1179class DebugLineWriter {
1180 typedef typename ElfTypes::Addr Elf_Addr;
1181
1182 public:
1183 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
1184 }
1185
1186 void Start() {
1187 builder_->GetDebugLine()->Start();
1188 }
1189
1190 // Write line table for given set of methods.
1191 // Returns the number of bytes written.
David Srbecky04b05262015-11-09 18:05:48 +00001192 size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
David Srbeckyb851b492015-11-11 20:19:38 +00001193 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +00001194 const Elf_Addr text_address = builder_->GetText()->Exists()
1195 ? builder_->GetText()->GetAddress()
1196 : 0;
David Srbecky04b05262015-11-09 18:05:48 +00001197
1198 compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
David Srbeckyb851b492015-11-11 20:19:38 +00001199
David Srbecky799b8c42015-04-14 01:57:43 +01001200 std::vector<FileEntry> files;
1201 std::unordered_map<std::string, size_t> files_map;
1202 std::vector<std::string> directories;
1203 std::unordered_map<std::string, size_t> directories_map;
1204 int code_factor_bits_ = 0;
1205 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +00001206 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +01001207 case kArm: // arm actually means thumb2.
1208 case kThumb2:
1209 code_factor_bits_ = 1; // 16-bit instuctions
1210 dwarf_isa = 1; // DW_ISA_ARM_thumb.
1211 break;
1212 case kArm64:
1213 case kMips:
1214 case kMips64:
1215 code_factor_bits_ = 2; // 32-bit instructions
1216 break;
1217 case kNone:
1218 case kX86:
1219 case kX86_64:
1220 break;
1221 }
David Srbecky297ed222015-04-15 01:18:12 +01001222 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
Vladimir Marko10c13562015-11-25 14:33:36 +00001223 for (const MethodDebugInfo* mi : compilation_unit.methods_) {
David Srbecky04b05262015-11-09 18:05:48 +00001224 // Ignore function if we have already generated line table for the same address.
1225 // It would confuse the debugger and the DWARF specification forbids it.
1226 if (mi->deduped_) {
1227 continue;
1228 }
1229
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001230 ArrayRef<const SrcMapElem> src_mapping_table;
1231 std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
1232 if (IsFromOptimizingCompiler(mi)) {
1233 // Use stack maps to create mapping table from pc to dex.
1234 const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data());
1235 const StackMapEncoding encoding = code_info.ExtractEncoding();
1236 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
1237 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
1238 DCHECK(stack_map.IsValid());
David Srbecky4f50ee22016-01-25 17:00:24 +00001239 // Emit only locations where we have local-variable information.
1240 // In particular, skip mappings inside the prologue.
1241 if (stack_map.HasDexRegisterMap(encoding)) {
1242 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
1243 const int32_t dex = stack_map.GetDexPc(encoding);
1244 src_mapping_table_from_stack_maps.push_back({pc, dex});
1245 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001246 }
1247 std::sort(src_mapping_table_from_stack_maps.begin(),
1248 src_mapping_table_from_stack_maps.end());
1249 src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
1250 } else {
1251 // Use the mapping table provided by the quick compiler.
1252 src_mapping_table = mi->compiled_method_->GetSrcMappingTable();
1253 }
1254
1255 if (src_mapping_table.empty()) {
1256 continue;
1257 }
1258
David Srbecky6d8c8f02015-10-26 10:57:09 +00001259 Elf_Addr method_address = text_address + mi->low_pc_;
1260
David Srbeckyb06e28e2015-12-10 13:15:00 +00001261 PositionInfos position_infos;
David Srbecky799b8c42015-04-14 01:57:43 +01001262 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001263 if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) {
1264 continue;
David Srbecky3b9d57a2015-04-10 00:22:14 +01001265 }
1266
David Srbeckyb06e28e2015-12-10 13:15:00 +00001267 if (position_infos.empty()) {
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001268 continue;
1269 }
1270
1271 opcodes.SetAddress(method_address);
1272 if (dwarf_isa != -1) {
1273 opcodes.SetISA(dwarf_isa);
1274 }
1275
David Srbecky799b8c42015-04-14 01:57:43 +01001276 // Get and deduplicate directory and filename.
1277 int file_index = 0; // 0 - primary source file of the compilation.
1278 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
1279 const char* source_file = dex->GetSourceFile(dex_class_def);
1280 if (source_file != nullptr) {
1281 std::string file_name(source_file);
1282 size_t file_name_slash = file_name.find_last_of('/');
1283 std::string class_name(dex->GetClassDescriptor(dex_class_def));
1284 size_t class_name_slash = class_name.find_last_of('/');
1285 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001286
David Srbecky799b8c42015-04-14 01:57:43 +01001287 // Guess directory from package name.
1288 int directory_index = 0; // 0 - current directory of the compilation.
1289 if (file_name_slash == std::string::npos && // Just filename.
1290 class_name.front() == 'L' && // Type descriptor for a class.
1291 class_name_slash != std::string::npos) { // Has package name.
1292 std::string package_name = class_name.substr(1, class_name_slash - 1);
1293 auto it = directories_map.find(package_name);
1294 if (it == directories_map.end()) {
1295 directory_index = 1 + directories.size();
1296 directories_map.emplace(package_name, directory_index);
1297 directories.push_back(package_name);
1298 } else {
1299 directory_index = it->second;
1300 }
1301 full_path = package_name + "/" + file_name;
1302 }
1303
1304 // Add file entry.
1305 auto it2 = files_map.find(full_path);
1306 if (it2 == files_map.end()) {
1307 file_index = 1 + files.size();
1308 files_map.emplace(full_path, file_index);
1309 files.push_back(FileEntry {
1310 file_name,
1311 directory_index,
1312 0, // Modification time - NA.
1313 0, // File size - NA.
1314 });
1315 } else {
1316 file_index = it2->second;
1317 }
1318 }
1319 opcodes.SetFile(file_index);
1320
1321 // Generate mapping opcodes from PC to Java lines.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001322 if (file_index != 0) {
David Srbecky799b8c42015-04-14 01:57:43 +01001323 bool first = true;
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001324 for (SrcMapElem pc2dex : src_mapping_table) {
David Srbecky799b8c42015-04-14 01:57:43 +01001325 uint32_t pc = pc2dex.from_;
1326 int dex_pc = pc2dex.to_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001327 // Find mapping with address with is greater than our dex pc; then go back one step.
1328 auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc,
1329 [](uint32_t address, const DexFile::PositionInfo& entry) {
1330 return address < entry.address_;
1331 });
1332 if (ub != position_infos.begin()) {
1333 int line = (--ub)->line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001334 if (first) {
1335 first = false;
1336 if (pc > 0) {
1337 // Assume that any preceding code is prologue.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001338 int first_line = position_infos.front().line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001339 // Prologue is not a sensible place for a breakpoint.
1340 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +00001341 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +01001342 opcodes.NegateStmt();
1343 opcodes.SetPrologueEnd();
1344 }
David Srbecky6d8c8f02015-10-26 10:57:09 +00001345 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +01001346 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +00001347 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001348 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001349 }
1350 }
David Srbecky799b8c42015-04-14 01:57:43 +01001351 } else {
1352 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +00001353 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001354 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001355
1356 opcodes.AdvancePC(text_address + mi->high_pc_);
1357 opcodes.EndSequence();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001358 }
David Srbeckyb851b492015-11-11 20:19:38 +00001359 std::vector<uint8_t> buffer;
1360 buffer.reserve(opcodes.data()->size() + KB);
1361 size_t offset = builder_->GetDebugLine()->GetSize();
1362 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
1363 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
1364 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001365 }
David Srbeckyb851b492015-11-11 20:19:38 +00001366
1367 void End() {
1368 builder_->GetDebugLine()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001369 builder_->WritePatches(".debug_line.oat_patches",
1370 ArrayRef<const uintptr_t>(debug_line_patches));
David Srbeckyb851b492015-11-11 20:19:38 +00001371 }
1372
1373 private:
1374 ElfBuilder<ElfTypes>* builder_;
1375 std::vector<uintptr_t> debug_line_patches;
1376};
1377
1378template<typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001379static void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
Tamas Berghammer86e42782016-01-05 14:29:02 +00001380 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyb851b492015-11-11 20:19:38 +00001381 // Group the methods into compilation units based on source file.
1382 std::vector<CompilationUnit> compilation_units;
1383 const char* last_source_file = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +00001384 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky04b05262015-11-09 18:05:48 +00001385 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
1386 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
1387 if (compilation_units.empty() || source_file != last_source_file) {
1388 compilation_units.push_back(CompilationUnit());
David Srbeckyb851b492015-11-11 20:19:38 +00001389 }
David Srbecky04b05262015-11-09 18:05:48 +00001390 CompilationUnit& cu = compilation_units.back();
1391 cu.methods_.push_back(&mi);
1392 cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
1393 cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
1394 last_source_file = source_file;
David Srbeckyb851b492015-11-11 20:19:38 +00001395 }
1396
1397 // Write .debug_line section.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001398 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001399 DebugLineWriter<ElfTypes> line_writer(builder);
1400 line_writer.Start();
David Srbeckyb851b492015-11-11 20:19:38 +00001401 for (auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001402 line_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001403 }
1404 line_writer.End();
1405 }
1406
1407 // Write .debug_info section.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001408 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001409 DebugInfoWriter<ElfTypes> info_writer(builder);
1410 info_writer.Start();
1411 for (const auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001412 info_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001413 }
1414 info_writer.End();
1415 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001416}
1417
David Srbeckye0febdf2015-12-17 20:53:07 +00001418template <typename ElfTypes>
David Srbecky52886112016-01-22 13:56:47 +00001419static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
1420 const ArrayRef<const MethodDebugInfo>& method_infos,
1421 bool with_signature) {
David Srbeckye0febdf2015-12-17 20:53:07 +00001422 bool generated_mapping_symbol = false;
1423 auto* strtab = builder->GetStrTab();
1424 auto* symtab = builder->GetSymTab();
1425
1426 if (method_infos.empty()) {
1427 return;
1428 }
1429
1430 // Find all addresses (low_pc) which contain deduped methods.
1431 // The first instance of method is not marked deduped_, but the rest is.
1432 std::unordered_set<uint32_t> deduped_addresses;
1433 for (const MethodDebugInfo& info : method_infos) {
1434 if (info.deduped_) {
1435 deduped_addresses.insert(info.low_pc_);
1436 }
1437 }
1438
1439 strtab->Start();
1440 strtab->Write(""); // strtab should start with empty string.
David Srbecky52886112016-01-22 13:56:47 +00001441 std::string last_name;
1442 size_t last_name_offset = 0;
David Srbeckye0febdf2015-12-17 20:53:07 +00001443 for (const MethodDebugInfo& info : method_infos) {
1444 if (info.deduped_) {
1445 continue; // Add symbol only for the first instance.
1446 }
David Srbecky52886112016-01-22 13:56:47 +00001447 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, with_signature);
David Srbeckye0febdf2015-12-17 20:53:07 +00001448 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
1449 name += " [DEDUPED]";
1450 }
David Srbecky52886112016-01-22 13:56:47 +00001451 // If we write method names without signature, we might see the same name multiple times.
1452 size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name));
David Srbeckye0febdf2015-12-17 20:53:07 +00001453
David Srbecky5cc349f2015-12-18 15:04:48 +00001454 const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr;
1455 const bool is_relative = (text != nullptr);
David Srbeckye0febdf2015-12-17 20:53:07 +00001456 uint32_t low_pc = info.low_pc_;
1457 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
1458 low_pc += info.compiled_method_->CodeDelta();
David Srbecky52886112016-01-22 13:56:47 +00001459 symtab->Add(name_offset,
1460 text,
1461 low_pc,
1462 is_relative,
1463 info.high_pc_ - info.low_pc_,
1464 STB_GLOBAL,
1465 STT_FUNC);
David Srbeckye0febdf2015-12-17 20:53:07 +00001466
1467 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
1468 // instructions, so that disassembler tools can correctly disassemble.
1469 // Note that even if we generate just a single mapping symbol, ARM's Streamline
1470 // requires it to match function symbol. Just address 0 does not work.
1471 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
1472 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001473 symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1,
1474 is_relative, 0, STB_LOCAL, STT_NOTYPE);
David Srbeckye0febdf2015-12-17 20:53:07 +00001475 generated_mapping_symbol = true;
1476 }
1477 }
David Srbecky52886112016-01-22 13:56:47 +00001478
1479 last_name = std::move(name);
1480 last_name_offset = name_offset;
David Srbeckye0febdf2015-12-17 20:53:07 +00001481 }
1482 strtab->End();
1483
1484 // Symbols are buffered and written after names (because they are smaller).
1485 // We could also do two passes in this function to avoid the buffering.
1486 symtab->Start();
1487 symtab->Write();
1488 symtab->End();
1489}
1490
1491template <typename ElfTypes>
1492void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
1493 const ArrayRef<const MethodDebugInfo>& method_infos,
1494 CFIFormat cfi_format) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001495 // Add methods to .symtab.
David Srbecky52886112016-01-22 13:56:47 +00001496 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001497 // Generate CFI (stack unwinding information).
David Srbecky5e974a62016-01-22 14:25:03 +00001498 WriteCFISection(builder, method_infos, cfi_format, true /* write_oat_patches */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001499 // Write DWARF .debug_* sections.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001500 WriteDebugSections(builder, method_infos);
David Srbeckye0febdf2015-12-17 20:53:07 +00001501}
1502
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001503static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) {
1504 // Configure the compression library.
1505 CrcGenerateTable();
1506 Crc64GenerateTable();
1507 CLzma2EncProps lzma2Props;
1508 Lzma2EncProps_Init(&lzma2Props);
1509 lzma2Props.lzmaProps.level = 1; // Fast compression.
1510 Lzma2EncProps_Normalize(&lzma2Props);
1511 CXzProps props;
1512 XzProps_Init(&props);
1513 props.lzma2Props = &lzma2Props;
1514 // Implement the required interface for communication (written in C so no virtual methods).
1515 struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress {
1516 static SRes ReadImpl(void* p, void* buf, size_t* size) {
1517 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p));
1518 *size = std::min(*size, ctx->src_->size() - ctx->src_pos_);
1519 memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size);
1520 ctx->src_pos_ += *size;
1521 return SZ_OK;
1522 }
1523 static size_t WriteImpl(void* p, const void* buf, size_t size) {
1524 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p));
1525 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
1526 ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size);
1527 return size;
1528 }
1529 static SRes ProgressImpl(void* , UInt64, UInt64) {
1530 return SZ_OK;
1531 }
1532 size_t src_pos_;
1533 const std::vector<uint8_t>* src_;
1534 std::vector<uint8_t>* dst_;
1535 };
1536 XzCallbacks callbacks;
1537 callbacks.Read = XzCallbacks::ReadImpl;
1538 callbacks.Write = XzCallbacks::WriteImpl;
1539 callbacks.Progress = XzCallbacks::ProgressImpl;
1540 callbacks.src_pos_ = 0;
1541 callbacks.src_ = src;
1542 callbacks.dst_ = dst;
1543 // Compress.
1544 SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
1545 CHECK_EQ(res, SZ_OK);
1546}
1547
1548template <typename ElfTypes>
1549void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder,
1550 const ArrayRef<const MethodDebugInfo>& method_infos) {
1551 const InstructionSet isa = parent_builder->GetIsa();
1552 std::vector<uint8_t> buffer;
1553 buffer.reserve(KB);
1554 VectorOutputStream out("Mini-debug-info ELF file", &buffer);
1555 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1556 builder->Start();
1557 // Write .rodata and .text as NOBITS sections.
1558 // This allows tools to detect virtual address relocation of the parent ELF file.
1559 builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress());
1560 builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize());
1561 builder->SetVirtualAddress(parent_builder->GetText()->GetAddress());
1562 builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize());
David Srbecky52886112016-01-22 13:56:47 +00001563 WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */);
David Srbecky5e974a62016-01-22 14:25:03 +00001564 WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001565 builder->End();
1566 CHECK(builder->Good());
1567 std::vector<uint8_t> compressed_buffer;
1568 compressed_buffer.reserve(buffer.size() / 4);
1569 XzCompress(&buffer, &compressed_buffer);
1570 parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer);
1571}
1572
David Srbecky5cc349f2015-12-18 15:04:48 +00001573template <typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001574static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal(
David Srbecky5cc349f2015-12-18 15:04:48 +00001575 const dwarf::MethodDebugInfo& method_info) {
1576 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1577 std::vector<uint8_t> buffer;
1578 buffer.reserve(KB);
1579 VectorOutputStream out("Debug ELF file", &buffer);
1580 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1581 builder->Start();
1582 WriteDebugInfo(builder.get(),
1583 ArrayRef<const MethodDebugInfo>(&method_info, 1),
1584 DW_DEBUG_FRAME_FORMAT);
1585 builder->End();
1586 CHECK(builder->Good());
1587 // Make a copy of the buffer. We want to shrink it anyway.
1588 uint8_t* result = new uint8_t[buffer.size()];
1589 CHECK(result != nullptr);
1590 memcpy(result, buffer.data(), buffer.size());
1591 return ArrayRef<const uint8_t>(result, buffer.size());
1592}
1593
Tamas Berghammer86e42782016-01-05 14:29:02 +00001594ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001595 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1596 if (Is64BitInstructionSet(isa)) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001597 return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info);
David Srbecky5cc349f2015-12-18 15:04:48 +00001598 } else {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001599 return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info);
1600 }
1601}
1602
1603template <typename ElfTypes>
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001604static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
1605 const InstructionSet isa, const ArrayRef<mirror::Class*>& types)
Tamas Berghammer86e42782016-01-05 14:29:02 +00001606 SHARED_REQUIRES(Locks::mutator_lock_) {
1607 std::vector<uint8_t> buffer;
1608 buffer.reserve(KB);
1609 VectorOutputStream out("Debug ELF file", &buffer);
1610 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1611 builder->Start();
1612
1613 DebugInfoWriter<ElfTypes> info_writer(builder.get());
1614 info_writer.Start();
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001615 info_writer.WriteTypes(types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001616 info_writer.End();
1617
1618 builder->End();
1619 CHECK(builder->Good());
1620 // Make a copy of the buffer. We want to shrink it anyway.
1621 uint8_t* result = new uint8_t[buffer.size()];
1622 CHECK(result != nullptr);
1623 memcpy(result, buffer.data(), buffer.size());
1624 return ArrayRef<const uint8_t>(result, buffer.size());
1625}
1626
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001627ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa,
1628 const ArrayRef<mirror::Class*>& types) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001629 if (Is64BitInstructionSet(isa)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001630 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001631 } else {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001632 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types);
David Srbecky5cc349f2015-12-18 15:04:48 +00001633 }
1634}
1635
David Srbecky6d8c8f02015-10-26 10:57:09 +00001636// Explicit instantiations
David Srbeckye0febdf2015-12-17 20:53:07 +00001637template void WriteDebugInfo<ElfTypes32>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001638 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001639 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001640 CFIFormat cfi_format);
1641template void WriteDebugInfo<ElfTypes64>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001642 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001643 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001644 CFIFormat cfi_format);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001645template void WriteMiniDebugInfo<ElfTypes32>(
1646 ElfBuilder<ElfTypes32>* builder,
1647 const ArrayRef<const MethodDebugInfo>& method_infos);
1648template void WriteMiniDebugInfo<ElfTypes64>(
1649 ElfBuilder<ElfTypes64>* builder,
1650 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +00001651
David Srbecky3b9d57a2015-04-10 00:22:14 +01001652} // namespace dwarf
1653} // namespace art