blob: 5dbbbc7e96a7a350985a3145658d626e69f6722f [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);
93 default:
94 LOG(FATAL) << "Unknown instruction set: " << isa;
95 UNREACHABLE();
96 }
97}
98
David Srbecky6d8c8f02015-10-26 10:57:09 +000099static void WriteCIE(InstructionSet isa,
100 CFIFormat format,
101 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100102 // Scratch registers should be marked as undefined. This tells the
103 // debugger that its value in the previous frame is not recoverable.
104 bool is64bit = Is64BitInstructionSet(isa);
105 switch (isa) {
106 case kArm:
107 case kThumb2: {
108 DebugFrameOpCodeWriter<> opcodes;
109 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
110 // core registers.
111 for (int reg = 0; reg < 13; reg++) {
112 if (reg < 4 || reg == 12) {
113 opcodes.Undefined(Reg::ArmCore(reg));
114 } else {
115 opcodes.SameValue(Reg::ArmCore(reg));
116 }
117 }
118 // fp registers.
119 for (int reg = 0; reg < 32; reg++) {
120 if (reg < 16) {
121 opcodes.Undefined(Reg::ArmFp(reg));
122 } else {
123 opcodes.SameValue(Reg::ArmFp(reg));
124 }
125 }
David Srbecky527c9c72015-04-17 21:14:10 +0100126 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100128 return;
129 }
130 case kArm64: {
131 DebugFrameOpCodeWriter<> opcodes;
132 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
133 // core registers.
134 for (int reg = 0; reg < 30; reg++) {
135 if (reg < 8 || reg == 16 || reg == 17) {
136 opcodes.Undefined(Reg::Arm64Core(reg));
137 } else {
138 opcodes.SameValue(Reg::Arm64Core(reg));
139 }
140 }
141 // fp registers.
142 for (int reg = 0; reg < 32; reg++) {
143 if (reg < 8 || reg >= 16) {
144 opcodes.Undefined(Reg::Arm64Fp(reg));
145 } else {
146 opcodes.SameValue(Reg::Arm64Fp(reg));
147 }
148 }
David Srbecky527c9c72015-04-17 21:14:10 +0100149 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000150 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100151 return;
152 }
153 case kMips:
154 case kMips64: {
155 DebugFrameOpCodeWriter<> opcodes;
156 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
157 // core registers.
158 for (int reg = 1; reg < 26; reg++) {
159 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
160 opcodes.Undefined(Reg::MipsCore(reg));
161 } else {
162 opcodes.SameValue(Reg::MipsCore(reg));
163 }
164 }
David Srbecky527c9c72015-04-17 21:14:10 +0100165 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100167 return;
168 }
169 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100170 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
171 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100172 DebugFrameOpCodeWriter<> opcodes;
173 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
174 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
175 // core registers.
176 for (int reg = 0; reg < 8; reg++) {
177 if (reg <= 3) {
178 opcodes.Undefined(Reg::X86Core(reg));
179 } else if (reg == 4) {
180 // Stack pointer.
181 } else {
182 opcodes.SameValue(Reg::X86Core(reg));
183 }
184 }
185 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100186 if (generate_opcodes_for_x86_fp) {
187 for (int reg = 0; reg < 8; reg++) {
188 opcodes.Undefined(Reg::X86Fp(reg));
189 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100190 }
David Srbecky527c9c72015-04-17 21:14:10 +0100191 auto return_reg = Reg::X86Core(8); // R8(EIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000192 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100193 return;
194 }
195 case kX86_64: {
196 DebugFrameOpCodeWriter<> opcodes;
197 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
198 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
199 // core registers.
200 for (int reg = 0; reg < 16; reg++) {
201 if (reg == 4) {
202 // Stack pointer.
203 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
204 opcodes.Undefined(Reg::X86_64Core(reg));
205 } else {
206 opcodes.SameValue(Reg::X86_64Core(reg));
207 }
208 }
209 // fp registers.
210 for (int reg = 0; reg < 16; reg++) {
211 if (reg < 12) {
212 opcodes.Undefined(Reg::X86_64Fp(reg));
213 } else {
214 opcodes.SameValue(Reg::X86_64Fp(reg));
215 }
216 }
David Srbecky527c9c72015-04-17 21:14:10 +0100217 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000218 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100219 return;
220 }
221 case kNone:
222 break;
223 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000224 LOG(FATAL) << "Cannot write CIE frame for ISA " << isa;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100225 UNREACHABLE();
226}
227
David Srbecky6d8c8f02015-10-26 10:57:09 +0000228template<typename ElfTypes>
229void WriteCFISection(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000230 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky5e974a62016-01-22 14:25:03 +0000231 CFIFormat format,
232 bool write_oat_patches) {
David Srbeckye0febdf2015-12-17 20:53:07 +0000233 CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000234 typedef typename ElfTypes::Addr Elf_Addr;
235
Tamas Berghammer86e42782016-01-05 14:29:02 +0000236 if (method_infos.empty()) {
237 return;
238 }
239
David Srbecky6d8c8f02015-10-26 10:57:09 +0000240 std::vector<uint32_t> binary_search_table;
241 std::vector<uintptr_t> patch_locations;
242 if (format == DW_EH_FRAME_FORMAT) {
243 binary_search_table.reserve(2 * method_infos.size());
244 } else {
245 patch_locations.reserve(method_infos.size());
246 }
David Srbecky527c9c72015-04-17 21:14:10 +0100247
David Srbecky5e974a62016-01-22 14:25:03 +0000248 // The methods can be written any order.
249 // Let's therefore sort them in the lexicographical order of the opcodes.
250 // This has no effect on its own. However, if the final .debug_frame section is
251 // compressed it reduces the size since similar opcodes sequences are grouped.
252 std::vector<const MethodDebugInfo*> sorted_method_infos;
253 sorted_method_infos.reserve(method_infos.size());
254 for (size_t i = 0; i < method_infos.size(); i++) {
255 sorted_method_infos.push_back(&method_infos[i]);
256 }
257 std::sort(
258 sorted_method_infos.begin(),
259 sorted_method_infos.end(),
260 [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) {
261 ArrayRef<const uint8_t> l = lhs->compiled_method_->GetCFIInfo();
262 ArrayRef<const uint8_t> r = rhs->compiled_method_->GetCFIInfo();
263 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
264 });
265
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100266 // Write .eh_frame/.debug_frame section.
David Srbeckye0febdf2015-12-17 20:53:07 +0000267 auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000268 ? builder->GetDebugFrame()
269 : builder->GetEhFrame());
270 {
271 cfi_section->Start();
272 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +0000273 const Elf_Addr text_address = builder->GetText()->Exists()
274 ? builder->GetText()->GetAddress()
275 : 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000276 const Elf_Addr cfi_address = cfi_section->GetAddress();
277 const Elf_Addr cie_address = cfi_address;
278 Elf_Addr buffer_address = cfi_address;
279 std::vector<uint8_t> buffer; // Small temporary buffer.
280 WriteCIE(builder->GetIsa(), format, &buffer);
281 cfi_section->WriteFully(buffer.data(), buffer.size());
282 buffer_address += buffer.size();
283 buffer.clear();
David Srbecky5e974a62016-01-22 14:25:03 +0000284 for (const MethodDebugInfo* mi : sorted_method_infos) {
285 if (!mi->deduped_) { // Only one FDE per unique address.
286 ArrayRef<const uint8_t> opcodes = mi->compiled_method_->GetCFIInfo();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000287 if (!opcodes.empty()) {
David Srbecky5e974a62016-01-22 14:25:03 +0000288 const Elf_Addr code_address = text_address + mi->low_pc_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000289 if (format == DW_EH_FRAME_FORMAT) {
290 binary_search_table.push_back(
291 dchecked_integral_cast<uint32_t>(code_address));
292 binary_search_table.push_back(
293 dchecked_integral_cast<uint32_t>(buffer_address));
294 }
295 WriteFDE(is64bit, cfi_address, cie_address,
David Srbecky5e974a62016-01-22 14:25:03 +0000296 code_address, mi->high_pc_ - mi->low_pc_,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000297 opcodes, format, buffer_address, &buffer,
298 &patch_locations);
299 cfi_section->WriteFully(buffer.data(), buffer.size());
300 buffer_address += buffer.size();
301 buffer.clear();
302 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100303 }
David Srbecky8dc73242015-04-12 11:40:39 +0100304 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000305 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100306 }
David Srbecky527c9c72015-04-17 21:14:10 +0100307
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100308 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000309 auto* header_section = builder->GetEhFrameHdr();
310 header_section->Start();
311 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100312 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000313 std::vector<uint8_t> buffer;
314 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100315 header.PushUint8(1); // Version.
316 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
317 // so we have to use pcrel which means relative to the pointer's location.
318 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
319 // Encoding of binary search table size.
320 header.PushUint8(DW_EH_PE_udata4);
321 // Encoding of binary search table addresses - libunwind supports only this
322 // specific combination, which means relative to the start of .eh_frame_hdr.
323 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000324 // .eh_frame pointer
325 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100326 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000327 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
328 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100329 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000330 for (size_t i = 0; i < binary_search_table.size(); i++) {
331 // Make addresses section-relative since we know the header address now.
332 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100333 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000334 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
335 header_section->End();
336 } else {
David Srbecky5e974a62016-01-22 14:25:03 +0000337 if (write_oat_patches) {
338 builder->WritePatches(".debug_frame.oat_patches",
339 ArrayRef<const uintptr_t>(patch_locations));
340 }
David Srbecky033d7452015-04-30 19:57:35 +0100341 }
David Srbecky8dc73242015-04-12 11:40:39 +0100342}
343
David Srbecky996ed0b2015-11-27 10:27:11 +0000344namespace {
345 struct CompilationUnit {
346 std::vector<const MethodDebugInfo*> methods_;
347 size_t debug_line_offset_ = 0;
David Srbecky5cc349f2015-12-18 15:04:48 +0000348 uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max();
349 uintptr_t high_pc_ = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000350 };
351
David Srbeckyb06e28e2015-12-10 13:15:00 +0000352 typedef std::vector<DexFile::LocalInfo> LocalInfos;
David Srbecky996ed0b2015-11-27 10:27:11 +0000353
David Srbeckyb06e28e2015-12-10 13:15:00 +0000354 void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
355 static_cast<LocalInfos*>(ctx)->push_back(entry);
356 }
357
358 typedef std::vector<DexFile::PositionInfo> PositionInfos;
359
360 bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) {
361 static_cast<PositionInfos*>(ctx)->push_back(entry);
362 return false;
363 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000364
365 std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
366 std::vector<const char*> names;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000367 if (mi->code_item_ != nullptr) {
368 const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_);
369 if (stream != nullptr) {
370 DecodeUnsignedLeb128(&stream); // line.
371 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
372 for (uint32_t i = 0; i < parameters_size; ++i) {
373 uint32_t id = DecodeUnsignedLeb128P1(&stream);
374 names.push_back(mi->dex_file_->StringDataByIdx(id));
375 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000376 }
377 }
378 return names;
379 }
380
381 struct VariableLocation {
382 uint32_t low_pc;
383 uint32_t high_pc;
384 DexRegisterLocation reg_lo; // May be None if the location is unknown.
385 DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
386 };
387
388 // Get the location of given dex register (e.g. stack or machine register).
389 // Note that the location might be different based on the current pc.
390 // The result will cover all ranges where the variable is in scope.
391 std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info,
392 uint16_t vreg,
393 bool is64bitValue,
394 uint32_t dex_pc_low,
395 uint32_t dex_pc_high) {
396 std::vector<VariableLocation> variable_locations;
397
398 // Get stack maps sorted by pc (they might not be sorted internally).
399 const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
400 const StackMapEncoding encoding = code_info.ExtractEncoding();
401 std::map<uint32_t, StackMap> stack_maps;
402 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
403 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
404 DCHECK(stack_map.IsValid());
405 const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
406 DCHECK_LE(low_pc, method_info->high_pc_);
407 stack_maps.emplace(low_pc, stack_map);
408 }
409
410 // Create entries for the requested register based on stack map data.
411 for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
412 const StackMap& stack_map = it->second;
413 const uint32_t low_pc = it->first;
414 auto next_it = it;
415 next_it++;
416 const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
417 : method_info->high_pc_;
418 DCHECK_LE(low_pc, high_pc);
419 if (low_pc == high_pc) {
420 continue; // Ignore if the address range is empty.
421 }
422
423 // Check that the stack map is in the requested range.
424 uint32_t dex_pc = stack_map.GetDexPc(encoding);
425 if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
426 continue;
427 }
428
429 // Find the location of the dex register.
430 DexRegisterLocation reg_lo = DexRegisterLocation::None();
431 DexRegisterLocation reg_hi = DexRegisterLocation::None();
432 if (stack_map.HasDexRegisterMap(encoding)) {
433 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
434 stack_map, encoding, method_info->code_item_->registers_size_);
435 reg_lo = dex_register_map.GetDexRegisterLocation(
436 vreg, method_info->code_item_->registers_size_, code_info, encoding);
437 if (is64bitValue) {
438 reg_hi = dex_register_map.GetDexRegisterLocation(
439 vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
440 }
441 }
442
443 // Add location entry for this address range.
444 if (!variable_locations.empty() &&
445 variable_locations.back().reg_lo == reg_lo &&
446 variable_locations.back().reg_hi == reg_hi &&
447 variable_locations.back().high_pc == low_pc) {
448 // Merge with the previous entry (extend its range).
449 variable_locations.back().high_pc = high_pc;
450 } else {
451 variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
452 }
453 }
454
455 return variable_locations;
456 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000457
458 bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) {
459 return method_info->compiled_method_->GetQuickCode().size() > 0 &&
460 method_info->compiled_method_->GetVmapTable().size() > 0 &&
461 method_info->compiled_method_->GetGcMap().size() == 0 &&
462 method_info->code_item_ != nullptr;
463 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000464} // namespace
David Srbecky04b05262015-11-09 18:05:48 +0000465
466// Helper class to write .debug_info and its supporting sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000467template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000468class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000469 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100470
David Srbecky04b05262015-11-09 18:05:48 +0000471 // Helper class to write one compilation unit.
472 // It holds helper methods and temporary state.
473 class CompilationUnitWriter {
474 public:
475 explicit CompilationUnitWriter(DebugInfoWriter* owner)
476 : owner_(owner),
477 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) {
478 }
479
480 void Write(const CompilationUnit& compilation_unit) {
481 CHECK(!compilation_unit.methods_.empty());
David Srbecky5cc349f2015-12-18 15:04:48 +0000482 const Elf_Addr text_address = owner_->builder_->GetText()->Exists()
483 ? owner_->builder_->GetText()->GetAddress()
484 : 0;
485 const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_;
David Srbecky04b05262015-11-09 18:05:48 +0000486
487 info_.StartTag(DW_TAG_compile_unit);
488 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
489 info_.WriteData1(DW_AT_language, DW_LANG_Java);
Tamas Berghammer8c557122015-12-10 15:06:25 +0000490 info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT"));
David Srbecky04b05262015-11-09 18:05:48 +0000491 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000492 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size));
David Srbecky0fd295f2015-11-16 16:39:10 +0000493 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
David Srbecky04b05262015-11-09 18:05:48 +0000494
495 const char* last_dex_class_desc = nullptr;
496 for (auto mi : compilation_unit.methods_) {
497 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000498 const DexFile::CodeItem* dex_code = mi->code_item_;
David Srbecky04b05262015-11-09 18:05:48 +0000499 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
500 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
501 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
502 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
David Srbecky996ed0b2015-11-27 10:27:11 +0000503 const bool is_static = (mi->access_flags_ & kAccStatic) != 0;
David Srbecky04b05262015-11-09 18:05:48 +0000504
505 // Enclose the method in correct class definition.
506 if (last_dex_class_desc != dex_class_desc) {
507 if (last_dex_class_desc != nullptr) {
David Srbecky7138d452016-01-29 15:02:44 +0000508 EndClassTag();
David Srbecky04b05262015-11-09 18:05:48 +0000509 }
Tamas Berghammer86e42782016-01-05 14:29:02 +0000510 // Write reference tag for the class we are about to declare.
511 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
512 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
513 size_t type_attrib_offset = info_.size();
514 info_.WriteRef4(DW_AT_type, 0);
515 info_.EndTag();
516 // Declare the class that owns this method.
517 size_t class_offset = StartClassTag(dex_class_desc);
518 info_.UpdateUint32(type_attrib_offset, class_offset);
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000519 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky04b05262015-11-09 18:05:48 +0000520 // Check that each class is defined only once.
521 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
522 CHECK(unique) << "Redefinition of " << dex_class_desc;
523 last_dex_class_desc = dex_class_desc;
524 }
525
David Srbecky04b05262015-11-09 18:05:48 +0000526 int start_depth = info_.Depth();
527 info_.StartTag(DW_TAG_subprogram);
528 WriteName(dex->GetMethodName(dex_method));
529 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
David Srbecky5cc349f2015-12-18 15:04:48 +0000530 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_));
David Srbecky91cb54e2016-01-15 13:47:59 +0000531 std::vector<uint8_t> expr_buffer;
532 Expression expr(&expr_buffer);
533 expr.WriteOpCallFrameCfa();
534 info_.WriteExprLoc(DW_AT_frame_base, expr);
David Srbecky04b05262015-11-09 18:05:48 +0000535 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
David Srbeckyb06e28e2015-12-10 13:15:00 +0000536
537 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
538 // guarantee order or uniqueness so it is safer to iterate over them manually.
539 // DecodeDebugLocalInfo might not also be available if there is no debug info.
540 std::vector<const char*> param_names = GetParamNames(mi);
541 uint32_t arg_reg = 0;
David Srbecky996ed0b2015-11-27 10:27:11 +0000542 if (!is_static) {
543 info_.StartTag(DW_TAG_formal_parameter);
544 WriteName("this");
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000545 info_.WriteFlagPresent(DW_AT_artificial);
David Srbecky996ed0b2015-11-27 10:27:11 +0000546 WriteLazyType(dex_class_desc);
David Srbeckyb06e28e2015-12-10 13:15:00 +0000547 if (dex_code != nullptr) {
548 // Write the stack location of the parameter.
549 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
550 const bool is64bitValue = false;
551 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
552 }
553 arg_reg++;
David Srbecky996ed0b2015-11-27 10:27:11 +0000554 info_.EndTag();
555 }
David Srbecky04b05262015-11-09 18:05:48 +0000556 if (dex_params != nullptr) {
557 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
558 info_.StartTag(DW_TAG_formal_parameter);
559 // Parameter names may not be always available.
David Srbeckyb06e28e2015-12-10 13:15:00 +0000560 if (i < param_names.size()) {
David Srbecky04b05262015-11-09 18:05:48 +0000561 WriteName(param_names[i]);
562 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000563 // Write the type.
564 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
565 WriteLazyType(type_desc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000566 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
David Srbeckyb06e28e2015-12-10 13:15:00 +0000567 if (dex_code != nullptr) {
568 // Write the stack location of the parameter.
569 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
570 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
571 }
572 arg_reg += is64bitValue ? 2 : 1;
David Srbecky04b05262015-11-09 18:05:48 +0000573 info_.EndTag();
574 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000575 if (dex_code != nullptr) {
576 DCHECK_EQ(arg_reg, dex_code->ins_size_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000577 }
David Srbecky04b05262015-11-09 18:05:48 +0000578 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000579
580 // Write local variables.
581 LocalInfos local_infos;
582 if (dex->DecodeDebugLocalInfo(dex_code,
583 is_static,
584 mi->dex_method_index_,
585 LocalInfoCallback,
586 &local_infos)) {
587 for (const DexFile::LocalInfo& var : local_infos) {
588 if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) {
589 info_.StartTag(DW_TAG_variable);
590 WriteName(var.name_);
591 WriteLazyType(var.descriptor_);
592 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
593 WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_,
594 var.start_address_, var.end_address_);
595 info_.EndTag();
596 }
David Srbecky996ed0b2015-11-27 10:27:11 +0000597 }
598 }
David Srbeckyb06e28e2015-12-10 13:15:00 +0000599
David Srbecky04b05262015-11-09 18:05:48 +0000600 info_.EndTag();
601 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
602 }
603 if (last_dex_class_desc != nullptr) {
David Srbecky7138d452016-01-29 15:02:44 +0000604 EndClassTag();
David Srbecky04b05262015-11-09 18:05:48 +0000605 }
David Srbecky04b05262015-11-09 18:05:48 +0000606 FinishLazyTypes();
David Srbecky7138d452016-01-29 15:02:44 +0000607 CloseNamespacesAboveDepth(0);
David Srbecky04b05262015-11-09 18:05:48 +0000608 info_.EndTag(); // DW_TAG_compile_unit
David Srbecky7138d452016-01-29 15:02:44 +0000609 CHECK_EQ(info_.Depth(), 0);
David Srbecky04b05262015-11-09 18:05:48 +0000610 std::vector<uint8_t> buffer;
611 buffer.reserve(info_.data()->size() + KB);
612 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
613 const size_t debug_abbrev_offset =
614 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
615 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
616 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
617 }
618
Tamas Berghammer86e42782016-01-05 14:29:02 +0000619 void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
620 info_.StartTag(DW_TAG_compile_unit);
621 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
622 info_.WriteData1(DW_AT_language, DW_LANG_Java);
623
David Srbeckyc7eecf92016-02-01 14:53:48 +0000624 // Base class references to be patched at the end.
625 std::map<size_t, mirror::Class*> base_class_references;
626
627 // Already written declarations or definitions.
628 std::map<mirror::Class*, size_t> class_declarations;
629
David Srbecky9b3607d2016-01-14 18:15:54 +0000630 std::vector<uint8_t> expr_buffer;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000631 for (mirror::Class* type : types) {
632 if (type->IsPrimitive()) {
633 // For primitive types the definition and the declaration is the same.
634 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
635 WriteTypeDeclaration(type->GetDescriptor(nullptr));
636 }
637 } else if (type->IsArrayClass()) {
638 mirror::Class* element_type = type->GetComponentType();
639 uint32_t component_size = type->GetComponentSize();
640 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
641 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
642
David Srbecky7138d452016-01-29 15:02:44 +0000643 CloseNamespacesAboveDepth(0); // Declare in root namespace.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000644 info_.StartTag(DW_TAG_array_type);
645 std::string descriptor_string;
646 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
David Srbecky9b3607d2016-01-14 18:15:54 +0000647 WriteLinkageName(type);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000648 info_.WriteUdata(DW_AT_data_member_location, data_offset);
649 info_.StartTag(DW_TAG_subrange_type);
David Srbecky9b3607d2016-01-14 18:15:54 +0000650 Expression count_expr(&expr_buffer);
David Srbecky91cb54e2016-01-15 13:47:59 +0000651 count_expr.WriteOpPushObjectAddress();
652 count_expr.WriteOpPlusUconst(length_offset);
653 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
654 info_.WriteExprLoc(DW_AT_count, count_expr);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000655 info_.EndTag(); // DW_TAG_subrange_type.
656 info_.EndTag(); // DW_TAG_array_type.
David Srbecky9b3607d2016-01-14 18:15:54 +0000657 } else if (type->IsInterface()) {
658 // Skip. Variables cannot have an interface as a dynamic type.
659 // We do not expose the interface information to the debugger in any way.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000660 } else {
661 std::string descriptor_string;
662 const char* desc = type->GetDescriptor(&descriptor_string);
David Srbeckyc7eecf92016-02-01 14:53:48 +0000663 size_t class_offset = StartClassTag(desc);
664 class_declarations.emplace(type, class_offset);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000665
666 if (!type->IsVariableSize()) {
667 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
668 }
669
David Srbecky9b3607d2016-01-14 18:15:54 +0000670 WriteLinkageName(type);
671
672 if (type->IsObjectClass()) {
673 // Generate artificial member which is used to get the dynamic type of variable.
674 // The run-time value of this field will correspond to linkage name of some type.
675 // We need to do it only once in j.l.Object since all other types inherit it.
676 info_.StartTag(DW_TAG_member);
677 WriteName(".dynamic_type");
678 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000679 info_.WriteFlagPresent(DW_AT_artificial);
David Srbecky9b3607d2016-01-14 18:15:54 +0000680 // Create DWARF expression to get the value of the methods_ field.
681 Expression expr(&expr_buffer);
682 // The address of the object has been implicitly pushed on the stack.
683 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
684 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
685 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
686 expr.WriteOpDerefSize(4);
687 if (kPoisonHeapReferences) {
688 expr.WriteOpNeg();
689 // DWARF stack is pointer sized. Ensure that the high bits are clear.
690 expr.WriteOpConstu(0xFFFFFFFF);
691 expr.WriteOpAnd();
692 }
693 // Add offset to the methods_ field.
694 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
695 // Top of stack holds the location of the field now.
696 info_.WriteExprLoc(DW_AT_data_member_location, expr);
697 info_.EndTag(); // DW_TAG_member.
698 }
699
Tamas Berghammer86e42782016-01-05 14:29:02 +0000700 // Base class.
David Srbeckyc7eecf92016-02-01 14:53:48 +0000701 mirror::Class* base_class = type->GetSuperClass();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000702 if (base_class != nullptr) {
703 info_.StartTag(DW_TAG_inheritance);
David Srbeckyc7eecf92016-02-01 14:53:48 +0000704 base_class_references.emplace(info_.size(), base_class);
705 info_.WriteRef4(DW_AT_type, 0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000706 info_.WriteUdata(DW_AT_data_member_location, 0);
707 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
708 info_.EndTag(); // DW_TAG_inheritance.
709 }
710
711 // Member variables.
712 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
713 ArtField* field = type->GetInstanceField(i);
714 info_.StartTag(DW_TAG_member);
715 WriteName(field->GetName());
716 WriteLazyType(field->GetTypeDescriptor());
717 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
718 uint32_t access_flags = field->GetAccessFlags();
719 if (access_flags & kAccPublic) {
720 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
721 } else if (access_flags & kAccProtected) {
722 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
723 } else if (access_flags & kAccPrivate) {
724 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
725 }
726 info_.EndTag(); // DW_TAG_member.
727 }
728
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000729 if (type->IsStringClass()) {
730 // Emit debug info about an artifical class member for java.lang.String which represents
731 // the first element of the data stored in a string instance. Consumers of the debug
732 // info will be able to read the content of java.lang.String based on the count (real
733 // field) and based on the location of this data member.
734 info_.StartTag(DW_TAG_member);
735 WriteName("value");
736 // We don't support fields with C like array types so we just say its type is java char.
737 WriteLazyType("C"); // char.
738 info_.WriteUdata(DW_AT_data_member_location,
739 mirror::String::ValueOffset().Uint32Value());
740 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
741 info_.EndTag(); // DW_TAG_member.
742 }
743
David Srbecky7138d452016-01-29 15:02:44 +0000744 EndClassTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000745 }
746 }
747
David Srbeckyc7eecf92016-02-01 14:53:48 +0000748 // Write base class declarations.
749 for (const auto& base_class_reference : base_class_references) {
750 size_t reference_offset = base_class_reference.first;
751 mirror::Class* base_class = base_class_reference.second;
752 const auto& it = class_declarations.find(base_class);
753 if (it != class_declarations.end()) {
754 info_.UpdateUint32(reference_offset, it->second);
755 } else {
756 // Declare base class. We can not use the standard WriteLazyType
757 // since we want to avoid the DW_TAG_reference_tag wrapping.
758 std::string tmp_storage;
759 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
760 size_t base_class_declaration_offset = StartClassTag(base_class_desc);
761 info_.WriteFlagPresent(DW_AT_declaration);
762 WriteLinkageName(base_class);
763 EndClassTag();
764 class_declarations.emplace(base_class, base_class_declaration_offset);
765 info_.UpdateUint32(reference_offset, base_class_declaration_offset);
766 }
767 }
768
Tamas Berghammer86e42782016-01-05 14:29:02 +0000769 FinishLazyTypes();
David Srbecky7138d452016-01-29 15:02:44 +0000770 CloseNamespacesAboveDepth(0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000771 info_.EndTag(); // DW_TAG_compile_unit.
David Srbecky7138d452016-01-29 15:02:44 +0000772 CHECK_EQ(info_.Depth(), 0);
Tamas Berghammer86e42782016-01-05 14:29:02 +0000773 std::vector<uint8_t> buffer;
774 buffer.reserve(info_.data()->size() + KB);
775 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
776 const size_t debug_abbrev_offset =
777 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
778 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
779 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
780 }
781
David Srbecky9b3607d2016-01-14 18:15:54 +0000782 // Linkage name uniquely identifies type.
783 // It is used to determine the dynamic type of objects.
784 // We use the methods_ field of class since it is unique and it is not moved by the GC.
785 void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
786 auto* methods_ptr = type->GetMethodsPtr();
787 if (methods_ptr == nullptr) {
788 // Some types might have no methods. Allocate empty array instead.
789 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
790 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
791 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
792 type->SetMethodsPtr(methods_ptr, 0, 0);
793 DCHECK(type->GetMethodsPtr() != nullptr);
794 }
795 char name[32];
796 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
797 info_.WriteString(DW_AT_linkage_name, name);
798 }
799
David Srbecky0fd295f2015-11-16 16:39:10 +0000800 // Write table into .debug_loc which describes location of dex register.
801 // The dex register might be valid only at some points and it might
802 // move between machine registers and stack.
David Srbecky996ed0b2015-11-27 10:27:11 +0000803 void WriteRegLocation(const MethodDebugInfo* method_info,
804 uint16_t vreg,
805 bool is64bitValue,
806 uint32_t compilation_unit_low_pc,
807 uint32_t dex_pc_low = 0,
808 uint32_t dex_pc_high = 0xFFFFFFFF) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000809 using Kind = DexRegisterLocation::Kind;
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000810 if (!IsFromOptimizingCompiler(method_info)) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000811 return;
812 }
813
David Srbecky996ed0b2015-11-27 10:27:11 +0000814 Writer<> debug_loc(&owner_->debug_loc_);
815 Writer<> debug_ranges(&owner_->debug_ranges_);
816 info_.WriteSecOffset(DW_AT_location, debug_loc.size());
817 info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size());
David Srbecky0fd295f2015-11-16 16:39:10 +0000818
David Srbecky996ed0b2015-11-27 10:27:11 +0000819 std::vector<VariableLocation> variable_locations = GetVariableLocations(
820 method_info,
821 vreg,
822 is64bitValue,
823 dex_pc_low,
824 dex_pc_high);
825
826 // Write .debug_loc entries.
David Srbecky0fd295f2015-11-16 16:39:10 +0000827 const InstructionSet isa = owner_->builder_->GetIsa();
828 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky91cb54e2016-01-15 13:47:59 +0000829 std::vector<uint8_t> expr_buffer;
David Srbecky996ed0b2015-11-27 10:27:11 +0000830 for (const VariableLocation& variable_location : variable_locations) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000831 // Translate dex register location to DWARF expression.
832 // Note that 64-bit value might be split to two distinct locations.
833 // (for example, two 32-bit machine registers, or even stack and register)
David Srbecky91cb54e2016-01-15 13:47:59 +0000834 Expression expr(&expr_buffer);
David Srbecky996ed0b2015-11-27 10:27:11 +0000835 DexRegisterLocation reg_lo = variable_location.reg_lo;
836 DexRegisterLocation reg_hi = variable_location.reg_hi;
David Srbecky0fd295f2015-11-16 16:39:10 +0000837 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
838 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
839 const Kind kind = reg_loc.GetKind();
840 const int32_t value = reg_loc.GetValue();
841 if (kind == Kind::kInStack) {
842 const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
David Srbecky0fd295f2015-11-16 16:39:10 +0000843 // The stack offset is relative to SP. Make it relative to CFA.
David Srbecky91cb54e2016-01-15 13:47:59 +0000844 expr.WriteOpFbreg(value - frame_size);
David Srbecky0fd295f2015-11-16 16:39:10 +0000845 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
846 reg_hi.GetValue() == value + 4) {
847 break; // the high word is correctly implied by the low word.
848 }
849 } else if (kind == Kind::kInRegister) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000850 expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000851 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
852 reg_hi.GetValue() == value) {
853 break; // the high word is correctly implied by the low word.
854 }
855 } else if (kind == Kind::kInFpuRegister) {
856 if ((isa == kArm || isa == kThumb2) &&
857 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
858 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
859 // Translate S register pair to D register (e.g. S4+S5 to D2).
David Srbecky91cb54e2016-01-15 13:47:59 +0000860 expr.WriteOpReg(Reg::ArmDp(value / 2).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000861 break;
862 }
David Srbecky3dd7e5a2015-11-27 13:31:16 +0000863 if (isa == kMips || isa == kMips64) {
864 // TODO: Find what the DWARF floating point register numbers are on MIPS.
865 break;
866 }
David Srbecky91cb54e2016-01-15 13:47:59 +0000867 expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
David Srbecky0fd295f2015-11-16 16:39:10 +0000868 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
869 reg_hi.GetValue() == reg_lo.GetValue()) {
870 break; // the high word is correctly implied by the low word.
871 }
872 } else if (kind == Kind::kConstant) {
David Srbecky91cb54e2016-01-15 13:47:59 +0000873 expr.WriteOpConsts(value);
874 expr.WriteOpStackValue();
David Srbecky0fd295f2015-11-16 16:39:10 +0000875 } else if (kind == Kind::kNone) {
876 break;
877 } else {
878 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
879 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
880 // the special cases above and they should not occur alone.
881 LOG(ERROR) << "Unexpected register location kind: "
882 << DexRegisterLocation::PrettyDescriptor(kind);
883 break;
884 }
885 if (is64bitValue) {
886 // Write the marker which is needed by split 64-bit values.
887 // This code is skipped by the special cases.
David Srbecky91cb54e2016-01-15 13:47:59 +0000888 expr.WriteOpPiece(4);
David Srbecky0fd295f2015-11-16 16:39:10 +0000889 }
890 }
891
David Srbecky91cb54e2016-01-15 13:47:59 +0000892 if (expr.size() > 0) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000893 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000894 debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
895 debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000896 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000897 debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
898 debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
David Srbecky0fd295f2015-11-16 16:39:10 +0000899 }
900 // Write the expression.
David Srbecky91cb54e2016-01-15 13:47:59 +0000901 debug_loc.PushUint16(expr.size());
902 debug_loc.PushData(expr.data());
David Srbecky0fd295f2015-11-16 16:39:10 +0000903 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000904 // Do not generate .debug_loc if the location is not known.
David Srbecky0fd295f2015-11-16 16:39:10 +0000905 }
906 }
907 // Write end-of-list entry.
908 if (is64bit) {
David Srbecky996ed0b2015-11-27 10:27:11 +0000909 debug_loc.PushUint64(0);
910 debug_loc.PushUint64(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000911 } else {
David Srbecky996ed0b2015-11-27 10:27:11 +0000912 debug_loc.PushUint32(0);
913 debug_loc.PushUint32(0);
914 }
915
916 // Write .debug_ranges entries.
917 // This includes ranges where the variable is in scope but the location is not known.
918 for (size_t i = 0; i < variable_locations.size(); i++) {
919 uint32_t low_pc = variable_locations[i].low_pc;
920 uint32_t high_pc = variable_locations[i].high_pc;
921 while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
922 // Merge address range with the next entry.
923 high_pc = variable_locations[++i].high_pc;
924 }
925 if (is64bit) {
926 debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
927 debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
928 } else {
929 debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
930 debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
931 }
932 }
933 // Write end-of-list entry.
934 if (is64bit) {
935 debug_ranges.PushUint64(0);
936 debug_ranges.PushUint64(0);
937 } else {
938 debug_ranges.PushUint32(0);
939 debug_ranges.PushUint32(0);
David Srbecky0fd295f2015-11-16 16:39:10 +0000940 }
941 }
942
David Srbecky04b05262015-11-09 18:05:48 +0000943 // Some types are difficult to define as we go since they need
944 // to be enclosed in the right set of namespaces. Therefore we
945 // just define all types lazily at the end of compilation unit.
946 void WriteLazyType(const char* type_descriptor) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000947 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000948 lazy_types_.emplace(std::string(type_descriptor), info_.size());
David Srbecky04b05262015-11-09 18:05:48 +0000949 info_.WriteRef4(DW_AT_type, 0);
950 }
951 }
952
953 void FinishLazyTypes() {
954 for (const auto& lazy_type : lazy_types_) {
Tamas Berghammer86e42782016-01-05 14:29:02 +0000955 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
David Srbecky04b05262015-11-09 18:05:48 +0000956 }
957 lazy_types_.clear();
958 }
959
960 private:
961 void WriteName(const char* name) {
David Srbeckyb06e28e2015-12-10 13:15:00 +0000962 if (name != nullptr) {
963 info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
964 }
David Srbecky04b05262015-11-09 18:05:48 +0000965 }
966
967 // Convert dex type descriptor to DWARF.
968 // Returns offset in the compilation unit.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000969 size_t WriteTypeDeclaration(const std::string& desc) {
970 DCHECK(!desc.empty());
David Srbecky04b05262015-11-09 18:05:48 +0000971 const auto& it = type_cache_.find(desc);
972 if (it != type_cache_.end()) {
973 return it->second;
974 }
975
976 size_t offset;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000977 if (desc[0] == 'L') {
David Srbecky04b05262015-11-09 18:05:48 +0000978 // Class type. For example: Lpackage/name;
Tamas Berghammer86e42782016-01-05 14:29:02 +0000979 size_t class_offset = StartClassTag(desc.c_str());
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000980 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky7138d452016-01-29 15:02:44 +0000981 EndClassTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000982 // Reference to the class type.
983 offset = info_.StartTag(DW_TAG_reference_type);
984 info_.WriteRef(DW_AT_type, class_offset);
985 info_.EndTag();
986 } else if (desc[0] == '[') {
David Srbecky04b05262015-11-09 18:05:48 +0000987 // Array type.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000988 size_t element_type = WriteTypeDeclaration(desc.substr(1));
David Srbecky7138d452016-01-29 15:02:44 +0000989 CloseNamespacesAboveDepth(0); // Declare in root namespace.
Tamas Berghammer86e42782016-01-05 14:29:02 +0000990 size_t array_type = info_.StartTag(DW_TAG_array_type);
David Srbeckyfa5ec2b2016-01-29 15:13:19 +0000991 info_.WriteFlagPresent(DW_AT_declaration);
David Srbecky04b05262015-11-09 18:05:48 +0000992 info_.WriteRef(DW_AT_type, element_type);
993 info_.EndTag();
Tamas Berghammer86e42782016-01-05 14:29:02 +0000994 offset = info_.StartTag(DW_TAG_reference_type);
995 info_.WriteRef4(DW_AT_type, array_type);
996 info_.EndTag();
David Srbecky04b05262015-11-09 18:05:48 +0000997 } else {
998 // Primitive types.
Tamas Berghammer03c941f2016-01-15 13:39:57 +0000999 DCHECK_EQ(desc.size(), 1u);
1000
David Srbecky04b05262015-11-09 18:05:48 +00001001 const char* name;
David Srbecky0fd295f2015-11-16 16:39:10 +00001002 uint32_t encoding;
1003 uint32_t byte_size;
Tamas Berghammer86e42782016-01-05 14:29:02 +00001004 switch (desc[0]) {
David Srbecky0fd295f2015-11-16 16:39:10 +00001005 case 'B':
1006 name = "byte";
1007 encoding = DW_ATE_signed;
1008 byte_size = 1;
1009 break;
1010 case 'C':
1011 name = "char";
1012 encoding = DW_ATE_UTF;
1013 byte_size = 2;
1014 break;
1015 case 'D':
1016 name = "double";
1017 encoding = DW_ATE_float;
1018 byte_size = 8;
1019 break;
1020 case 'F':
1021 name = "float";
1022 encoding = DW_ATE_float;
1023 byte_size = 4;
1024 break;
1025 case 'I':
1026 name = "int";
1027 encoding = DW_ATE_signed;
1028 byte_size = 4;
1029 break;
1030 case 'J':
1031 name = "long";
1032 encoding = DW_ATE_signed;
1033 byte_size = 8;
1034 break;
1035 case 'S':
1036 name = "short";
1037 encoding = DW_ATE_signed;
1038 byte_size = 2;
1039 break;
1040 case 'Z':
1041 name = "boolean";
1042 encoding = DW_ATE_boolean;
1043 byte_size = 1;
1044 break;
1045 case 'V':
1046 LOG(FATAL) << "Void type should not be encoded";
1047 UNREACHABLE();
David Srbecky04b05262015-11-09 18:05:48 +00001048 default:
Tamas Berghammer86e42782016-01-05 14:29:02 +00001049 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
David Srbecky04b05262015-11-09 18:05:48 +00001050 UNREACHABLE();
1051 }
David Srbecky7138d452016-01-29 15:02:44 +00001052 CloseNamespacesAboveDepth(0); // Declare in root namespace.
David Srbecky04b05262015-11-09 18:05:48 +00001053 offset = info_.StartTag(DW_TAG_base_type);
1054 WriteName(name);
David Srbecky0fd295f2015-11-16 16:39:10 +00001055 info_.WriteData1(DW_AT_encoding, encoding);
1056 info_.WriteData1(DW_AT_byte_size, byte_size);
David Srbecky04b05262015-11-09 18:05:48 +00001057 info_.EndTag();
1058 }
1059
1060 type_cache_.emplace(desc, offset);
1061 return offset;
1062 }
1063
1064 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
1065 // Returns offset of the class tag in the compilation unit.
1066 size_t StartClassTag(const char* desc) {
David Srbecky7138d452016-01-29 15:02:44 +00001067 std::string name = SetNamespaceForClass(desc);
David Srbecky04b05262015-11-09 18:05:48 +00001068 size_t offset = info_.StartTag(DW_TAG_class_type);
David Srbecky7138d452016-01-29 15:02:44 +00001069 WriteName(name.c_str());
David Srbecky04b05262015-11-09 18:05:48 +00001070 return offset;
1071 }
1072
David Srbecky7138d452016-01-29 15:02:44 +00001073 void EndClassTag() {
David Srbecky04b05262015-11-09 18:05:48 +00001074 info_.EndTag();
David Srbecky7138d452016-01-29 15:02:44 +00001075 }
1076
1077 // Set the current namespace nesting to one required by the given class.
1078 // Returns the class name with namespaces, 'L', and ';' stripped.
1079 std::string SetNamespaceForClass(const char* desc) {
1080 DCHECK(desc != nullptr && desc[0] == 'L');
1081 desc++; // Skip the initial 'L'.
1082 size_t depth = 0;
1083 for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) {
1084 // Check whether the name at this depth is already what we need.
1085 if (depth < current_namespace_.size()) {
1086 const std::string& name = current_namespace_[depth];
1087 if (name.compare(0, name.size(), desc, end - desc) == 0) {
1088 continue;
1089 }
1090 }
1091 // Otherwise we need to open a new namespace tag at this depth.
1092 CloseNamespacesAboveDepth(depth);
1093 info_.StartTag(DW_TAG_namespace);
1094 std::string name(desc, end - desc);
1095 WriteName(name.c_str());
1096 current_namespace_.push_back(std::move(name));
1097 }
1098 CloseNamespacesAboveDepth(depth);
1099 return std::string(desc, strchr(desc, ';') - desc);
1100 }
1101
1102 // Close namespace tags to reach the given nesting depth.
1103 void CloseNamespacesAboveDepth(size_t depth) {
1104 DCHECK_LE(depth, current_namespace_.size());
1105 while (current_namespace_.size() > depth) {
David Srbecky04b05262015-11-09 18:05:48 +00001106 info_.EndTag();
David Srbecky7138d452016-01-29 15:02:44 +00001107 current_namespace_.pop_back();
David Srbecky04b05262015-11-09 18:05:48 +00001108 }
1109 }
1110
1111 // For access to the ELF sections.
1112 DebugInfoWriter<ElfTypes>* owner_;
1113 // Debug abbrevs for this compilation unit only.
1114 std::vector<uint8_t> debug_abbrev_;
1115 // Temporary buffer to create and store the entries.
1116 DebugInfoEntryWriter<> info_;
1117 // Cache of already translated type descriptors.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001118 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
David Srbecky04b05262015-11-09 18:05:48 +00001119 // 32-bit references which need to be resolved to a type later.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001120 // Given type may be used multiple times. Therefore we need a multimap.
1121 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
David Srbecky7138d452016-01-29 15:02:44 +00001122 // The current set of open namespace tags which are active and not closed yet.
1123 std::vector<std::string> current_namespace_;
David Srbecky04b05262015-11-09 18:05:48 +00001124 };
1125
David Srbeckyb851b492015-11-11 20:19:38 +00001126 public:
1127 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbecky626a1662015-04-12 13:12:26 +01001128 }
1129
David Srbeckyb851b492015-11-11 20:19:38 +00001130 void Start() {
1131 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +01001132 }
1133
David Srbecky04b05262015-11-09 18:05:48 +00001134 void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
1135 CompilationUnitWriter writer(this);
1136 writer.Write(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001137 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001138
Tamas Berghammer86e42782016-01-05 14:29:02 +00001139 void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
1140 CompilationUnitWriter writer(this);
1141 writer.Write(types);
1142 }
1143
David Srbeckyb851b492015-11-11 20:19:38 +00001144 void End() {
1145 builder_->GetDebugInfo()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001146 builder_->WritePatches(".debug_info.oat_patches",
1147 ArrayRef<const uintptr_t>(debug_info_patches_));
David Srbecky04b05262015-11-09 18:05:48 +00001148 builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data());
1149 builder_->WriteSection(".debug_str", &debug_str_.Data());
David Srbecky0fd295f2015-11-16 16:39:10 +00001150 builder_->WriteSection(".debug_loc", &debug_loc_);
David Srbecky996ed0b2015-11-27 10:27:11 +00001151 builder_->WriteSection(".debug_ranges", &debug_ranges_);
David Srbeckyb851b492015-11-11 20:19:38 +00001152 }
1153
1154 private:
David Srbecky04b05262015-11-09 18:05:48 +00001155 size_t WriteString(const char* str) {
1156 return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
1157 }
1158
David Srbeckyb851b492015-11-11 20:19:38 +00001159 ElfBuilder<ElfTypes>* builder_;
1160 std::vector<uintptr_t> debug_info_patches_;
David Srbecky04b05262015-11-09 18:05:48 +00001161 DedupVector debug_abbrev_;
1162 DedupVector debug_str_;
David Srbecky0fd295f2015-11-16 16:39:10 +00001163 std::vector<uint8_t> debug_loc_;
David Srbecky996ed0b2015-11-27 10:27:11 +00001164 std::vector<uint8_t> debug_ranges_;
David Srbecky04b05262015-11-09 18:05:48 +00001165
1166 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
David Srbeckyb851b492015-11-11 20:19:38 +00001167};
1168
1169template<typename ElfTypes>
1170class DebugLineWriter {
1171 typedef typename ElfTypes::Addr Elf_Addr;
1172
1173 public:
1174 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
1175 }
1176
1177 void Start() {
1178 builder_->GetDebugLine()->Start();
1179 }
1180
1181 // Write line table for given set of methods.
1182 // Returns the number of bytes written.
David Srbecky04b05262015-11-09 18:05:48 +00001183 size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
David Srbeckyb851b492015-11-11 20:19:38 +00001184 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
David Srbecky5cc349f2015-12-18 15:04:48 +00001185 const Elf_Addr text_address = builder_->GetText()->Exists()
1186 ? builder_->GetText()->GetAddress()
1187 : 0;
David Srbecky04b05262015-11-09 18:05:48 +00001188
1189 compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
David Srbeckyb851b492015-11-11 20:19:38 +00001190
David Srbecky799b8c42015-04-14 01:57:43 +01001191 std::vector<FileEntry> files;
1192 std::unordered_map<std::string, size_t> files_map;
1193 std::vector<std::string> directories;
1194 std::unordered_map<std::string, size_t> directories_map;
1195 int code_factor_bits_ = 0;
1196 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +00001197 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +01001198 case kArm: // arm actually means thumb2.
1199 case kThumb2:
1200 code_factor_bits_ = 1; // 16-bit instuctions
1201 dwarf_isa = 1; // DW_ISA_ARM_thumb.
1202 break;
1203 case kArm64:
1204 case kMips:
1205 case kMips64:
1206 code_factor_bits_ = 2; // 32-bit instructions
1207 break;
1208 case kNone:
1209 case kX86:
1210 case kX86_64:
1211 break;
1212 }
David Srbecky297ed222015-04-15 01:18:12 +01001213 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
Vladimir Marko10c13562015-11-25 14:33:36 +00001214 for (const MethodDebugInfo* mi : compilation_unit.methods_) {
David Srbecky04b05262015-11-09 18:05:48 +00001215 // Ignore function if we have already generated line table for the same address.
1216 // It would confuse the debugger and the DWARF specification forbids it.
1217 if (mi->deduped_) {
1218 continue;
1219 }
1220
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001221 ArrayRef<const SrcMapElem> src_mapping_table;
1222 std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
1223 if (IsFromOptimizingCompiler(mi)) {
1224 // Use stack maps to create mapping table from pc to dex.
1225 const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data());
1226 const StackMapEncoding encoding = code_info.ExtractEncoding();
1227 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
1228 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
1229 DCHECK(stack_map.IsValid());
David Srbecky4f50ee22016-01-25 17:00:24 +00001230 // Emit only locations where we have local-variable information.
1231 // In particular, skip mappings inside the prologue.
1232 if (stack_map.HasDexRegisterMap(encoding)) {
1233 const uint32_t pc = stack_map.GetNativePcOffset(encoding);
1234 const int32_t dex = stack_map.GetDexPc(encoding);
1235 src_mapping_table_from_stack_maps.push_back({pc, dex});
1236 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001237 }
1238 std::sort(src_mapping_table_from_stack_maps.begin(),
1239 src_mapping_table_from_stack_maps.end());
1240 src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
1241 } else {
1242 // Use the mapping table provided by the quick compiler.
1243 src_mapping_table = mi->compiled_method_->GetSrcMappingTable();
1244 }
1245
1246 if (src_mapping_table.empty()) {
1247 continue;
1248 }
1249
David Srbecky6d8c8f02015-10-26 10:57:09 +00001250 Elf_Addr method_address = text_address + mi->low_pc_;
1251
David Srbeckyb06e28e2015-12-10 13:15:00 +00001252 PositionInfos position_infos;
David Srbecky799b8c42015-04-14 01:57:43 +01001253 const DexFile* dex = mi->dex_file_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001254 if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) {
1255 continue;
David Srbecky3b9d57a2015-04-10 00:22:14 +01001256 }
1257
David Srbeckyb06e28e2015-12-10 13:15:00 +00001258 if (position_infos.empty()) {
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001259 continue;
1260 }
1261
1262 opcodes.SetAddress(method_address);
1263 if (dwarf_isa != -1) {
1264 opcodes.SetISA(dwarf_isa);
1265 }
1266
David Srbecky799b8c42015-04-14 01:57:43 +01001267 // Get and deduplicate directory and filename.
1268 int file_index = 0; // 0 - primary source file of the compilation.
1269 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
1270 const char* source_file = dex->GetSourceFile(dex_class_def);
1271 if (source_file != nullptr) {
1272 std::string file_name(source_file);
1273 size_t file_name_slash = file_name.find_last_of('/');
1274 std::string class_name(dex->GetClassDescriptor(dex_class_def));
1275 size_t class_name_slash = class_name.find_last_of('/');
1276 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001277
David Srbecky799b8c42015-04-14 01:57:43 +01001278 // Guess directory from package name.
1279 int directory_index = 0; // 0 - current directory of the compilation.
1280 if (file_name_slash == std::string::npos && // Just filename.
1281 class_name.front() == 'L' && // Type descriptor for a class.
1282 class_name_slash != std::string::npos) { // Has package name.
1283 std::string package_name = class_name.substr(1, class_name_slash - 1);
1284 auto it = directories_map.find(package_name);
1285 if (it == directories_map.end()) {
1286 directory_index = 1 + directories.size();
1287 directories_map.emplace(package_name, directory_index);
1288 directories.push_back(package_name);
1289 } else {
1290 directory_index = it->second;
1291 }
1292 full_path = package_name + "/" + file_name;
1293 }
1294
1295 // Add file entry.
1296 auto it2 = files_map.find(full_path);
1297 if (it2 == files_map.end()) {
1298 file_index = 1 + files.size();
1299 files_map.emplace(full_path, file_index);
1300 files.push_back(FileEntry {
1301 file_name,
1302 directory_index,
1303 0, // Modification time - NA.
1304 0, // File size - NA.
1305 });
1306 } else {
1307 file_index = it2->second;
1308 }
1309 }
1310 opcodes.SetFile(file_index);
1311
1312 // Generate mapping opcodes from PC to Java lines.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001313 if (file_index != 0) {
David Srbecky799b8c42015-04-14 01:57:43 +01001314 bool first = true;
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001315 for (SrcMapElem pc2dex : src_mapping_table) {
David Srbecky799b8c42015-04-14 01:57:43 +01001316 uint32_t pc = pc2dex.from_;
1317 int dex_pc = pc2dex.to_;
David Srbeckyb06e28e2015-12-10 13:15:00 +00001318 // Find mapping with address with is greater than our dex pc; then go back one step.
1319 auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc,
1320 [](uint32_t address, const DexFile::PositionInfo& entry) {
1321 return address < entry.address_;
1322 });
1323 if (ub != position_infos.begin()) {
1324 int line = (--ub)->line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001325 if (first) {
1326 first = false;
1327 if (pc > 0) {
1328 // Assume that any preceding code is prologue.
David Srbeckyb06e28e2015-12-10 13:15:00 +00001329 int first_line = position_infos.front().line_;
David Srbecky799b8c42015-04-14 01:57:43 +01001330 // Prologue is not a sensible place for a breakpoint.
1331 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +00001332 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +01001333 opcodes.NegateStmt();
1334 opcodes.SetPrologueEnd();
1335 }
David Srbecky6d8c8f02015-10-26 10:57:09 +00001336 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +01001337 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +00001338 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001339 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001340 }
1341 }
David Srbecky799b8c42015-04-14 01:57:43 +01001342 } else {
1343 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +00001344 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +01001345 }
David Srbeckyf71b3ad2015-12-08 15:05:08 +00001346
1347 opcodes.AdvancePC(text_address + mi->high_pc_);
1348 opcodes.EndSequence();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001349 }
David Srbeckyb851b492015-11-11 20:19:38 +00001350 std::vector<uint8_t> buffer;
1351 buffer.reserve(opcodes.data()->size() + KB);
1352 size_t offset = builder_->GetDebugLine()->GetSize();
1353 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
1354 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
1355 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +01001356 }
David Srbeckyb851b492015-11-11 20:19:38 +00001357
1358 void End() {
1359 builder_->GetDebugLine()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +00001360 builder_->WritePatches(".debug_line.oat_patches",
1361 ArrayRef<const uintptr_t>(debug_line_patches));
David Srbeckyb851b492015-11-11 20:19:38 +00001362 }
1363
1364 private:
1365 ElfBuilder<ElfTypes>* builder_;
1366 std::vector<uintptr_t> debug_line_patches;
1367};
1368
1369template<typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001370static void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
Tamas Berghammer86e42782016-01-05 14:29:02 +00001371 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyb851b492015-11-11 20:19:38 +00001372 // Group the methods into compilation units based on source file.
1373 std::vector<CompilationUnit> compilation_units;
1374 const char* last_source_file = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +00001375 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky04b05262015-11-09 18:05:48 +00001376 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
1377 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
1378 if (compilation_units.empty() || source_file != last_source_file) {
1379 compilation_units.push_back(CompilationUnit());
David Srbeckyb851b492015-11-11 20:19:38 +00001380 }
David Srbecky04b05262015-11-09 18:05:48 +00001381 CompilationUnit& cu = compilation_units.back();
1382 cu.methods_.push_back(&mi);
1383 cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
1384 cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
1385 last_source_file = source_file;
David Srbeckyb851b492015-11-11 20:19:38 +00001386 }
1387
1388 // Write .debug_line section.
Tamas Berghammer86e42782016-01-05 14:29:02 +00001389 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001390 DebugLineWriter<ElfTypes> line_writer(builder);
1391 line_writer.Start();
David Srbeckyb851b492015-11-11 20:19:38 +00001392 for (auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001393 line_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001394 }
1395 line_writer.End();
1396 }
1397
1398 // Write .debug_info section.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001399 if (!compilation_units.empty()) {
David Srbeckyb851b492015-11-11 20:19:38 +00001400 DebugInfoWriter<ElfTypes> info_writer(builder);
1401 info_writer.Start();
1402 for (const auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +00001403 info_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +00001404 }
1405 info_writer.End();
1406 }
David Srbecky3b9d57a2015-04-10 00:22:14 +01001407}
1408
David Srbeckye0febdf2015-12-17 20:53:07 +00001409template <typename ElfTypes>
David Srbecky52886112016-01-22 13:56:47 +00001410static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
1411 const ArrayRef<const MethodDebugInfo>& method_infos,
1412 bool with_signature) {
David Srbeckye0febdf2015-12-17 20:53:07 +00001413 bool generated_mapping_symbol = false;
1414 auto* strtab = builder->GetStrTab();
1415 auto* symtab = builder->GetSymTab();
1416
1417 if (method_infos.empty()) {
1418 return;
1419 }
1420
1421 // Find all addresses (low_pc) which contain deduped methods.
1422 // The first instance of method is not marked deduped_, but the rest is.
1423 std::unordered_set<uint32_t> deduped_addresses;
1424 for (const MethodDebugInfo& info : method_infos) {
1425 if (info.deduped_) {
1426 deduped_addresses.insert(info.low_pc_);
1427 }
1428 }
1429
1430 strtab->Start();
1431 strtab->Write(""); // strtab should start with empty string.
David Srbecky52886112016-01-22 13:56:47 +00001432 std::string last_name;
1433 size_t last_name_offset = 0;
David Srbeckye0febdf2015-12-17 20:53:07 +00001434 for (const MethodDebugInfo& info : method_infos) {
1435 if (info.deduped_) {
1436 continue; // Add symbol only for the first instance.
1437 }
David Srbecky52886112016-01-22 13:56:47 +00001438 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, with_signature);
David Srbeckye0febdf2015-12-17 20:53:07 +00001439 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
1440 name += " [DEDUPED]";
1441 }
David Srbecky52886112016-01-22 13:56:47 +00001442 // If we write method names without signature, we might see the same name multiple times.
1443 size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name));
David Srbeckye0febdf2015-12-17 20:53:07 +00001444
David Srbecky5cc349f2015-12-18 15:04:48 +00001445 const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr;
1446 const bool is_relative = (text != nullptr);
David Srbeckye0febdf2015-12-17 20:53:07 +00001447 uint32_t low_pc = info.low_pc_;
1448 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
1449 low_pc += info.compiled_method_->CodeDelta();
David Srbecky52886112016-01-22 13:56:47 +00001450 symtab->Add(name_offset,
1451 text,
1452 low_pc,
1453 is_relative,
1454 info.high_pc_ - info.low_pc_,
1455 STB_GLOBAL,
1456 STT_FUNC);
David Srbeckye0febdf2015-12-17 20:53:07 +00001457
1458 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
1459 // instructions, so that disassembler tools can correctly disassemble.
1460 // Note that even if we generate just a single mapping symbol, ARM's Streamline
1461 // requires it to match function symbol. Just address 0 does not work.
1462 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
1463 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001464 symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1,
1465 is_relative, 0, STB_LOCAL, STT_NOTYPE);
David Srbeckye0febdf2015-12-17 20:53:07 +00001466 generated_mapping_symbol = true;
1467 }
1468 }
David Srbecky52886112016-01-22 13:56:47 +00001469
1470 last_name = std::move(name);
1471 last_name_offset = name_offset;
David Srbeckye0febdf2015-12-17 20:53:07 +00001472 }
1473 strtab->End();
1474
1475 // Symbols are buffered and written after names (because they are smaller).
1476 // We could also do two passes in this function to avoid the buffering.
1477 symtab->Start();
1478 symtab->Write();
1479 symtab->End();
1480}
1481
1482template <typename ElfTypes>
1483void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
1484 const ArrayRef<const MethodDebugInfo>& method_infos,
1485 CFIFormat cfi_format) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001486 // Add methods to .symtab.
David Srbecky52886112016-01-22 13:56:47 +00001487 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001488 // Generate CFI (stack unwinding information).
David Srbecky5e974a62016-01-22 14:25:03 +00001489 WriteCFISection(builder, method_infos, cfi_format, true /* write_oat_patches */);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001490 // Write DWARF .debug_* sections.
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001491 WriteDebugSections(builder, method_infos);
David Srbeckye0febdf2015-12-17 20:53:07 +00001492}
1493
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001494static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) {
1495 // Configure the compression library.
1496 CrcGenerateTable();
1497 Crc64GenerateTable();
1498 CLzma2EncProps lzma2Props;
1499 Lzma2EncProps_Init(&lzma2Props);
1500 lzma2Props.lzmaProps.level = 1; // Fast compression.
1501 Lzma2EncProps_Normalize(&lzma2Props);
1502 CXzProps props;
1503 XzProps_Init(&props);
1504 props.lzma2Props = &lzma2Props;
1505 // Implement the required interface for communication (written in C so no virtual methods).
1506 struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress {
1507 static SRes ReadImpl(void* p, void* buf, size_t* size) {
1508 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p));
1509 *size = std::min(*size, ctx->src_->size() - ctx->src_pos_);
1510 memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size);
1511 ctx->src_pos_ += *size;
1512 return SZ_OK;
1513 }
1514 static size_t WriteImpl(void* p, const void* buf, size_t size) {
1515 auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p));
1516 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
1517 ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size);
1518 return size;
1519 }
1520 static SRes ProgressImpl(void* , UInt64, UInt64) {
1521 return SZ_OK;
1522 }
1523 size_t src_pos_;
1524 const std::vector<uint8_t>* src_;
1525 std::vector<uint8_t>* dst_;
1526 };
1527 XzCallbacks callbacks;
1528 callbacks.Read = XzCallbacks::ReadImpl;
1529 callbacks.Write = XzCallbacks::WriteImpl;
1530 callbacks.Progress = XzCallbacks::ProgressImpl;
1531 callbacks.src_pos_ = 0;
1532 callbacks.src_ = src;
1533 callbacks.dst_ = dst;
1534 // Compress.
1535 SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
1536 CHECK_EQ(res, SZ_OK);
1537}
1538
1539template <typename ElfTypes>
1540void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder,
1541 const ArrayRef<const MethodDebugInfo>& method_infos) {
1542 const InstructionSet isa = parent_builder->GetIsa();
1543 std::vector<uint8_t> buffer;
1544 buffer.reserve(KB);
1545 VectorOutputStream out("Mini-debug-info ELF file", &buffer);
1546 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1547 builder->Start();
1548 // Write .rodata and .text as NOBITS sections.
1549 // This allows tools to detect virtual address relocation of the parent ELF file.
1550 builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress());
1551 builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize());
1552 builder->SetVirtualAddress(parent_builder->GetText()->GetAddress());
1553 builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize());
David Srbecky52886112016-01-22 13:56:47 +00001554 WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */);
David Srbecky5e974a62016-01-22 14:25:03 +00001555 WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001556 builder->End();
1557 CHECK(builder->Good());
1558 std::vector<uint8_t> compressed_buffer;
1559 compressed_buffer.reserve(buffer.size() / 4);
1560 XzCompress(&buffer, &compressed_buffer);
1561 parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer);
1562}
1563
David Srbecky5cc349f2015-12-18 15:04:48 +00001564template <typename ElfTypes>
Tamas Berghammer86e42782016-01-05 14:29:02 +00001565static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal(
David Srbecky5cc349f2015-12-18 15:04:48 +00001566 const dwarf::MethodDebugInfo& method_info) {
1567 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1568 std::vector<uint8_t> buffer;
1569 buffer.reserve(KB);
1570 VectorOutputStream out("Debug ELF file", &buffer);
1571 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1572 builder->Start();
1573 WriteDebugInfo(builder.get(),
1574 ArrayRef<const MethodDebugInfo>(&method_info, 1),
1575 DW_DEBUG_FRAME_FORMAT);
1576 builder->End();
1577 CHECK(builder->Good());
1578 // Make a copy of the buffer. We want to shrink it anyway.
1579 uint8_t* result = new uint8_t[buffer.size()];
1580 CHECK(result != nullptr);
1581 memcpy(result, buffer.data(), buffer.size());
1582 return ArrayRef<const uint8_t>(result, buffer.size());
1583}
1584
Tamas Berghammer86e42782016-01-05 14:29:02 +00001585ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001586 const InstructionSet isa = method_info.compiled_method_->GetInstructionSet();
1587 if (Is64BitInstructionSet(isa)) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001588 return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info);
David Srbecky5cc349f2015-12-18 15:04:48 +00001589 } else {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001590 return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info);
1591 }
1592}
1593
1594template <typename ElfTypes>
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001595static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
1596 const InstructionSet isa, const ArrayRef<mirror::Class*>& types)
Tamas Berghammer86e42782016-01-05 14:29:02 +00001597 SHARED_REQUIRES(Locks::mutator_lock_) {
1598 std::vector<uint8_t> buffer;
1599 buffer.reserve(KB);
1600 VectorOutputStream out("Debug ELF file", &buffer);
1601 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out));
1602 builder->Start();
1603
1604 DebugInfoWriter<ElfTypes> info_writer(builder.get());
1605 info_writer.Start();
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001606 info_writer.WriteTypes(types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001607 info_writer.End();
1608
1609 builder->End();
1610 CHECK(builder->Good());
1611 // Make a copy of the buffer. We want to shrink it anyway.
1612 uint8_t* result = new uint8_t[buffer.size()];
1613 CHECK(result != nullptr);
1614 memcpy(result, buffer.data(), buffer.size());
1615 return ArrayRef<const uint8_t>(result, buffer.size());
1616}
1617
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001618ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa,
1619 const ArrayRef<mirror::Class*>& types) {
Tamas Berghammer86e42782016-01-05 14:29:02 +00001620 if (Is64BitInstructionSet(isa)) {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001621 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types);
Tamas Berghammer86e42782016-01-05 14:29:02 +00001622 } else {
Tamas Berghammerfffbee42016-01-15 13:09:34 +00001623 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types);
David Srbecky5cc349f2015-12-18 15:04:48 +00001624 }
1625}
1626
David Srbecky6d8c8f02015-10-26 10:57:09 +00001627// Explicit instantiations
David Srbeckye0febdf2015-12-17 20:53:07 +00001628template void WriteDebugInfo<ElfTypes32>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001629 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001630 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001631 CFIFormat cfi_format);
1632template void WriteDebugInfo<ElfTypes64>(
David Srbecky6d8c8f02015-10-26 10:57:09 +00001633 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +00001634 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbeckye0febdf2015-12-17 20:53:07 +00001635 CFIFormat cfi_format);
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001636template void WriteMiniDebugInfo<ElfTypes32>(
1637 ElfBuilder<ElfTypes32>* builder,
1638 const ArrayRef<const MethodDebugInfo>& method_infos);
1639template void WriteMiniDebugInfo<ElfTypes64>(
1640 ElfBuilder<ElfTypes64>* builder,
1641 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +00001642
David Srbecky3b9d57a2015-04-10 00:22:14 +01001643} // namespace dwarf
1644} // namespace art