David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1 | /* |
| 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 Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 19 | #include <unordered_set> |
| 20 | |
Andreas Gampe | e3d623e | 2015-05-01 16:11:04 -0700 | [diff] [blame] | 21 | #include "base/casts.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 22 | #include "base/stl_util.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 23 | #include "compiled_method.h" |
| 24 | #include "driver/compiler_driver.h" |
| 25 | #include "dex_file-inl.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 26 | #include "dwarf/dedup_vector.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 27 | #include "dwarf/headers.h" |
| 28 | #include "dwarf/register.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 29 | #include "elf_builder.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 30 | #include "oat_writer.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | #include "utils.h" |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 32 | #include "stack_map.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace dwarf { |
| 36 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 37 | static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) { |
| 38 | switch (isa) { |
| 39 | case kArm: |
| 40 | case kThumb2: |
| 41 | return Reg::ArmCore(machine_reg); |
| 42 | case kArm64: |
| 43 | return Reg::Arm64Core(machine_reg); |
| 44 | case kX86: |
| 45 | return Reg::X86Core(machine_reg); |
| 46 | case kX86_64: |
| 47 | return Reg::X86_64Core(machine_reg); |
| 48 | case kMips: |
| 49 | return Reg::MipsCore(machine_reg); |
| 50 | case kMips64: |
| 51 | return Reg::Mips64Core(machine_reg); |
| 52 | default: |
| 53 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 54 | UNREACHABLE(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) { |
| 59 | switch (isa) { |
| 60 | case kArm: |
| 61 | case kThumb2: |
| 62 | return Reg::ArmFp(machine_reg); |
| 63 | case kArm64: |
| 64 | return Reg::Arm64Fp(machine_reg); |
| 65 | case kX86: |
| 66 | return Reg::X86Fp(machine_reg); |
| 67 | case kX86_64: |
| 68 | return Reg::X86_64Fp(machine_reg); |
| 69 | default: |
| 70 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 71 | UNREACHABLE(); |
| 72 | } |
| 73 | } |
| 74 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 75 | static void WriteCIE(InstructionSet isa, |
| 76 | CFIFormat format, |
| 77 | std::vector<uint8_t>* buffer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 78 | // Scratch registers should be marked as undefined. This tells the |
| 79 | // debugger that its value in the previous frame is not recoverable. |
| 80 | bool is64bit = Is64BitInstructionSet(isa); |
| 81 | switch (isa) { |
| 82 | case kArm: |
| 83 | case kThumb2: { |
| 84 | DebugFrameOpCodeWriter<> opcodes; |
| 85 | opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP). |
| 86 | // core registers. |
| 87 | for (int reg = 0; reg < 13; reg++) { |
| 88 | if (reg < 4 || reg == 12) { |
| 89 | opcodes.Undefined(Reg::ArmCore(reg)); |
| 90 | } else { |
| 91 | opcodes.SameValue(Reg::ArmCore(reg)); |
| 92 | } |
| 93 | } |
| 94 | // fp registers. |
| 95 | for (int reg = 0; reg < 32; reg++) { |
| 96 | if (reg < 16) { |
| 97 | opcodes.Undefined(Reg::ArmFp(reg)); |
| 98 | } else { |
| 99 | opcodes.SameValue(Reg::ArmFp(reg)); |
| 100 | } |
| 101 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 102 | auto return_reg = Reg::ArmCore(14); // R14(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 103 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | case kArm64: { |
| 107 | DebugFrameOpCodeWriter<> opcodes; |
| 108 | opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP). |
| 109 | // core registers. |
| 110 | for (int reg = 0; reg < 30; reg++) { |
| 111 | if (reg < 8 || reg == 16 || reg == 17) { |
| 112 | opcodes.Undefined(Reg::Arm64Core(reg)); |
| 113 | } else { |
| 114 | opcodes.SameValue(Reg::Arm64Core(reg)); |
| 115 | } |
| 116 | } |
| 117 | // fp registers. |
| 118 | for (int reg = 0; reg < 32; reg++) { |
| 119 | if (reg < 8 || reg >= 16) { |
| 120 | opcodes.Undefined(Reg::Arm64Fp(reg)); |
| 121 | } else { |
| 122 | opcodes.SameValue(Reg::Arm64Fp(reg)); |
| 123 | } |
| 124 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 125 | auto return_reg = Reg::Arm64Core(30); // R30(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 126 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | case kMips: |
| 130 | case kMips64: { |
| 131 | DebugFrameOpCodeWriter<> opcodes; |
| 132 | opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP). |
| 133 | // core registers. |
| 134 | for (int reg = 1; reg < 26; reg++) { |
| 135 | if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*. |
| 136 | opcodes.Undefined(Reg::MipsCore(reg)); |
| 137 | } else { |
| 138 | opcodes.SameValue(Reg::MipsCore(reg)); |
| 139 | } |
| 140 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 141 | auto return_reg = Reg::MipsCore(31); // R31(RA). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 142 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | case kX86: { |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 146 | // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296 |
| 147 | constexpr bool generate_opcodes_for_x86_fp = false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 148 | DebugFrameOpCodeWriter<> opcodes; |
| 149 | opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP). |
| 150 | opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP). |
| 151 | // core registers. |
| 152 | for (int reg = 0; reg < 8; reg++) { |
| 153 | if (reg <= 3) { |
| 154 | opcodes.Undefined(Reg::X86Core(reg)); |
| 155 | } else if (reg == 4) { |
| 156 | // Stack pointer. |
| 157 | } else { |
| 158 | opcodes.SameValue(Reg::X86Core(reg)); |
| 159 | } |
| 160 | } |
| 161 | // fp registers. |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 162 | if (generate_opcodes_for_x86_fp) { |
| 163 | for (int reg = 0; reg < 8; reg++) { |
| 164 | opcodes.Undefined(Reg::X86Fp(reg)); |
| 165 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 166 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 167 | auto return_reg = Reg::X86Core(8); // R8(EIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 168 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | case kX86_64: { |
| 172 | DebugFrameOpCodeWriter<> opcodes; |
| 173 | opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP). |
| 174 | opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP). |
| 175 | // core registers. |
| 176 | for (int reg = 0; reg < 16; reg++) { |
| 177 | if (reg == 4) { |
| 178 | // Stack pointer. |
| 179 | } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP. |
| 180 | opcodes.Undefined(Reg::X86_64Core(reg)); |
| 181 | } else { |
| 182 | opcodes.SameValue(Reg::X86_64Core(reg)); |
| 183 | } |
| 184 | } |
| 185 | // fp registers. |
| 186 | for (int reg = 0; reg < 16; reg++) { |
| 187 | if (reg < 12) { |
| 188 | opcodes.Undefined(Reg::X86_64Fp(reg)); |
| 189 | } else { |
| 190 | opcodes.SameValue(Reg::X86_64Fp(reg)); |
| 191 | } |
| 192 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 193 | auto return_reg = Reg::X86_64Core(16); // R16(RIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 194 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | case kNone: |
| 198 | break; |
| 199 | } |
| 200 | LOG(FATAL) << "Can not write CIE frame for ISA " << isa; |
| 201 | UNREACHABLE(); |
| 202 | } |
| 203 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 204 | template<typename ElfTypes> |
| 205 | void WriteCFISection(ElfBuilder<ElfTypes>* builder, |
| 206 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 207 | CFIFormat format) { |
| 208 | CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT || |
| 209 | format == dwarf::DW_EH_FRAME_FORMAT); |
| 210 | typedef typename ElfTypes::Addr Elf_Addr; |
| 211 | |
| 212 | std::vector<uint32_t> binary_search_table; |
| 213 | std::vector<uintptr_t> patch_locations; |
| 214 | if (format == DW_EH_FRAME_FORMAT) { |
| 215 | binary_search_table.reserve(2 * method_infos.size()); |
| 216 | } else { |
| 217 | patch_locations.reserve(method_infos.size()); |
| 218 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 219 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 220 | // Write .eh_frame/.debug_frame section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 221 | auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT |
| 222 | ? builder->GetDebugFrame() |
| 223 | : builder->GetEhFrame()); |
| 224 | { |
| 225 | cfi_section->Start(); |
| 226 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
| 227 | const Elf_Addr text_address = builder->GetText()->GetAddress(); |
| 228 | const Elf_Addr cfi_address = cfi_section->GetAddress(); |
| 229 | const Elf_Addr cie_address = cfi_address; |
| 230 | Elf_Addr buffer_address = cfi_address; |
| 231 | std::vector<uint8_t> buffer; // Small temporary buffer. |
| 232 | WriteCIE(builder->GetIsa(), format, &buffer); |
| 233 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 234 | buffer_address += buffer.size(); |
| 235 | buffer.clear(); |
| 236 | for (const OatWriter::DebugInfo& mi : method_infos) { |
| 237 | if (!mi.deduped_) { // Only one FDE per unique address. |
| 238 | ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo(); |
| 239 | if (!opcodes.empty()) { |
| 240 | const Elf_Addr code_address = text_address + mi.low_pc_; |
| 241 | if (format == DW_EH_FRAME_FORMAT) { |
| 242 | binary_search_table.push_back( |
| 243 | dchecked_integral_cast<uint32_t>(code_address)); |
| 244 | binary_search_table.push_back( |
| 245 | dchecked_integral_cast<uint32_t>(buffer_address)); |
| 246 | } |
| 247 | WriteFDE(is64bit, cfi_address, cie_address, |
| 248 | code_address, mi.high_pc_ - mi.low_pc_, |
| 249 | opcodes, format, buffer_address, &buffer, |
| 250 | &patch_locations); |
| 251 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 252 | buffer_address += buffer.size(); |
| 253 | buffer.clear(); |
| 254 | } |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 255 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 256 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 257 | cfi_section->End(); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 258 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 259 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 260 | if (format == DW_EH_FRAME_FORMAT) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 261 | auto* header_section = builder->GetEhFrameHdr(); |
| 262 | header_section->Start(); |
| 263 | uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 264 | // Write .eh_frame_hdr section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 265 | std::vector<uint8_t> buffer; |
| 266 | Writer<> header(&buffer); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 267 | header.PushUint8(1); // Version. |
| 268 | // Encoding of .eh_frame pointer - libunwind does not honor datarel here, |
| 269 | // so we have to use pcrel which means relative to the pointer's location. |
| 270 | header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
| 271 | // Encoding of binary search table size. |
| 272 | header.PushUint8(DW_EH_PE_udata4); |
| 273 | // Encoding of binary search table addresses - libunwind supports only this |
| 274 | // specific combination, which means relative to the start of .eh_frame_hdr. |
| 275 | header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 276 | // .eh_frame pointer |
| 277 | header.PushInt32(cfi_section->GetAddress() - (header_address + 4u)); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 278 | // Binary search table size (number of entries). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 279 | header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2)); |
| 280 | header_section->WriteFully(buffer.data(), buffer.size()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 281 | // Binary search table. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 282 | for (size_t i = 0; i < binary_search_table.size(); i++) { |
| 283 | // Make addresses section-relative since we know the header address now. |
| 284 | binary_search_table[i] -= header_address; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 285 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 286 | header_section->WriteFully(binary_search_table.data(), binary_search_table.size()); |
| 287 | header_section->End(); |
| 288 | } else { |
| 289 | builder->WritePatches(".debug_frame.oat_patches", &patch_locations); |
David Srbecky | 033d745 | 2015-04-30 19:57:35 +0100 | [diff] [blame] | 290 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 291 | } |
| 292 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 293 | struct CompilationUnit { |
| 294 | std::vector<const OatWriter::DebugInfo*> methods_; |
| 295 | size_t debug_line_offset_ = 0; |
| 296 | uint32_t low_pc_ = 0xFFFFFFFFU; |
| 297 | uint32_t high_pc_ = 0; |
| 298 | }; |
| 299 | |
| 300 | // Helper class to write .debug_info and its supporting sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 301 | template<typename ElfTypes> |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 302 | class DebugInfoWriter { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 303 | typedef typename ElfTypes::Addr Elf_Addr; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 304 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 305 | // Helper class to write one compilation unit. |
| 306 | // It holds helper methods and temporary state. |
| 307 | class CompilationUnitWriter { |
| 308 | public: |
| 309 | explicit CompilationUnitWriter(DebugInfoWriter* owner) |
| 310 | : owner_(owner), |
| 311 | info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) { |
| 312 | } |
| 313 | |
| 314 | void Write(const CompilationUnit& compilation_unit) { |
| 315 | CHECK(!compilation_unit.methods_.empty()); |
| 316 | const Elf_Addr text_address = owner_->builder_->GetText()->GetAddress(); |
| 317 | |
| 318 | info_.StartTag(DW_TAG_compile_unit); |
| 319 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 320 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
| 321 | info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 322 | info_.WriteUdata(DW_AT_high_pc, compilation_unit.high_pc_ - compilation_unit.low_pc_); |
| 323 | info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 324 | |
| 325 | const char* last_dex_class_desc = nullptr; |
| 326 | for (auto mi : compilation_unit.methods_) { |
| 327 | const DexFile* dex = mi->dex_file_; |
| 328 | const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_); |
| 329 | const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method); |
| 330 | const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto); |
| 331 | const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method); |
| 332 | |
| 333 | // Enclose the method in correct class definition. |
| 334 | if (last_dex_class_desc != dex_class_desc) { |
| 335 | if (last_dex_class_desc != nullptr) { |
| 336 | EndClassTag(last_dex_class_desc); |
| 337 | } |
| 338 | size_t offset = StartClassTag(dex_class_desc); |
| 339 | type_cache_.emplace(dex_class_desc, offset); |
| 340 | // Check that each class is defined only once. |
| 341 | bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second; |
| 342 | CHECK(unique) << "Redefinition of " << dex_class_desc; |
| 343 | last_dex_class_desc = dex_class_desc; |
| 344 | } |
| 345 | |
| 346 | std::vector<const char*> param_names; |
| 347 | if (mi->code_item_ != nullptr) { |
| 348 | const uint8_t* stream = dex->GetDebugInfoStream(mi->code_item_); |
| 349 | if (stream != nullptr) { |
| 350 | DecodeUnsignedLeb128(&stream); // line. |
| 351 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 352 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 353 | uint32_t id = DecodeUnsignedLeb128P1(&stream); |
| 354 | param_names.push_back(mi->dex_file_->StringDataByIdx(id)); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | int start_depth = info_.Depth(); |
| 360 | info_.StartTag(DW_TAG_subprogram); |
| 361 | WriteName(dex->GetMethodName(dex_method)); |
| 362 | info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 363 | info_.WriteUdata(DW_AT_high_pc, mi->high_pc_ - mi->low_pc_); |
| 364 | uint8_t frame_base[] = { DW_OP_call_frame_cfa }; |
| 365 | info_.WriteExprLoc(DW_AT_frame_base, &frame_base, sizeof(frame_base)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 366 | WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto)); |
| 367 | if (dex_params != nullptr) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 368 | uint32_t vreg = mi->code_item_ == nullptr ? 0 : |
| 369 | mi->code_item_->registers_size_ - mi->code_item_->ins_size_; |
| 370 | if ((mi->access_flags_ & kAccStatic) == 0) { |
| 371 | info_.StartTag(DW_TAG_formal_parameter); |
| 372 | WriteName("this"); |
| 373 | info_.WriteFlag(DW_AT_artificial, true); |
| 374 | WriteLazyType(dex_class_desc); |
| 375 | const bool is64bitValue = false; |
| 376 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 377 | vreg++; |
| 378 | info_.EndTag(); |
| 379 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 380 | for (uint32_t i = 0; i < dex_params->Size(); ++i) { |
| 381 | info_.StartTag(DW_TAG_formal_parameter); |
| 382 | // Parameter names may not be always available. |
| 383 | if (i < param_names.size() && param_names[i] != nullptr) { |
| 384 | WriteName(param_names[i]); |
| 385 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 386 | // Write the type. |
| 387 | const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_); |
| 388 | WriteLazyType(type_desc); |
| 389 | // Write the stack location of the parameter. |
| 390 | const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J'; |
| 391 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 392 | vreg += is64bitValue ? 2 : 1; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 393 | info_.EndTag(); |
| 394 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 395 | if (mi->code_item_ != nullptr) { |
| 396 | CHECK_EQ(vreg, mi->code_item_->registers_size_); |
| 397 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 398 | } |
| 399 | info_.EndTag(); |
| 400 | CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end. |
| 401 | } |
| 402 | if (last_dex_class_desc != nullptr) { |
| 403 | EndClassTag(last_dex_class_desc); |
| 404 | } |
| 405 | CHECK_EQ(info_.Depth(), 1); |
| 406 | FinishLazyTypes(); |
| 407 | info_.EndTag(); // DW_TAG_compile_unit |
| 408 | std::vector<uint8_t> buffer; |
| 409 | buffer.reserve(info_.data()->size() + KB); |
| 410 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
| 411 | const size_t debug_abbrev_offset = |
| 412 | owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size()); |
| 413 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 414 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 415 | } |
| 416 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 417 | // Write table into .debug_loc which describes location of dex register. |
| 418 | // The dex register might be valid only at some points and it might |
| 419 | // move between machine registers and stack. |
| 420 | void WriteRegLocation(const OatWriter::DebugInfo* method_info, uint16_t vreg, |
| 421 | bool is64bitValue, uint32_t compilation_unit_low_pc) { |
| 422 | using Kind = DexRegisterLocation::Kind; |
| 423 | bool is_optimizing = method_info->compiled_method_->GetQuickCode().size() > 0 && |
| 424 | method_info->compiled_method_->GetVmapTable().size() > 0 && |
| 425 | method_info->compiled_method_->GetGcMap().size() == 0 && |
| 426 | method_info->code_item_ != nullptr; |
| 427 | if (!is_optimizing) { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | Writer<> writer(&owner_->debug_loc_); |
| 432 | info_.WriteSecOffset(DW_AT_location, writer.size()); |
| 433 | |
| 434 | const InstructionSet isa = owner_->builder_->GetIsa(); |
| 435 | const bool is64bit = Is64BitInstructionSet(isa); |
| 436 | const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data()); |
| 437 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 438 | DexRegisterLocation last_reg_lo = DexRegisterLocation::None(); |
| 439 | DexRegisterLocation last_reg_hi = DexRegisterLocation::None(); |
| 440 | size_t offset_of_last_end_address = 0; |
| 441 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 442 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 443 | DCHECK(stack_map.IsValid()); |
| 444 | |
| 445 | // Find the location of the dex register. |
| 446 | DexRegisterLocation reg_lo = DexRegisterLocation::None(); |
| 447 | DexRegisterLocation reg_hi = DexRegisterLocation::None(); |
| 448 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 449 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 450 | stack_map, encoding, method_info->code_item_->registers_size_); |
| 451 | reg_lo = dex_register_map.GetDexRegisterLocation( |
| 452 | vreg, method_info->code_item_->registers_size_, code_info, encoding); |
| 453 | if (is64bitValue) { |
| 454 | reg_hi = dex_register_map.GetDexRegisterLocation( |
| 455 | vreg + 1, method_info->code_item_->registers_size_, code_info, encoding); |
| 456 | } |
| 457 | } |
| 458 | if ((reg_lo == last_reg_lo && reg_hi == last_reg_hi) || |
| 459 | reg_lo.GetKind() == Kind::kNone) { |
| 460 | // Skip identical or undefined locations. |
| 461 | continue; |
| 462 | } |
| 463 | last_reg_lo = reg_lo; |
| 464 | last_reg_hi = reg_hi; |
| 465 | |
| 466 | // Translate dex register location to DWARF expression. |
| 467 | // Note that 64-bit value might be split to two distinct locations. |
| 468 | // (for example, two 32-bit machine registers, or even stack and register) |
| 469 | uint8_t buffer[64]; |
| 470 | uint8_t* pos = buffer; |
| 471 | for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) { |
| 472 | DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi); |
| 473 | const Kind kind = reg_loc.GetKind(); |
| 474 | const int32_t value = reg_loc.GetValue(); |
| 475 | if (kind == Kind::kInStack) { |
| 476 | const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes(); |
| 477 | *(pos++) = DW_OP_fbreg; |
| 478 | // The stack offset is relative to SP. Make it relative to CFA. |
| 479 | pos = EncodeSignedLeb128(pos, value - frame_size); |
| 480 | if (piece == 0 && reg_hi.GetKind() == Kind::kInStack && |
| 481 | reg_hi.GetValue() == value + 4) { |
| 482 | break; // the high word is correctly implied by the low word. |
| 483 | } |
| 484 | } else if (kind == Kind::kInRegister) { |
| 485 | pos = WriteOpReg(pos, GetDwarfCoreReg(isa, value).num()); |
| 486 | if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh && |
| 487 | reg_hi.GetValue() == value) { |
| 488 | break; // the high word is correctly implied by the low word. |
| 489 | } |
| 490 | } else if (kind == Kind::kInFpuRegister) { |
| 491 | if ((isa == kArm || isa == kThumb2) && |
| 492 | piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister && |
| 493 | reg_hi.GetValue() == value + 1 && value % 2 == 0) { |
| 494 | // Translate S register pair to D register (e.g. S4+S5 to D2). |
| 495 | pos = WriteOpReg(pos, Reg::ArmDp(value / 2).num()); |
| 496 | break; |
| 497 | } |
David Srbecky | 3dd7e5a | 2015-11-27 13:31:16 +0000 | [diff] [blame] | 498 | if (isa == kMips || isa == kMips64) { |
| 499 | // TODO: Find what the DWARF floating point register numbers are on MIPS. |
| 500 | break; |
| 501 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 502 | pos = WriteOpReg(pos, GetDwarfFpReg(isa, value).num()); |
| 503 | if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh && |
| 504 | reg_hi.GetValue() == reg_lo.GetValue()) { |
| 505 | break; // the high word is correctly implied by the low word. |
| 506 | } |
| 507 | } else if (kind == Kind::kConstant) { |
| 508 | *(pos++) = DW_OP_consts; |
| 509 | pos = EncodeSignedLeb128(pos, value); |
| 510 | *(pos++) = DW_OP_stack_value; |
| 511 | } else if (kind == Kind::kNone) { |
| 512 | break; |
| 513 | } else { |
| 514 | // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind(). |
| 515 | // kInRegisterHigh and kInFpuRegisterHigh should be handled by |
| 516 | // the special cases above and they should not occur alone. |
| 517 | LOG(ERROR) << "Unexpected register location kind: " |
| 518 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 519 | break; |
| 520 | } |
| 521 | if (is64bitValue) { |
| 522 | // Write the marker which is needed by split 64-bit values. |
| 523 | // This code is skipped by the special cases. |
| 524 | *(pos++) = DW_OP_piece; |
| 525 | pos = EncodeUnsignedLeb128(pos, 4); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Write end address for previous entry. |
| 530 | const uint32_t pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding); |
| 531 | if (offset_of_last_end_address != 0) { |
| 532 | if (is64bit) { |
| 533 | writer.UpdateUint64(offset_of_last_end_address, pc - compilation_unit_low_pc); |
| 534 | } else { |
| 535 | writer.UpdateUint32(offset_of_last_end_address, pc - compilation_unit_low_pc); |
| 536 | } |
| 537 | } |
| 538 | offset_of_last_end_address = 0; |
| 539 | |
| 540 | DCHECK_LE(static_cast<size_t>(pos - buffer), sizeof(buffer)); |
| 541 | if (pos > buffer) { |
| 542 | // Write start/end address. |
| 543 | if (is64bit) { |
| 544 | writer.PushUint64(pc - compilation_unit_low_pc); |
| 545 | offset_of_last_end_address = writer.size(); |
| 546 | writer.PushUint64(method_info->high_pc_ - compilation_unit_low_pc); |
| 547 | } else { |
| 548 | writer.PushUint32(pc - compilation_unit_low_pc); |
| 549 | offset_of_last_end_address = writer.size(); |
| 550 | writer.PushUint32(method_info->high_pc_ - compilation_unit_low_pc); |
| 551 | } |
| 552 | // Write the expression. |
| 553 | writer.PushUint16(pos - buffer); |
| 554 | writer.PushData(buffer, pos - buffer); |
| 555 | } else { |
| 556 | // Otherwise leave the address range undefined. |
| 557 | } |
| 558 | } |
| 559 | // Write end-of-list entry. |
| 560 | if (is64bit) { |
| 561 | writer.PushUint64(0); |
| 562 | writer.PushUint64(0); |
| 563 | } else { |
| 564 | writer.PushUint32(0); |
| 565 | writer.PushUint32(0); |
| 566 | } |
| 567 | } |
| 568 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 569 | // Some types are difficult to define as we go since they need |
| 570 | // to be enclosed in the right set of namespaces. Therefore we |
| 571 | // just define all types lazily at the end of compilation unit. |
| 572 | void WriteLazyType(const char* type_descriptor) { |
| 573 | DCHECK(type_descriptor != nullptr); |
| 574 | if (type_descriptor[0] != 'V') { |
| 575 | lazy_types_.emplace(type_descriptor, info_.size()); |
| 576 | info_.WriteRef4(DW_AT_type, 0); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | void FinishLazyTypes() { |
| 581 | for (const auto& lazy_type : lazy_types_) { |
| 582 | info_.UpdateUint32(lazy_type.second, WriteType(lazy_type.first)); |
| 583 | } |
| 584 | lazy_types_.clear(); |
| 585 | } |
| 586 | |
| 587 | private: |
| 588 | void WriteName(const char* name) { |
| 589 | info_.WriteStrp(DW_AT_name, owner_->WriteString(name)); |
| 590 | } |
| 591 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 592 | // Helper which writes DWARF expression referencing a register. |
| 593 | static uint8_t* WriteOpReg(uint8_t* buffer, uint32_t dwarf_reg_num) { |
| 594 | if (dwarf_reg_num < 32) { |
| 595 | *(buffer++) = DW_OP_reg0 + dwarf_reg_num; |
| 596 | } else { |
| 597 | *(buffer++) = DW_OP_regx; |
| 598 | buffer = EncodeUnsignedLeb128(buffer, dwarf_reg_num); |
| 599 | } |
| 600 | return buffer; |
| 601 | } |
| 602 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 603 | // Convert dex type descriptor to DWARF. |
| 604 | // Returns offset in the compilation unit. |
| 605 | size_t WriteType(const char* desc) { |
| 606 | const auto& it = type_cache_.find(desc); |
| 607 | if (it != type_cache_.end()) { |
| 608 | return it->second; |
| 609 | } |
| 610 | |
| 611 | size_t offset; |
| 612 | if (*desc == 'L') { |
| 613 | // Class type. For example: Lpackage/name; |
| 614 | offset = StartClassTag(desc); |
| 615 | info_.WriteFlag(DW_AT_declaration, true); |
| 616 | EndClassTag(desc); |
| 617 | } else if (*desc == '[') { |
| 618 | // Array type. |
| 619 | size_t element_type = WriteType(desc + 1); |
| 620 | offset = info_.StartTag(DW_TAG_array_type); |
| 621 | info_.WriteRef(DW_AT_type, element_type); |
| 622 | info_.EndTag(); |
| 623 | } else { |
| 624 | // Primitive types. |
| 625 | const char* name; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 626 | uint32_t encoding; |
| 627 | uint32_t byte_size; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 628 | switch (*desc) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 629 | case 'B': |
| 630 | name = "byte"; |
| 631 | encoding = DW_ATE_signed; |
| 632 | byte_size = 1; |
| 633 | break; |
| 634 | case 'C': |
| 635 | name = "char"; |
| 636 | encoding = DW_ATE_UTF; |
| 637 | byte_size = 2; |
| 638 | break; |
| 639 | case 'D': |
| 640 | name = "double"; |
| 641 | encoding = DW_ATE_float; |
| 642 | byte_size = 8; |
| 643 | break; |
| 644 | case 'F': |
| 645 | name = "float"; |
| 646 | encoding = DW_ATE_float; |
| 647 | byte_size = 4; |
| 648 | break; |
| 649 | case 'I': |
| 650 | name = "int"; |
| 651 | encoding = DW_ATE_signed; |
| 652 | byte_size = 4; |
| 653 | break; |
| 654 | case 'J': |
| 655 | name = "long"; |
| 656 | encoding = DW_ATE_signed; |
| 657 | byte_size = 8; |
| 658 | break; |
| 659 | case 'S': |
| 660 | name = "short"; |
| 661 | encoding = DW_ATE_signed; |
| 662 | byte_size = 2; |
| 663 | break; |
| 664 | case 'Z': |
| 665 | name = "boolean"; |
| 666 | encoding = DW_ATE_boolean; |
| 667 | byte_size = 1; |
| 668 | break; |
| 669 | case 'V': |
| 670 | LOG(FATAL) << "Void type should not be encoded"; |
| 671 | UNREACHABLE(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 672 | default: |
| 673 | LOG(FATAL) << "Unknown dex type descriptor: " << desc; |
| 674 | UNREACHABLE(); |
| 675 | } |
| 676 | offset = info_.StartTag(DW_TAG_base_type); |
| 677 | WriteName(name); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 678 | info_.WriteData1(DW_AT_encoding, encoding); |
| 679 | info_.WriteData1(DW_AT_byte_size, byte_size); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 680 | info_.EndTag(); |
| 681 | } |
| 682 | |
| 683 | type_cache_.emplace(desc, offset); |
| 684 | return offset; |
| 685 | } |
| 686 | |
| 687 | // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags. |
| 688 | // Returns offset of the class tag in the compilation unit. |
| 689 | size_t StartClassTag(const char* desc) { |
| 690 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 691 | // Enclose the type in namespace tags. |
| 692 | const char* end; |
| 693 | for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) { |
| 694 | info_.StartTag(DW_TAG_namespace); |
| 695 | WriteName(std::string(desc, end - desc).c_str()); |
| 696 | } |
| 697 | // Start the class tag. |
| 698 | size_t offset = info_.StartTag(DW_TAG_class_type); |
| 699 | end = strchr(desc, ';'); |
| 700 | CHECK(end != nullptr); |
| 701 | WriteName(std::string(desc, end - desc).c_str()); |
| 702 | return offset; |
| 703 | } |
| 704 | |
| 705 | void EndClassTag(const char* desc) { |
| 706 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 707 | // End the class tag. |
| 708 | info_.EndTag(); |
| 709 | // Close namespace tags. |
| 710 | const char* end; |
| 711 | for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) { |
| 712 | info_.EndTag(); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // For access to the ELF sections. |
| 717 | DebugInfoWriter<ElfTypes>* owner_; |
| 718 | // Debug abbrevs for this compilation unit only. |
| 719 | std::vector<uint8_t> debug_abbrev_; |
| 720 | // Temporary buffer to create and store the entries. |
| 721 | DebugInfoEntryWriter<> info_; |
| 722 | // Cache of already translated type descriptors. |
| 723 | std::map<const char*, size_t, CStringLess> type_cache_; // type_desc -> definition_offset. |
| 724 | // 32-bit references which need to be resolved to a type later. |
| 725 | std::multimap<const char*, size_t, CStringLess> lazy_types_; // type_desc -> patch_offset. |
| 726 | }; |
| 727 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 728 | public: |
| 729 | explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 730 | } |
| 731 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 732 | void Start() { |
| 733 | builder_->GetDebugInfo()->Start(); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 734 | } |
| 735 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 736 | void WriteCompilationUnit(const CompilationUnit& compilation_unit) { |
| 737 | CompilationUnitWriter writer(this); |
| 738 | writer.Write(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 739 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 740 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 741 | void End() { |
| 742 | builder_->GetDebugInfo()->End(); |
| 743 | builder_->WritePatches(".debug_info.oat_patches", &debug_info_patches_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 744 | builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data()); |
| 745 | builder_->WriteSection(".debug_str", &debug_str_.Data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 746 | builder_->WriteSection(".debug_loc", &debug_loc_); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | private: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 750 | size_t WriteString(const char* str) { |
| 751 | return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1); |
| 752 | } |
| 753 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 754 | ElfBuilder<ElfTypes>* builder_; |
| 755 | std::vector<uintptr_t> debug_info_patches_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 756 | DedupVector debug_abbrev_; |
| 757 | DedupVector debug_str_; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 758 | std::vector<uint8_t> debug_loc_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 759 | |
| 760 | std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only. |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | template<typename ElfTypes> |
| 764 | class 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 Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 777 | size_t WriteCompilationUnit(CompilationUnit& compilation_unit) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 778 | const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); |
| 779 | const Elf_Addr text_address = builder_->GetText()->GetAddress(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 780 | |
| 781 | compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 782 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 783 | 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 Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 789 | switch (builder_->GetIsa()) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 790 | 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 Srbecky | 297ed22 | 2015-04-15 01:18:12 +0100 | [diff] [blame] | 805 | DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 806 | opcodes.SetAddress(text_address + compilation_unit.low_pc_); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 807 | if (dwarf_isa != -1) { |
| 808 | opcodes.SetISA(dwarf_isa); |
| 809 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 810 | for (const OatWriter::DebugInfo* mi : compilation_unit.methods_) { |
| 811 | // 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 Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 817 | 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 Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 822 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 823 | DefaultSrcMap dex2line_; |
| 824 | } debug_info_callbacks; |
| 825 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 826 | Elf_Addr method_address = text_address + mi->low_pc_; |
| 827 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 828 | 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 Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 836 | } |
| 837 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 838 | // 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 Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 848 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 849 | // 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 Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 885 | 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 Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 900 | opcodes.AddRow(method_address, first_line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 901 | opcodes.NegateStmt(); |
| 902 | opcodes.SetPrologueEnd(); |
| 903 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 904 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 905 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 906 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 907 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 908 | } |
| 909 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 910 | } else { |
| 911 | // line 0 - instruction cannot be attributed to any source line. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 912 | opcodes.AddRow(method_address, 0); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 913 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 914 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 915 | opcodes.AdvancePC(text_address + compilation_unit.high_pc_); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 916 | opcodes.EndSequence(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 917 | 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 Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 923 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 924 | |
| 925 | void End() { |
| 926 | builder_->GetDebugLine()->End(); |
| 927 | builder_->WritePatches(".debug_line.oat_patches", &debug_line_patches); |
| 928 | } |
| 929 | |
| 930 | private: |
| 931 | ElfBuilder<ElfTypes>* builder_; |
| 932 | std::vector<uintptr_t> debug_line_patches; |
| 933 | }; |
| 934 | |
| 935 | template<typename ElfTypes> |
| 936 | void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
| 937 | const std::vector<OatWriter::DebugInfo>& method_infos) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 938 | // Group the methods into compilation units based on source file. |
| 939 | std::vector<CompilationUnit> compilation_units; |
| 940 | const char* last_source_file = nullptr; |
| 941 | for (const OatWriter::DebugInfo& mi : method_infos) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 942 | auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_); |
| 943 | const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def); |
| 944 | if (compilation_units.empty() || source_file != last_source_file) { |
| 945 | compilation_units.push_back(CompilationUnit()); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 946 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 947 | CompilationUnit& cu = compilation_units.back(); |
| 948 | cu.methods_.push_back(&mi); |
| 949 | cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_); |
| 950 | cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_); |
| 951 | last_source_file = source_file; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | // Write .debug_line section. |
| 955 | { |
| 956 | DebugLineWriter<ElfTypes> line_writer(builder); |
| 957 | line_writer.Start(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 958 | for (auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 959 | line_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 960 | } |
| 961 | line_writer.End(); |
| 962 | } |
| 963 | |
| 964 | // Write .debug_info section. |
| 965 | { |
| 966 | DebugInfoWriter<ElfTypes> info_writer(builder); |
| 967 | info_writer.Start(); |
| 968 | for (const auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 969 | info_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 970 | } |
| 971 | info_writer.End(); |
| 972 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 973 | } |
| 974 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 975 | // Explicit instantiations |
| 976 | template void WriteCFISection<ElfTypes32>( |
| 977 | ElfBuilder<ElfTypes32>* builder, |
| 978 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 979 | CFIFormat format); |
| 980 | template void WriteCFISection<ElfTypes64>( |
| 981 | ElfBuilder<ElfTypes64>* builder, |
| 982 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 983 | CFIFormat format); |
| 984 | template void WriteDebugSections<ElfTypes32>( |
| 985 | ElfBuilder<ElfTypes32>* builder, |
| 986 | const std::vector<OatWriter::DebugInfo>& method_infos); |
| 987 | template void WriteDebugSections<ElfTypes64>( |
| 988 | ElfBuilder<ElfTypes64>* builder, |
| 989 | const std::vector<OatWriter::DebugInfo>& method_infos); |
| 990 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 991 | } // namespace dwarf |
| 992 | } // namespace art |