blob: 5738779609e7b90e8f8a0d6d5d85e4be8c77e6a7 [file] [log] [blame]
David Srbecky3b9d57a2015-04-10 00:22:14 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "elf_writer_debug.h"
18
David Srbecky626a1662015-04-12 13:12:26 +010019#include <unordered_set>
Vladimir Marko10c13562015-11-25 14:33:36 +000020#include <vector>
David Srbecky626a1662015-04-12 13:12:26 +010021
Andreas Gampee3d623e2015-05-01 16:11:04 -070022#include "base/casts.h"
David Srbecky04b05262015-11-09 18:05:48 +000023#include "base/stl_util.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010024#include "compiled_method.h"
25#include "driver/compiler_driver.h"
26#include "dex_file-inl.h"
David Srbecky04b05262015-11-09 18:05:48 +000027#include "dwarf/dedup_vector.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010028#include "dwarf/headers.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000029#include "dwarf/method_debug_info.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010030#include "dwarf/register.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000031#include "elf_builder.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010032#include "oat_writer.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010033#include "utils.h"
David Srbecky0fd295f2015-11-16 16:39:10 +000034#include "stack_map.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010035
36namespace art {
37namespace dwarf {
38
David Srbecky0fd295f2015-11-16 16:39:10 +000039static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
40 switch (isa) {
41 case kArm:
42 case kThumb2:
43 return Reg::ArmCore(machine_reg);
44 case kArm64:
45 return Reg::Arm64Core(machine_reg);
46 case kX86:
47 return Reg::X86Core(machine_reg);
48 case kX86_64:
49 return Reg::X86_64Core(machine_reg);
50 case kMips:
51 return Reg::MipsCore(machine_reg);
52 case kMips64:
53 return Reg::Mips64Core(machine_reg);
54 default:
55 LOG(FATAL) << "Unknown instruction set: " << isa;
56 UNREACHABLE();
57 }
58}
59
60static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
61 switch (isa) {
62 case kArm:
63 case kThumb2:
64 return Reg::ArmFp(machine_reg);
65 case kArm64:
66 return Reg::Arm64Fp(machine_reg);
67 case kX86:
68 return Reg::X86Fp(machine_reg);
69 case kX86_64:
70 return Reg::X86_64Fp(machine_reg);
71 default:
72 LOG(FATAL) << "Unknown instruction set: " << isa;
73 UNREACHABLE();
74 }
75}
76
David Srbecky6d8c8f02015-10-26 10:57:09 +000077static void WriteCIE(InstructionSet isa,
78 CFIFormat format,
79 std::vector<uint8_t>* buffer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +010080 // Scratch registers should be marked as undefined. This tells the
81 // debugger that its value in the previous frame is not recoverable.
82 bool is64bit = Is64BitInstructionSet(isa);
83 switch (isa) {
84 case kArm:
85 case kThumb2: {
86 DebugFrameOpCodeWriter<> opcodes;
87 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
88 // core registers.
89 for (int reg = 0; reg < 13; reg++) {
90 if (reg < 4 || reg == 12) {
91 opcodes.Undefined(Reg::ArmCore(reg));
92 } else {
93 opcodes.SameValue(Reg::ArmCore(reg));
94 }
95 }
96 // fp registers.
97 for (int reg = 0; reg < 32; reg++) {
98 if (reg < 16) {
99 opcodes.Undefined(Reg::ArmFp(reg));
100 } else {
101 opcodes.SameValue(Reg::ArmFp(reg));
102 }
103 }
David Srbecky527c9c72015-04-17 21:14:10 +0100104 auto return_reg = Reg::ArmCore(14); // R14(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000105 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100106 return;
107 }
108 case kArm64: {
109 DebugFrameOpCodeWriter<> opcodes;
110 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
111 // core registers.
112 for (int reg = 0; reg < 30; reg++) {
113 if (reg < 8 || reg == 16 || reg == 17) {
114 opcodes.Undefined(Reg::Arm64Core(reg));
115 } else {
116 opcodes.SameValue(Reg::Arm64Core(reg));
117 }
118 }
119 // fp registers.
120 for (int reg = 0; reg < 32; reg++) {
121 if (reg < 8 || reg >= 16) {
122 opcodes.Undefined(Reg::Arm64Fp(reg));
123 } else {
124 opcodes.SameValue(Reg::Arm64Fp(reg));
125 }
126 }
David Srbecky527c9c72015-04-17 21:14:10 +0100127 auto return_reg = Reg::Arm64Core(30); // R30(LR).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100129 return;
130 }
131 case kMips:
132 case kMips64: {
133 DebugFrameOpCodeWriter<> opcodes;
134 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
135 // core registers.
136 for (int reg = 1; reg < 26; reg++) {
137 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
138 opcodes.Undefined(Reg::MipsCore(reg));
139 } else {
140 opcodes.SameValue(Reg::MipsCore(reg));
141 }
142 }
David Srbecky527c9c72015-04-17 21:14:10 +0100143 auto return_reg = Reg::MipsCore(31); // R31(RA).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100145 return;
146 }
147 case kX86: {
David Srbecky8a813f72015-04-20 16:43:52 +0100148 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
149 constexpr bool generate_opcodes_for_x86_fp = false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100150 DebugFrameOpCodeWriter<> opcodes;
151 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
152 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
153 // core registers.
154 for (int reg = 0; reg < 8; reg++) {
155 if (reg <= 3) {
156 opcodes.Undefined(Reg::X86Core(reg));
157 } else if (reg == 4) {
158 // Stack pointer.
159 } else {
160 opcodes.SameValue(Reg::X86Core(reg));
161 }
162 }
163 // fp registers.
David Srbecky8a813f72015-04-20 16:43:52 +0100164 if (generate_opcodes_for_x86_fp) {
165 for (int reg = 0; reg < 8; reg++) {
166 opcodes.Undefined(Reg::X86Fp(reg));
167 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100168 }
David Srbecky527c9c72015-04-17 21:14:10 +0100169 auto return_reg = Reg::X86Core(8); // R8(EIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100171 return;
172 }
173 case kX86_64: {
174 DebugFrameOpCodeWriter<> opcodes;
175 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
176 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
177 // core registers.
178 for (int reg = 0; reg < 16; reg++) {
179 if (reg == 4) {
180 // Stack pointer.
181 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
182 opcodes.Undefined(Reg::X86_64Core(reg));
183 } else {
184 opcodes.SameValue(Reg::X86_64Core(reg));
185 }
186 }
187 // fp registers.
188 for (int reg = 0; reg < 16; reg++) {
189 if (reg < 12) {
190 opcodes.Undefined(Reg::X86_64Fp(reg));
191 } else {
192 opcodes.SameValue(Reg::X86_64Fp(reg));
193 }
194 }
David Srbecky527c9c72015-04-17 21:14:10 +0100195 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000196 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100197 return;
198 }
199 case kNone:
200 break;
201 }
202 LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
203 UNREACHABLE();
204}
205
David Srbecky6d8c8f02015-10-26 10:57:09 +0000206template<typename ElfTypes>
207void WriteCFISection(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000208 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000209 CFIFormat format) {
210 CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT ||
211 format == dwarf::DW_EH_FRAME_FORMAT);
212 typedef typename ElfTypes::Addr Elf_Addr;
213
214 std::vector<uint32_t> binary_search_table;
215 std::vector<uintptr_t> patch_locations;
216 if (format == DW_EH_FRAME_FORMAT) {
217 binary_search_table.reserve(2 * method_infos.size());
218 } else {
219 patch_locations.reserve(method_infos.size());
220 }
David Srbecky527c9c72015-04-17 21:14:10 +0100221
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100222 // Write .eh_frame/.debug_frame section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000223 auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT
224 ? builder->GetDebugFrame()
225 : builder->GetEhFrame());
226 {
227 cfi_section->Start();
228 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
229 const Elf_Addr text_address = builder->GetText()->GetAddress();
230 const Elf_Addr cfi_address = cfi_section->GetAddress();
231 const Elf_Addr cie_address = cfi_address;
232 Elf_Addr buffer_address = cfi_address;
233 std::vector<uint8_t> buffer; // Small temporary buffer.
234 WriteCIE(builder->GetIsa(), format, &buffer);
235 cfi_section->WriteFully(buffer.data(), buffer.size());
236 buffer_address += buffer.size();
237 buffer.clear();
Vladimir Marko10c13562015-11-25 14:33:36 +0000238 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000239 if (!mi.deduped_) { // Only one FDE per unique address.
240 ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo();
241 if (!opcodes.empty()) {
242 const Elf_Addr code_address = text_address + mi.low_pc_;
243 if (format == DW_EH_FRAME_FORMAT) {
244 binary_search_table.push_back(
245 dchecked_integral_cast<uint32_t>(code_address));
246 binary_search_table.push_back(
247 dchecked_integral_cast<uint32_t>(buffer_address));
248 }
249 WriteFDE(is64bit, cfi_address, cie_address,
250 code_address, mi.high_pc_ - mi.low_pc_,
251 opcodes, format, buffer_address, &buffer,
252 &patch_locations);
253 cfi_section->WriteFully(buffer.data(), buffer.size());
254 buffer_address += buffer.size();
255 buffer.clear();
256 }
David Srbecky6d73c9d2015-05-01 15:00:40 +0100257 }
David Srbecky8dc73242015-04-12 11:40:39 +0100258 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000259 cfi_section->End();
David Srbecky8dc73242015-04-12 11:40:39 +0100260 }
David Srbecky527c9c72015-04-17 21:14:10 +0100261
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100262 if (format == DW_EH_FRAME_FORMAT) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000263 auto* header_section = builder->GetEhFrameHdr();
264 header_section->Start();
265 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100266 // Write .eh_frame_hdr section.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000267 std::vector<uint8_t> buffer;
268 Writer<> header(&buffer);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100269 header.PushUint8(1); // Version.
270 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
271 // so we have to use pcrel which means relative to the pointer's location.
272 header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
273 // Encoding of binary search table size.
274 header.PushUint8(DW_EH_PE_udata4);
275 // Encoding of binary search table addresses - libunwind supports only this
276 // specific combination, which means relative to the start of .eh_frame_hdr.
277 header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000278 // .eh_frame pointer
279 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100280 // Binary search table size (number of entries).
David Srbecky6d8c8f02015-10-26 10:57:09 +0000281 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
282 header_section->WriteFully(buffer.data(), buffer.size());
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100283 // Binary search table.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000284 for (size_t i = 0; i < binary_search_table.size(); i++) {
285 // Make addresses section-relative since we know the header address now.
286 binary_search_table[i] -= header_address;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100287 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000288 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
289 header_section->End();
290 } else {
Vladimir Marko10c13562015-11-25 14:33:36 +0000291 builder->WritePatches(".debug_frame.oat_patches",
292 ArrayRef<const uintptr_t>(patch_locations));
David Srbecky033d7452015-04-30 19:57:35 +0100293 }
David Srbecky8dc73242015-04-12 11:40:39 +0100294}
295
David Srbecky04b05262015-11-09 18:05:48 +0000296struct CompilationUnit {
Vladimir Marko10c13562015-11-25 14:33:36 +0000297 std::vector<const MethodDebugInfo*> methods_;
David Srbecky04b05262015-11-09 18:05:48 +0000298 size_t debug_line_offset_ = 0;
299 uint32_t low_pc_ = 0xFFFFFFFFU;
300 uint32_t high_pc_ = 0;
301};
302
303// Helper class to write .debug_info and its supporting sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000304template<typename ElfTypes>
David Srbeckyb851b492015-11-11 20:19:38 +0000305class DebugInfoWriter {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000306 typedef typename ElfTypes::Addr Elf_Addr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100307
David Srbecky04b05262015-11-09 18:05:48 +0000308 // Helper class to write one compilation unit.
309 // It holds helper methods and temporary state.
310 class CompilationUnitWriter {
311 public:
312 explicit CompilationUnitWriter(DebugInfoWriter* owner)
313 : owner_(owner),
314 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) {
315 }
316
317 void Write(const CompilationUnit& compilation_unit) {
318 CHECK(!compilation_unit.methods_.empty());
319 const Elf_Addr text_address = owner_->builder_->GetText()->GetAddress();
320
321 info_.StartTag(DW_TAG_compile_unit);
322 info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
323 info_.WriteData1(DW_AT_language, DW_LANG_Java);
324 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000325 info_.WriteUdata(DW_AT_high_pc, compilation_unit.high_pc_ - compilation_unit.low_pc_);
326 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
David Srbecky04b05262015-11-09 18:05:48 +0000327
328 const char* last_dex_class_desc = nullptr;
329 for (auto mi : compilation_unit.methods_) {
330 const DexFile* dex = mi->dex_file_;
331 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
332 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
333 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
334 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
335
336 // Enclose the method in correct class definition.
337 if (last_dex_class_desc != dex_class_desc) {
338 if (last_dex_class_desc != nullptr) {
339 EndClassTag(last_dex_class_desc);
340 }
341 size_t offset = StartClassTag(dex_class_desc);
342 type_cache_.emplace(dex_class_desc, offset);
343 // Check that each class is defined only once.
344 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
345 CHECK(unique) << "Redefinition of " << dex_class_desc;
346 last_dex_class_desc = dex_class_desc;
347 }
348
349 std::vector<const char*> param_names;
350 if (mi->code_item_ != nullptr) {
351 const uint8_t* stream = dex->GetDebugInfoStream(mi->code_item_);
352 if (stream != nullptr) {
353 DecodeUnsignedLeb128(&stream); // line.
354 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
355 for (uint32_t i = 0; i < parameters_size; ++i) {
356 uint32_t id = DecodeUnsignedLeb128P1(&stream);
357 param_names.push_back(mi->dex_file_->StringDataByIdx(id));
358 }
359 }
360 }
361
362 int start_depth = info_.Depth();
363 info_.StartTag(DW_TAG_subprogram);
364 WriteName(dex->GetMethodName(dex_method));
365 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
David Srbecky0fd295f2015-11-16 16:39:10 +0000366 info_.WriteUdata(DW_AT_high_pc, mi->high_pc_ - mi->low_pc_);
367 uint8_t frame_base[] = { DW_OP_call_frame_cfa };
368 info_.WriteExprLoc(DW_AT_frame_base, &frame_base, sizeof(frame_base));
David Srbecky04b05262015-11-09 18:05:48 +0000369 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
370 if (dex_params != nullptr) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000371 uint32_t vreg = mi->code_item_ == nullptr ? 0 :
372 mi->code_item_->registers_size_ - mi->code_item_->ins_size_;
373 if ((mi->access_flags_ & kAccStatic) == 0) {
374 info_.StartTag(DW_TAG_formal_parameter);
375 WriteName("this");
376 info_.WriteFlag(DW_AT_artificial, true);
377 WriteLazyType(dex_class_desc);
378 const bool is64bitValue = false;
379 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
380 vreg++;
381 info_.EndTag();
382 }
David Srbecky04b05262015-11-09 18:05:48 +0000383 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
384 info_.StartTag(DW_TAG_formal_parameter);
385 // Parameter names may not be always available.
386 if (i < param_names.size() && param_names[i] != nullptr) {
387 WriteName(param_names[i]);
388 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000389 // Write the type.
390 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
391 WriteLazyType(type_desc);
392 // Write the stack location of the parameter.
393 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
394 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
395 vreg += is64bitValue ? 2 : 1;
David Srbecky04b05262015-11-09 18:05:48 +0000396 info_.EndTag();
397 }
David Srbecky0fd295f2015-11-16 16:39:10 +0000398 if (mi->code_item_ != nullptr) {
399 CHECK_EQ(vreg, mi->code_item_->registers_size_);
400 }
David Srbecky04b05262015-11-09 18:05:48 +0000401 }
402 info_.EndTag();
403 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
404 }
405 if (last_dex_class_desc != nullptr) {
406 EndClassTag(last_dex_class_desc);
407 }
408 CHECK_EQ(info_.Depth(), 1);
409 FinishLazyTypes();
410 info_.EndTag(); // DW_TAG_compile_unit
411 std::vector<uint8_t> buffer;
412 buffer.reserve(info_.data()->size() + KB);
413 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
414 const size_t debug_abbrev_offset =
415 owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
416 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
417 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
418 }
419
David Srbecky0fd295f2015-11-16 16:39:10 +0000420 // Write table into .debug_loc which describes location of dex register.
421 // The dex register might be valid only at some points and it might
422 // move between machine registers and stack.
Vladimir Marko10c13562015-11-25 14:33:36 +0000423 void WriteRegLocation(const MethodDebugInfo* method_info, uint16_t vreg,
David Srbecky0fd295f2015-11-16 16:39:10 +0000424 bool is64bitValue, uint32_t compilation_unit_low_pc) {
425 using Kind = DexRegisterLocation::Kind;
426 bool is_optimizing = method_info->compiled_method_->GetQuickCode().size() > 0 &&
427 method_info->compiled_method_->GetVmapTable().size() > 0 &&
428 method_info->compiled_method_->GetGcMap().size() == 0 &&
429 method_info->code_item_ != nullptr;
430 if (!is_optimizing) {
431 return;
432 }
433
434 Writer<> writer(&owner_->debug_loc_);
435 info_.WriteSecOffset(DW_AT_location, writer.size());
436
437 const InstructionSet isa = owner_->builder_->GetIsa();
438 const bool is64bit = Is64BitInstructionSet(isa);
439 const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
440 const StackMapEncoding encoding = code_info.ExtractEncoding();
441 DexRegisterLocation last_reg_lo = DexRegisterLocation::None();
442 DexRegisterLocation last_reg_hi = DexRegisterLocation::None();
443 size_t offset_of_last_end_address = 0;
444 for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
445 StackMap stack_map = code_info.GetStackMapAt(s, encoding);
446 DCHECK(stack_map.IsValid());
447
448 // Find the location of the dex register.
449 DexRegisterLocation reg_lo = DexRegisterLocation::None();
450 DexRegisterLocation reg_hi = DexRegisterLocation::None();
451 if (stack_map.HasDexRegisterMap(encoding)) {
452 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
453 stack_map, encoding, method_info->code_item_->registers_size_);
454 reg_lo = dex_register_map.GetDexRegisterLocation(
455 vreg, method_info->code_item_->registers_size_, code_info, encoding);
456 if (is64bitValue) {
457 reg_hi = dex_register_map.GetDexRegisterLocation(
458 vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
459 }
460 }
461 if ((reg_lo == last_reg_lo && reg_hi == last_reg_hi) ||
462 reg_lo.GetKind() == Kind::kNone) {
463 // Skip identical or undefined locations.
464 continue;
465 }
466 last_reg_lo = reg_lo;
467 last_reg_hi = reg_hi;
468
469 // Translate dex register location to DWARF expression.
470 // Note that 64-bit value might be split to two distinct locations.
471 // (for example, two 32-bit machine registers, or even stack and register)
472 uint8_t buffer[64];
473 uint8_t* pos = buffer;
474 for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
475 DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
476 const Kind kind = reg_loc.GetKind();
477 const int32_t value = reg_loc.GetValue();
478 if (kind == Kind::kInStack) {
479 const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
480 *(pos++) = DW_OP_fbreg;
481 // The stack offset is relative to SP. Make it relative to CFA.
482 pos = EncodeSignedLeb128(pos, value - frame_size);
483 if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
484 reg_hi.GetValue() == value + 4) {
485 break; // the high word is correctly implied by the low word.
486 }
487 } else if (kind == Kind::kInRegister) {
488 pos = WriteOpReg(pos, GetDwarfCoreReg(isa, value).num());
489 if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
490 reg_hi.GetValue() == value) {
491 break; // the high word is correctly implied by the low word.
492 }
493 } else if (kind == Kind::kInFpuRegister) {
494 if ((isa == kArm || isa == kThumb2) &&
495 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
496 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
497 // Translate S register pair to D register (e.g. S4+S5 to D2).
498 pos = WriteOpReg(pos, Reg::ArmDp(value / 2).num());
499 break;
500 }
501 pos = WriteOpReg(pos, GetDwarfFpReg(isa, value).num());
502 if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
503 reg_hi.GetValue() == reg_lo.GetValue()) {
504 break; // the high word is correctly implied by the low word.
505 }
506 } else if (kind == Kind::kConstant) {
507 *(pos++) = DW_OP_consts;
508 pos = EncodeSignedLeb128(pos, value);
509 *(pos++) = DW_OP_stack_value;
510 } else if (kind == Kind::kNone) {
511 break;
512 } else {
513 // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
514 // kInRegisterHigh and kInFpuRegisterHigh should be handled by
515 // the special cases above and they should not occur alone.
516 LOG(ERROR) << "Unexpected register location kind: "
517 << DexRegisterLocation::PrettyDescriptor(kind);
518 break;
519 }
520 if (is64bitValue) {
521 // Write the marker which is needed by split 64-bit values.
522 // This code is skipped by the special cases.
523 *(pos++) = DW_OP_piece;
524 pos = EncodeUnsignedLeb128(pos, 4);
525 }
526 }
527
528 // Write end address for previous entry.
529 const uint32_t pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
530 if (offset_of_last_end_address != 0) {
531 if (is64bit) {
532 writer.UpdateUint64(offset_of_last_end_address, pc - compilation_unit_low_pc);
533 } else {
534 writer.UpdateUint32(offset_of_last_end_address, pc - compilation_unit_low_pc);
535 }
536 }
537 offset_of_last_end_address = 0;
538
539 DCHECK_LE(static_cast<size_t>(pos - buffer), sizeof(buffer));
540 if (pos > buffer) {
541 // Write start/end address.
542 if (is64bit) {
543 writer.PushUint64(pc - compilation_unit_low_pc);
544 offset_of_last_end_address = writer.size();
545 writer.PushUint64(method_info->high_pc_ - compilation_unit_low_pc);
546 } else {
547 writer.PushUint32(pc - compilation_unit_low_pc);
548 offset_of_last_end_address = writer.size();
549 writer.PushUint32(method_info->high_pc_ - compilation_unit_low_pc);
550 }
551 // Write the expression.
552 writer.PushUint16(pos - buffer);
553 writer.PushData(buffer, pos - buffer);
554 } else {
555 // Otherwise leave the address range undefined.
556 }
557 }
558 // Write end-of-list entry.
559 if (is64bit) {
560 writer.PushUint64(0);
561 writer.PushUint64(0);
562 } else {
563 writer.PushUint32(0);
564 writer.PushUint32(0);
565 }
566 }
567
David Srbecky04b05262015-11-09 18:05:48 +0000568 // Some types are difficult to define as we go since they need
569 // to be enclosed in the right set of namespaces. Therefore we
570 // just define all types lazily at the end of compilation unit.
571 void WriteLazyType(const char* type_descriptor) {
572 DCHECK(type_descriptor != nullptr);
573 if (type_descriptor[0] != 'V') {
574 lazy_types_.emplace(type_descriptor, info_.size());
575 info_.WriteRef4(DW_AT_type, 0);
576 }
577 }
578
579 void FinishLazyTypes() {
580 for (const auto& lazy_type : lazy_types_) {
581 info_.UpdateUint32(lazy_type.second, WriteType(lazy_type.first));
582 }
583 lazy_types_.clear();
584 }
585
586 private:
587 void WriteName(const char* name) {
588 info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
589 }
590
David Srbecky0fd295f2015-11-16 16:39:10 +0000591 // Helper which writes DWARF expression referencing a register.
592 static uint8_t* WriteOpReg(uint8_t* buffer, uint32_t dwarf_reg_num) {
593 if (dwarf_reg_num < 32) {
594 *(buffer++) = DW_OP_reg0 + dwarf_reg_num;
595 } else {
596 *(buffer++) = DW_OP_regx;
597 buffer = EncodeUnsignedLeb128(buffer, dwarf_reg_num);
598 }
599 return buffer;
600 }
601
David Srbecky04b05262015-11-09 18:05:48 +0000602 // Convert dex type descriptor to DWARF.
603 // Returns offset in the compilation unit.
604 size_t WriteType(const char* desc) {
605 const auto& it = type_cache_.find(desc);
606 if (it != type_cache_.end()) {
607 return it->second;
608 }
609
610 size_t offset;
611 if (*desc == 'L') {
612 // Class type. For example: Lpackage/name;
613 offset = StartClassTag(desc);
614 info_.WriteFlag(DW_AT_declaration, true);
615 EndClassTag(desc);
616 } else if (*desc == '[') {
617 // Array type.
618 size_t element_type = WriteType(desc + 1);
619 offset = info_.StartTag(DW_TAG_array_type);
620 info_.WriteRef(DW_AT_type, element_type);
621 info_.EndTag();
622 } else {
623 // Primitive types.
624 const char* name;
David Srbecky0fd295f2015-11-16 16:39:10 +0000625 uint32_t encoding;
626 uint32_t byte_size;
David Srbecky04b05262015-11-09 18:05:48 +0000627 switch (*desc) {
David Srbecky0fd295f2015-11-16 16:39:10 +0000628 case 'B':
629 name = "byte";
630 encoding = DW_ATE_signed;
631 byte_size = 1;
632 break;
633 case 'C':
634 name = "char";
635 encoding = DW_ATE_UTF;
636 byte_size = 2;
637 break;
638 case 'D':
639 name = "double";
640 encoding = DW_ATE_float;
641 byte_size = 8;
642 break;
643 case 'F':
644 name = "float";
645 encoding = DW_ATE_float;
646 byte_size = 4;
647 break;
648 case 'I':
649 name = "int";
650 encoding = DW_ATE_signed;
651 byte_size = 4;
652 break;
653 case 'J':
654 name = "long";
655 encoding = DW_ATE_signed;
656 byte_size = 8;
657 break;
658 case 'S':
659 name = "short";
660 encoding = DW_ATE_signed;
661 byte_size = 2;
662 break;
663 case 'Z':
664 name = "boolean";
665 encoding = DW_ATE_boolean;
666 byte_size = 1;
667 break;
668 case 'V':
669 LOG(FATAL) << "Void type should not be encoded";
670 UNREACHABLE();
David Srbecky04b05262015-11-09 18:05:48 +0000671 default:
672 LOG(FATAL) << "Unknown dex type descriptor: " << desc;
673 UNREACHABLE();
674 }
675 offset = info_.StartTag(DW_TAG_base_type);
676 WriteName(name);
David Srbecky0fd295f2015-11-16 16:39:10 +0000677 info_.WriteData1(DW_AT_encoding, encoding);
678 info_.WriteData1(DW_AT_byte_size, byte_size);
David Srbecky04b05262015-11-09 18:05:48 +0000679 info_.EndTag();
680 }
681
682 type_cache_.emplace(desc, offset);
683 return offset;
684 }
685
686 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
687 // Returns offset of the class tag in the compilation unit.
688 size_t StartClassTag(const char* desc) {
689 DCHECK(desc != nullptr && desc[0] == 'L');
690 // Enclose the type in namespace tags.
691 const char* end;
692 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
693 info_.StartTag(DW_TAG_namespace);
694 WriteName(std::string(desc, end - desc).c_str());
695 }
696 // Start the class tag.
697 size_t offset = info_.StartTag(DW_TAG_class_type);
698 end = strchr(desc, ';');
699 CHECK(end != nullptr);
700 WriteName(std::string(desc, end - desc).c_str());
701 return offset;
702 }
703
704 void EndClassTag(const char* desc) {
705 DCHECK(desc != nullptr && desc[0] == 'L');
706 // End the class tag.
707 info_.EndTag();
708 // Close namespace tags.
709 const char* end;
710 for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
711 info_.EndTag();
712 }
713 }
714
715 // For access to the ELF sections.
716 DebugInfoWriter<ElfTypes>* owner_;
717 // Debug abbrevs for this compilation unit only.
718 std::vector<uint8_t> debug_abbrev_;
719 // Temporary buffer to create and store the entries.
720 DebugInfoEntryWriter<> info_;
721 // Cache of already translated type descriptors.
722 std::map<const char*, size_t, CStringLess> type_cache_; // type_desc -> definition_offset.
723 // 32-bit references which need to be resolved to a type later.
724 std::multimap<const char*, size_t, CStringLess> lazy_types_; // type_desc -> patch_offset.
725 };
726
David Srbeckyb851b492015-11-11 20:19:38 +0000727 public:
728 explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
David Srbecky626a1662015-04-12 13:12:26 +0100729 }
730
David Srbeckyb851b492015-11-11 20:19:38 +0000731 void Start() {
732 builder_->GetDebugInfo()->Start();
David Srbecky799b8c42015-04-14 01:57:43 +0100733 }
734
David Srbecky04b05262015-11-09 18:05:48 +0000735 void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
736 CompilationUnitWriter writer(this);
737 writer.Write(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +0000738 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100739
David Srbeckyb851b492015-11-11 20:19:38 +0000740 void End() {
741 builder_->GetDebugInfo()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +0000742 builder_->WritePatches(".debug_info.oat_patches",
743 ArrayRef<const uintptr_t>(debug_info_patches_));
David Srbecky04b05262015-11-09 18:05:48 +0000744 builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data());
745 builder_->WriteSection(".debug_str", &debug_str_.Data());
David Srbecky0fd295f2015-11-16 16:39:10 +0000746 builder_->WriteSection(".debug_loc", &debug_loc_);
David Srbeckyb851b492015-11-11 20:19:38 +0000747 }
748
749 private:
David Srbecky04b05262015-11-09 18:05:48 +0000750 size_t WriteString(const char* str) {
751 return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
752 }
753
David Srbeckyb851b492015-11-11 20:19:38 +0000754 ElfBuilder<ElfTypes>* builder_;
755 std::vector<uintptr_t> debug_info_patches_;
David Srbecky04b05262015-11-09 18:05:48 +0000756 DedupVector debug_abbrev_;
757 DedupVector debug_str_;
David Srbecky0fd295f2015-11-16 16:39:10 +0000758 std::vector<uint8_t> debug_loc_;
David Srbecky04b05262015-11-09 18:05:48 +0000759
760 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
David Srbeckyb851b492015-11-11 20:19:38 +0000761};
762
763template<typename ElfTypes>
764class DebugLineWriter {
765 typedef typename ElfTypes::Addr Elf_Addr;
766
767 public:
768 explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
769 }
770
771 void Start() {
772 builder_->GetDebugLine()->Start();
773 }
774
775 // Write line table for given set of methods.
776 // Returns the number of bytes written.
David Srbecky04b05262015-11-09 18:05:48 +0000777 size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
David Srbeckyb851b492015-11-11 20:19:38 +0000778 const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
779 const Elf_Addr text_address = builder_->GetText()->GetAddress();
David Srbecky04b05262015-11-09 18:05:48 +0000780
781 compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
David Srbeckyb851b492015-11-11 20:19:38 +0000782
David Srbecky799b8c42015-04-14 01:57:43 +0100783 std::vector<FileEntry> files;
784 std::unordered_map<std::string, size_t> files_map;
785 std::vector<std::string> directories;
786 std::unordered_map<std::string, size_t> directories_map;
787 int code_factor_bits_ = 0;
788 int dwarf_isa = -1;
David Srbeckyb851b492015-11-11 20:19:38 +0000789 switch (builder_->GetIsa()) {
David Srbecky799b8c42015-04-14 01:57:43 +0100790 case kArm: // arm actually means thumb2.
791 case kThumb2:
792 code_factor_bits_ = 1; // 16-bit instuctions
793 dwarf_isa = 1; // DW_ISA_ARM_thumb.
794 break;
795 case kArm64:
796 case kMips:
797 case kMips64:
798 code_factor_bits_ = 2; // 32-bit instructions
799 break;
800 case kNone:
801 case kX86:
802 case kX86_64:
803 break;
804 }
David Srbecky297ed222015-04-15 01:18:12 +0100805 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
David Srbecky04b05262015-11-09 18:05:48 +0000806 opcodes.SetAddress(text_address + compilation_unit.low_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100807 if (dwarf_isa != -1) {
808 opcodes.SetISA(dwarf_isa);
809 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000810 for (const MethodDebugInfo* mi : compilation_unit.methods_) {
David Srbecky04b05262015-11-09 18:05:48 +0000811 // Ignore function if we have already generated line table for the same address.
812 // It would confuse the debugger and the DWARF specification forbids it.
813 if (mi->deduped_) {
814 continue;
815 }
816
David Srbecky799b8c42015-04-14 01:57:43 +0100817 struct DebugInfoCallbacks {
818 static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
819 auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx);
820 context->dex2line_.push_back({address, static_cast<int32_t>(line)});
821 return false;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100822 }
David Srbecky799b8c42015-04-14 01:57:43 +0100823 DefaultSrcMap dex2line_;
824 } debug_info_callbacks;
825
David Srbecky6d8c8f02015-10-26 10:57:09 +0000826 Elf_Addr method_address = text_address + mi->low_pc_;
827
David Srbecky799b8c42015-04-14 01:57:43 +0100828 const DexFile* dex = mi->dex_file_;
829 if (mi->code_item_ != nullptr) {
830 dex->DecodeDebugInfo(mi->code_item_,
831 (mi->access_flags_ & kAccStatic) != 0,
832 mi->dex_method_index_,
833 DebugInfoCallbacks::NewPosition,
834 nullptr,
835 &debug_info_callbacks);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100836 }
837
David Srbecky799b8c42015-04-14 01:57:43 +0100838 // Get and deduplicate directory and filename.
839 int file_index = 0; // 0 - primary source file of the compilation.
840 auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
841 const char* source_file = dex->GetSourceFile(dex_class_def);
842 if (source_file != nullptr) {
843 std::string file_name(source_file);
844 size_t file_name_slash = file_name.find_last_of('/');
845 std::string class_name(dex->GetClassDescriptor(dex_class_def));
846 size_t class_name_slash = class_name.find_last_of('/');
847 std::string full_path(file_name);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100848
David Srbecky799b8c42015-04-14 01:57:43 +0100849 // Guess directory from package name.
850 int directory_index = 0; // 0 - current directory of the compilation.
851 if (file_name_slash == std::string::npos && // Just filename.
852 class_name.front() == 'L' && // Type descriptor for a class.
853 class_name_slash != std::string::npos) { // Has package name.
854 std::string package_name = class_name.substr(1, class_name_slash - 1);
855 auto it = directories_map.find(package_name);
856 if (it == directories_map.end()) {
857 directory_index = 1 + directories.size();
858 directories_map.emplace(package_name, directory_index);
859 directories.push_back(package_name);
860 } else {
861 directory_index = it->second;
862 }
863 full_path = package_name + "/" + file_name;
864 }
865
866 // Add file entry.
867 auto it2 = files_map.find(full_path);
868 if (it2 == files_map.end()) {
869 file_index = 1 + files.size();
870 files_map.emplace(full_path, file_index);
871 files.push_back(FileEntry {
872 file_name,
873 directory_index,
874 0, // Modification time - NA.
875 0, // File size - NA.
876 });
877 } else {
878 file_index = it2->second;
879 }
880 }
881 opcodes.SetFile(file_index);
882
883 // Generate mapping opcodes from PC to Java lines.
884 const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
David Srbecky799b8c42015-04-14 01:57:43 +0100885 if (file_index != 0 && !dex2line_map.empty()) {
886 bool first = true;
887 for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) {
888 uint32_t pc = pc2dex.from_;
889 int dex_pc = pc2dex.to_;
890 auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
891 if (dex2line.first) {
892 int line = dex2line.second;
893 if (first) {
894 first = false;
895 if (pc > 0) {
896 // Assume that any preceding code is prologue.
897 int first_line = dex2line_map.front().to_;
898 // Prologue is not a sensible place for a breakpoint.
899 opcodes.NegateStmt();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000900 opcodes.AddRow(method_address, first_line);
David Srbecky799b8c42015-04-14 01:57:43 +0100901 opcodes.NegateStmt();
902 opcodes.SetPrologueEnd();
903 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000904 opcodes.AddRow(method_address + pc, line);
David Srbecky799b8c42015-04-14 01:57:43 +0100905 } else if (line != opcodes.CurrentLine()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000906 opcodes.AddRow(method_address + pc, line);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100907 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100908 }
909 }
David Srbecky799b8c42015-04-14 01:57:43 +0100910 } else {
911 // line 0 - instruction cannot be attributed to any source line.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000912 opcodes.AddRow(method_address, 0);
David Srbecky3b9d57a2015-04-10 00:22:14 +0100913 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100914 }
David Srbecky04b05262015-11-09 18:05:48 +0000915 opcodes.AdvancePC(text_address + compilation_unit.high_pc_);
David Srbecky799b8c42015-04-14 01:57:43 +0100916 opcodes.EndSequence();
David Srbeckyb851b492015-11-11 20:19:38 +0000917 std::vector<uint8_t> buffer;
918 buffer.reserve(opcodes.data()->size() + KB);
919 size_t offset = builder_->GetDebugLine()->GetSize();
920 WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
921 builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
922 return buffer.size();
David Srbecky3b9d57a2015-04-10 00:22:14 +0100923 }
David Srbeckyb851b492015-11-11 20:19:38 +0000924
925 void End() {
926 builder_->GetDebugLine()->End();
Vladimir Marko10c13562015-11-25 14:33:36 +0000927 builder_->WritePatches(".debug_line.oat_patches",
928 ArrayRef<const uintptr_t>(debug_line_patches));
David Srbeckyb851b492015-11-11 20:19:38 +0000929 }
930
931 private:
932 ElfBuilder<ElfTypes>* builder_;
933 std::vector<uintptr_t> debug_line_patches;
934};
935
936template<typename ElfTypes>
937void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000938 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyb851b492015-11-11 20:19:38 +0000939 // Group the methods into compilation units based on source file.
940 std::vector<CompilationUnit> compilation_units;
941 const char* last_source_file = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +0000942 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky04b05262015-11-09 18:05:48 +0000943 auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
944 const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
945 if (compilation_units.empty() || source_file != last_source_file) {
946 compilation_units.push_back(CompilationUnit());
David Srbeckyb851b492015-11-11 20:19:38 +0000947 }
David Srbecky04b05262015-11-09 18:05:48 +0000948 CompilationUnit& cu = compilation_units.back();
949 cu.methods_.push_back(&mi);
950 cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
951 cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
952 last_source_file = source_file;
David Srbeckyb851b492015-11-11 20:19:38 +0000953 }
954
955 // Write .debug_line section.
956 {
957 DebugLineWriter<ElfTypes> line_writer(builder);
958 line_writer.Start();
David Srbeckyb851b492015-11-11 20:19:38 +0000959 for (auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +0000960 line_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +0000961 }
962 line_writer.End();
963 }
964
965 // Write .debug_info section.
966 {
967 DebugInfoWriter<ElfTypes> info_writer(builder);
968 info_writer.Start();
969 for (const auto& compilation_unit : compilation_units) {
David Srbecky04b05262015-11-09 18:05:48 +0000970 info_writer.WriteCompilationUnit(compilation_unit);
David Srbeckyb851b492015-11-11 20:19:38 +0000971 }
972 info_writer.End();
973 }
David Srbecky3b9d57a2015-04-10 00:22:14 +0100974}
975
David Srbecky6d8c8f02015-10-26 10:57:09 +0000976// Explicit instantiations
977template void WriteCFISection<ElfTypes32>(
978 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000979 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000980 CFIFormat format);
981template void WriteCFISection<ElfTypes64>(
982 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000983 const ArrayRef<const MethodDebugInfo>& method_infos,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000984 CFIFormat format);
985template void WriteDebugSections<ElfTypes32>(
986 ElfBuilder<ElfTypes32>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000987 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000988template void WriteDebugSections<ElfTypes64>(
989 ElfBuilder<ElfTypes64>* builder,
Vladimir Marko10c13562015-11-25 14:33:36 +0000990 const ArrayRef<const MethodDebugInfo>& method_infos);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000991
David Srbecky3b9d57a2015-04-10 00:22:14 +0100992} // namespace dwarf
993} // namespace art