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> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 20 | #include <vector> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 21 | |
Andreas Gampe | e3d623e | 2015-05-01 16:11:04 -0700 | [diff] [blame] | 22 | #include "base/casts.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 23 | #include "base/stl_util.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 24 | #include "compiled_method.h" |
| 25 | #include "driver/compiler_driver.h" |
| 26 | #include "dex_file-inl.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 27 | #include "dwarf/dedup_vector.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 28 | #include "dwarf/headers.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 29 | #include "dwarf/method_debug_info.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 30 | #include "dwarf/register.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 31 | #include "elf_builder.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 32 | #include "oat_writer.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 33 | #include "utils.h" |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 34 | #include "stack_map.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | namespace dwarf { |
| 38 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 39 | // The ARM specification defines three special mapping symbols |
| 40 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 41 | // These symbols can be used by tools, for example, to pretty |
| 42 | // print instructions correctly. Objdump will use them if they |
| 43 | // exist, but it will still work well without them. |
| 44 | // However, these extra symbols take space, so let's just generate |
| 45 | // one symbol which marks the whole .text section as code. |
| 46 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 47 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 48 | static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) { |
| 49 | switch (isa) { |
| 50 | case kArm: |
| 51 | case kThumb2: |
| 52 | return Reg::ArmCore(machine_reg); |
| 53 | case kArm64: |
| 54 | return Reg::Arm64Core(machine_reg); |
| 55 | case kX86: |
| 56 | return Reg::X86Core(machine_reg); |
| 57 | case kX86_64: |
| 58 | return Reg::X86_64Core(machine_reg); |
| 59 | case kMips: |
| 60 | return Reg::MipsCore(machine_reg); |
| 61 | case kMips64: |
| 62 | return Reg::Mips64Core(machine_reg); |
| 63 | default: |
| 64 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 65 | UNREACHABLE(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) { |
| 70 | switch (isa) { |
| 71 | case kArm: |
| 72 | case kThumb2: |
| 73 | return Reg::ArmFp(machine_reg); |
| 74 | case kArm64: |
| 75 | return Reg::Arm64Fp(machine_reg); |
| 76 | case kX86: |
| 77 | return Reg::X86Fp(machine_reg); |
| 78 | case kX86_64: |
| 79 | return Reg::X86_64Fp(machine_reg); |
| 80 | default: |
| 81 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 82 | UNREACHABLE(); |
| 83 | } |
| 84 | } |
| 85 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 86 | static void WriteCIE(InstructionSet isa, |
| 87 | CFIFormat format, |
| 88 | std::vector<uint8_t>* buffer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 89 | // Scratch registers should be marked as undefined. This tells the |
| 90 | // debugger that its value in the previous frame is not recoverable. |
| 91 | bool is64bit = Is64BitInstructionSet(isa); |
| 92 | switch (isa) { |
| 93 | case kArm: |
| 94 | case kThumb2: { |
| 95 | DebugFrameOpCodeWriter<> opcodes; |
| 96 | opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP). |
| 97 | // core registers. |
| 98 | for (int reg = 0; reg < 13; reg++) { |
| 99 | if (reg < 4 || reg == 12) { |
| 100 | opcodes.Undefined(Reg::ArmCore(reg)); |
| 101 | } else { |
| 102 | opcodes.SameValue(Reg::ArmCore(reg)); |
| 103 | } |
| 104 | } |
| 105 | // fp registers. |
| 106 | for (int reg = 0; reg < 32; reg++) { |
| 107 | if (reg < 16) { |
| 108 | opcodes.Undefined(Reg::ArmFp(reg)); |
| 109 | } else { |
| 110 | opcodes.SameValue(Reg::ArmFp(reg)); |
| 111 | } |
| 112 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 113 | auto return_reg = Reg::ArmCore(14); // R14(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 114 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | case kArm64: { |
| 118 | DebugFrameOpCodeWriter<> opcodes; |
| 119 | opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP). |
| 120 | // core registers. |
| 121 | for (int reg = 0; reg < 30; reg++) { |
| 122 | if (reg < 8 || reg == 16 || reg == 17) { |
| 123 | opcodes.Undefined(Reg::Arm64Core(reg)); |
| 124 | } else { |
| 125 | opcodes.SameValue(Reg::Arm64Core(reg)); |
| 126 | } |
| 127 | } |
| 128 | // fp registers. |
| 129 | for (int reg = 0; reg < 32; reg++) { |
| 130 | if (reg < 8 || reg >= 16) { |
| 131 | opcodes.Undefined(Reg::Arm64Fp(reg)); |
| 132 | } else { |
| 133 | opcodes.SameValue(Reg::Arm64Fp(reg)); |
| 134 | } |
| 135 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 136 | auto return_reg = Reg::Arm64Core(30); // R30(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 137 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | case kMips: |
| 141 | case kMips64: { |
| 142 | DebugFrameOpCodeWriter<> opcodes; |
| 143 | opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP). |
| 144 | // core registers. |
| 145 | for (int reg = 1; reg < 26; reg++) { |
| 146 | if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*. |
| 147 | opcodes.Undefined(Reg::MipsCore(reg)); |
| 148 | } else { |
| 149 | opcodes.SameValue(Reg::MipsCore(reg)); |
| 150 | } |
| 151 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 152 | auto return_reg = Reg::MipsCore(31); // R31(RA). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 153 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | case kX86: { |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 157 | // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296 |
| 158 | constexpr bool generate_opcodes_for_x86_fp = false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 159 | DebugFrameOpCodeWriter<> opcodes; |
| 160 | opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP). |
| 161 | opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP). |
| 162 | // core registers. |
| 163 | for (int reg = 0; reg < 8; reg++) { |
| 164 | if (reg <= 3) { |
| 165 | opcodes.Undefined(Reg::X86Core(reg)); |
| 166 | } else if (reg == 4) { |
| 167 | // Stack pointer. |
| 168 | } else { |
| 169 | opcodes.SameValue(Reg::X86Core(reg)); |
| 170 | } |
| 171 | } |
| 172 | // fp registers. |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 173 | if (generate_opcodes_for_x86_fp) { |
| 174 | for (int reg = 0; reg < 8; reg++) { |
| 175 | opcodes.Undefined(Reg::X86Fp(reg)); |
| 176 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 177 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 178 | auto return_reg = Reg::X86Core(8); // R8(EIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 179 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | case kX86_64: { |
| 183 | DebugFrameOpCodeWriter<> opcodes; |
| 184 | opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP). |
| 185 | opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP). |
| 186 | // core registers. |
| 187 | for (int reg = 0; reg < 16; reg++) { |
| 188 | if (reg == 4) { |
| 189 | // Stack pointer. |
| 190 | } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP. |
| 191 | opcodes.Undefined(Reg::X86_64Core(reg)); |
| 192 | } else { |
| 193 | opcodes.SameValue(Reg::X86_64Core(reg)); |
| 194 | } |
| 195 | } |
| 196 | // fp registers. |
| 197 | for (int reg = 0; reg < 16; reg++) { |
| 198 | if (reg < 12) { |
| 199 | opcodes.Undefined(Reg::X86_64Fp(reg)); |
| 200 | } else { |
| 201 | opcodes.SameValue(Reg::X86_64Fp(reg)); |
| 202 | } |
| 203 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 204 | auto return_reg = Reg::X86_64Core(16); // R16(RIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 205 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | case kNone: |
| 209 | break; |
| 210 | } |
| 211 | LOG(FATAL) << "Can not write CIE frame for ISA " << isa; |
| 212 | UNREACHABLE(); |
| 213 | } |
| 214 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 215 | template<typename ElfTypes> |
| 216 | void WriteCFISection(ElfBuilder<ElfTypes>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 217 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 218 | CFIFormat format) { |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 219 | CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 220 | typedef typename ElfTypes::Addr Elf_Addr; |
| 221 | |
| 222 | std::vector<uint32_t> binary_search_table; |
| 223 | std::vector<uintptr_t> patch_locations; |
| 224 | if (format == DW_EH_FRAME_FORMAT) { |
| 225 | binary_search_table.reserve(2 * method_infos.size()); |
| 226 | } else { |
| 227 | patch_locations.reserve(method_infos.size()); |
| 228 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 229 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 230 | // Write .eh_frame/.debug_frame section. |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 231 | auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 232 | ? builder->GetDebugFrame() |
| 233 | : builder->GetEhFrame()); |
| 234 | { |
| 235 | cfi_section->Start(); |
| 236 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
| 237 | const Elf_Addr text_address = builder->GetText()->GetAddress(); |
| 238 | const Elf_Addr cfi_address = cfi_section->GetAddress(); |
| 239 | const Elf_Addr cie_address = cfi_address; |
| 240 | Elf_Addr buffer_address = cfi_address; |
| 241 | std::vector<uint8_t> buffer; // Small temporary buffer. |
| 242 | WriteCIE(builder->GetIsa(), format, &buffer); |
| 243 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 244 | buffer_address += buffer.size(); |
| 245 | buffer.clear(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 246 | for (const MethodDebugInfo& mi : method_infos) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 247 | if (!mi.deduped_) { // Only one FDE per unique address. |
| 248 | ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo(); |
| 249 | if (!opcodes.empty()) { |
| 250 | const Elf_Addr code_address = text_address + mi.low_pc_; |
| 251 | if (format == DW_EH_FRAME_FORMAT) { |
| 252 | binary_search_table.push_back( |
| 253 | dchecked_integral_cast<uint32_t>(code_address)); |
| 254 | binary_search_table.push_back( |
| 255 | dchecked_integral_cast<uint32_t>(buffer_address)); |
| 256 | } |
| 257 | WriteFDE(is64bit, cfi_address, cie_address, |
| 258 | code_address, mi.high_pc_ - mi.low_pc_, |
| 259 | opcodes, format, buffer_address, &buffer, |
| 260 | &patch_locations); |
| 261 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 262 | buffer_address += buffer.size(); |
| 263 | buffer.clear(); |
| 264 | } |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 265 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 266 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 267 | cfi_section->End(); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 268 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 269 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 270 | if (format == DW_EH_FRAME_FORMAT) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 271 | auto* header_section = builder->GetEhFrameHdr(); |
| 272 | header_section->Start(); |
| 273 | uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 274 | // Write .eh_frame_hdr section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 275 | std::vector<uint8_t> buffer; |
| 276 | Writer<> header(&buffer); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 277 | header.PushUint8(1); // Version. |
| 278 | // Encoding of .eh_frame pointer - libunwind does not honor datarel here, |
| 279 | // so we have to use pcrel which means relative to the pointer's location. |
| 280 | header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
| 281 | // Encoding of binary search table size. |
| 282 | header.PushUint8(DW_EH_PE_udata4); |
| 283 | // Encoding of binary search table addresses - libunwind supports only this |
| 284 | // specific combination, which means relative to the start of .eh_frame_hdr. |
| 285 | header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 286 | // .eh_frame pointer |
| 287 | header.PushInt32(cfi_section->GetAddress() - (header_address + 4u)); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 288 | // Binary search table size (number of entries). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 289 | header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2)); |
| 290 | header_section->WriteFully(buffer.data(), buffer.size()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 291 | // Binary search table. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 292 | for (size_t i = 0; i < binary_search_table.size(); i++) { |
| 293 | // Make addresses section-relative since we know the header address now. |
| 294 | binary_search_table[i] -= header_address; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 295 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 296 | header_section->WriteFully(binary_search_table.data(), binary_search_table.size()); |
| 297 | header_section->End(); |
| 298 | } else { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 299 | builder->WritePatches(".debug_frame.oat_patches", |
| 300 | ArrayRef<const uintptr_t>(patch_locations)); |
David Srbecky | 033d745 | 2015-04-30 19:57:35 +0100 | [diff] [blame] | 301 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 302 | } |
| 303 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 304 | namespace { |
| 305 | struct CompilationUnit { |
| 306 | std::vector<const MethodDebugInfo*> methods_; |
| 307 | size_t debug_line_offset_ = 0; |
| 308 | uint32_t low_pc_ = 0xFFFFFFFFU; |
| 309 | uint32_t high_pc_ = 0; |
| 310 | }; |
| 311 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 312 | typedef std::vector<DexFile::LocalInfo> LocalInfos; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 313 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 314 | void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) { |
| 315 | static_cast<LocalInfos*>(ctx)->push_back(entry); |
| 316 | } |
| 317 | |
| 318 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 319 | |
| 320 | bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 321 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 322 | return false; |
| 323 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 324 | |
| 325 | std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) { |
| 326 | std::vector<const char*> names; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 327 | if (mi->code_item_ != nullptr) { |
| 328 | const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_); |
| 329 | if (stream != nullptr) { |
| 330 | DecodeUnsignedLeb128(&stream); // line. |
| 331 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 332 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 333 | uint32_t id = DecodeUnsignedLeb128P1(&stream); |
| 334 | names.push_back(mi->dex_file_->StringDataByIdx(id)); |
| 335 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | return names; |
| 339 | } |
| 340 | |
| 341 | struct VariableLocation { |
| 342 | uint32_t low_pc; |
| 343 | uint32_t high_pc; |
| 344 | DexRegisterLocation reg_lo; // May be None if the location is unknown. |
| 345 | DexRegisterLocation reg_hi; // Most significant bits of 64-bit value. |
| 346 | }; |
| 347 | |
| 348 | // Get the location of given dex register (e.g. stack or machine register). |
| 349 | // Note that the location might be different based on the current pc. |
| 350 | // The result will cover all ranges where the variable is in scope. |
| 351 | std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info, |
| 352 | uint16_t vreg, |
| 353 | bool is64bitValue, |
| 354 | uint32_t dex_pc_low, |
| 355 | uint32_t dex_pc_high) { |
| 356 | std::vector<VariableLocation> variable_locations; |
| 357 | |
| 358 | // Get stack maps sorted by pc (they might not be sorted internally). |
| 359 | const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data()); |
| 360 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 361 | std::map<uint32_t, StackMap> stack_maps; |
| 362 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 363 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 364 | DCHECK(stack_map.IsValid()); |
| 365 | const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding); |
| 366 | DCHECK_LE(low_pc, method_info->high_pc_); |
| 367 | stack_maps.emplace(low_pc, stack_map); |
| 368 | } |
| 369 | |
| 370 | // Create entries for the requested register based on stack map data. |
| 371 | for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) { |
| 372 | const StackMap& stack_map = it->second; |
| 373 | const uint32_t low_pc = it->first; |
| 374 | auto next_it = it; |
| 375 | next_it++; |
| 376 | const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first |
| 377 | : method_info->high_pc_; |
| 378 | DCHECK_LE(low_pc, high_pc); |
| 379 | if (low_pc == high_pc) { |
| 380 | continue; // Ignore if the address range is empty. |
| 381 | } |
| 382 | |
| 383 | // Check that the stack map is in the requested range. |
| 384 | uint32_t dex_pc = stack_map.GetDexPc(encoding); |
| 385 | if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) { |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | // Find the location of the dex register. |
| 390 | DexRegisterLocation reg_lo = DexRegisterLocation::None(); |
| 391 | DexRegisterLocation reg_hi = DexRegisterLocation::None(); |
| 392 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 393 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 394 | stack_map, encoding, method_info->code_item_->registers_size_); |
| 395 | reg_lo = dex_register_map.GetDexRegisterLocation( |
| 396 | vreg, method_info->code_item_->registers_size_, code_info, encoding); |
| 397 | if (is64bitValue) { |
| 398 | reg_hi = dex_register_map.GetDexRegisterLocation( |
| 399 | vreg + 1, method_info->code_item_->registers_size_, code_info, encoding); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Add location entry for this address range. |
| 404 | if (!variable_locations.empty() && |
| 405 | variable_locations.back().reg_lo == reg_lo && |
| 406 | variable_locations.back().reg_hi == reg_hi && |
| 407 | variable_locations.back().high_pc == low_pc) { |
| 408 | // Merge with the previous entry (extend its range). |
| 409 | variable_locations.back().high_pc = high_pc; |
| 410 | } else { |
| 411 | variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi}); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return variable_locations; |
| 416 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 417 | |
| 418 | bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) { |
| 419 | return method_info->compiled_method_->GetQuickCode().size() > 0 && |
| 420 | method_info->compiled_method_->GetVmapTable().size() > 0 && |
| 421 | method_info->compiled_method_->GetGcMap().size() == 0 && |
| 422 | method_info->code_item_ != nullptr; |
| 423 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 424 | } // namespace |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 425 | |
| 426 | // Helper class to write .debug_info and its supporting sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 427 | template<typename ElfTypes> |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 428 | class DebugInfoWriter { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 429 | typedef typename ElfTypes::Addr Elf_Addr; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 430 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 431 | // Helper class to write one compilation unit. |
| 432 | // It holds helper methods and temporary state. |
| 433 | class CompilationUnitWriter { |
| 434 | public: |
| 435 | explicit CompilationUnitWriter(DebugInfoWriter* owner) |
| 436 | : owner_(owner), |
| 437 | info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) { |
| 438 | } |
| 439 | |
| 440 | void Write(const CompilationUnit& compilation_unit) { |
| 441 | CHECK(!compilation_unit.methods_.empty()); |
| 442 | const Elf_Addr text_address = owner_->builder_->GetText()->GetAddress(); |
| 443 | |
| 444 | info_.StartTag(DW_TAG_compile_unit); |
| 445 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 446 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
Tamas Berghammer | 8c55712 | 2015-12-10 15:06:25 +0000 | [diff] [blame] | 447 | info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT")); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 448 | info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 449 | info_.WriteUdata(DW_AT_high_pc, compilation_unit.high_pc_ - compilation_unit.low_pc_); |
| 450 | info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 451 | |
| 452 | const char* last_dex_class_desc = nullptr; |
| 453 | for (auto mi : compilation_unit.methods_) { |
| 454 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 455 | const DexFile::CodeItem* dex_code = mi->code_item_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 456 | const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_); |
| 457 | const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method); |
| 458 | const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto); |
| 459 | const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 460 | const bool is_static = (mi->access_flags_ & kAccStatic) != 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 461 | |
| 462 | // Enclose the method in correct class definition. |
| 463 | if (last_dex_class_desc != dex_class_desc) { |
| 464 | if (last_dex_class_desc != nullptr) { |
| 465 | EndClassTag(last_dex_class_desc); |
| 466 | } |
| 467 | size_t offset = StartClassTag(dex_class_desc); |
| 468 | type_cache_.emplace(dex_class_desc, offset); |
| 469 | // Check that each class is defined only once. |
| 470 | bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second; |
| 471 | CHECK(unique) << "Redefinition of " << dex_class_desc; |
| 472 | last_dex_class_desc = dex_class_desc; |
| 473 | } |
| 474 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 475 | int start_depth = info_.Depth(); |
| 476 | info_.StartTag(DW_TAG_subprogram); |
| 477 | WriteName(dex->GetMethodName(dex_method)); |
| 478 | info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 479 | info_.WriteUdata(DW_AT_high_pc, mi->high_pc_ - mi->low_pc_); |
| 480 | uint8_t frame_base[] = { DW_OP_call_frame_cfa }; |
| 481 | info_.WriteExprLoc(DW_AT_frame_base, &frame_base, sizeof(frame_base)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 482 | WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 483 | |
| 484 | // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not |
| 485 | // guarantee order or uniqueness so it is safer to iterate over them manually. |
| 486 | // DecodeDebugLocalInfo might not also be available if there is no debug info. |
| 487 | std::vector<const char*> param_names = GetParamNames(mi); |
| 488 | uint32_t arg_reg = 0; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 489 | if (!is_static) { |
| 490 | info_.StartTag(DW_TAG_formal_parameter); |
| 491 | WriteName("this"); |
| 492 | info_.WriteFlag(DW_AT_artificial, true); |
| 493 | WriteLazyType(dex_class_desc); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 494 | if (dex_code != nullptr) { |
| 495 | // Write the stack location of the parameter. |
| 496 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 497 | const bool is64bitValue = false; |
| 498 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 499 | } |
| 500 | arg_reg++; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 501 | info_.EndTag(); |
| 502 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 503 | if (dex_params != nullptr) { |
| 504 | for (uint32_t i = 0; i < dex_params->Size(); ++i) { |
| 505 | info_.StartTag(DW_TAG_formal_parameter); |
| 506 | // Parameter names may not be always available. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 507 | if (i < param_names.size()) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 508 | WriteName(param_names[i]); |
| 509 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 510 | // Write the type. |
| 511 | const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_); |
| 512 | WriteLazyType(type_desc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 513 | const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J'; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 514 | if (dex_code != nullptr) { |
| 515 | // Write the stack location of the parameter. |
| 516 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 517 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 518 | } |
| 519 | arg_reg += is64bitValue ? 2 : 1; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 520 | info_.EndTag(); |
| 521 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 522 | if (dex_code != nullptr) { |
| 523 | DCHECK_EQ(arg_reg, dex_code->ins_size_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 524 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 525 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 526 | |
| 527 | // Write local variables. |
| 528 | LocalInfos local_infos; |
| 529 | if (dex->DecodeDebugLocalInfo(dex_code, |
| 530 | is_static, |
| 531 | mi->dex_method_index_, |
| 532 | LocalInfoCallback, |
| 533 | &local_infos)) { |
| 534 | for (const DexFile::LocalInfo& var : local_infos) { |
| 535 | if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) { |
| 536 | info_.StartTag(DW_TAG_variable); |
| 537 | WriteName(var.name_); |
| 538 | WriteLazyType(var.descriptor_); |
| 539 | bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J'; |
| 540 | WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_, |
| 541 | var.start_address_, var.end_address_); |
| 542 | info_.EndTag(); |
| 543 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 546 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 547 | info_.EndTag(); |
| 548 | CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end. |
| 549 | } |
| 550 | if (last_dex_class_desc != nullptr) { |
| 551 | EndClassTag(last_dex_class_desc); |
| 552 | } |
| 553 | CHECK_EQ(info_.Depth(), 1); |
| 554 | FinishLazyTypes(); |
| 555 | info_.EndTag(); // DW_TAG_compile_unit |
| 556 | std::vector<uint8_t> buffer; |
| 557 | buffer.reserve(info_.data()->size() + KB); |
| 558 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
| 559 | const size_t debug_abbrev_offset = |
| 560 | owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size()); |
| 561 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 562 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 563 | } |
| 564 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 565 | // Write table into .debug_loc which describes location of dex register. |
| 566 | // The dex register might be valid only at some points and it might |
| 567 | // move between machine registers and stack. |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 568 | void WriteRegLocation(const MethodDebugInfo* method_info, |
| 569 | uint16_t vreg, |
| 570 | bool is64bitValue, |
| 571 | uint32_t compilation_unit_low_pc, |
| 572 | uint32_t dex_pc_low = 0, |
| 573 | uint32_t dex_pc_high = 0xFFFFFFFF) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 574 | using Kind = DexRegisterLocation::Kind; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 575 | if (!IsFromOptimizingCompiler(method_info)) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 576 | return; |
| 577 | } |
| 578 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 579 | Writer<> debug_loc(&owner_->debug_loc_); |
| 580 | Writer<> debug_ranges(&owner_->debug_ranges_); |
| 581 | info_.WriteSecOffset(DW_AT_location, debug_loc.size()); |
| 582 | info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 583 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 584 | std::vector<VariableLocation> variable_locations = GetVariableLocations( |
| 585 | method_info, |
| 586 | vreg, |
| 587 | is64bitValue, |
| 588 | dex_pc_low, |
| 589 | dex_pc_high); |
| 590 | |
| 591 | // Write .debug_loc entries. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 592 | const InstructionSet isa = owner_->builder_->GetIsa(); |
| 593 | const bool is64bit = Is64BitInstructionSet(isa); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 594 | for (const VariableLocation& variable_location : variable_locations) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 595 | // Translate dex register location to DWARF expression. |
| 596 | // Note that 64-bit value might be split to two distinct locations. |
| 597 | // (for example, two 32-bit machine registers, or even stack and register) |
| 598 | uint8_t buffer[64]; |
| 599 | uint8_t* pos = buffer; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 600 | DexRegisterLocation reg_lo = variable_location.reg_lo; |
| 601 | DexRegisterLocation reg_hi = variable_location.reg_hi; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 602 | for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) { |
| 603 | DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi); |
| 604 | const Kind kind = reg_loc.GetKind(); |
| 605 | const int32_t value = reg_loc.GetValue(); |
| 606 | if (kind == Kind::kInStack) { |
| 607 | const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes(); |
| 608 | *(pos++) = DW_OP_fbreg; |
| 609 | // The stack offset is relative to SP. Make it relative to CFA. |
| 610 | pos = EncodeSignedLeb128(pos, value - frame_size); |
| 611 | if (piece == 0 && reg_hi.GetKind() == Kind::kInStack && |
| 612 | reg_hi.GetValue() == value + 4) { |
| 613 | break; // the high word is correctly implied by the low word. |
| 614 | } |
| 615 | } else if (kind == Kind::kInRegister) { |
| 616 | pos = WriteOpReg(pos, GetDwarfCoreReg(isa, value).num()); |
| 617 | if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh && |
| 618 | reg_hi.GetValue() == value) { |
| 619 | break; // the high word is correctly implied by the low word. |
| 620 | } |
| 621 | } else if (kind == Kind::kInFpuRegister) { |
| 622 | if ((isa == kArm || isa == kThumb2) && |
| 623 | piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister && |
| 624 | reg_hi.GetValue() == value + 1 && value % 2 == 0) { |
| 625 | // Translate S register pair to D register (e.g. S4+S5 to D2). |
| 626 | pos = WriteOpReg(pos, Reg::ArmDp(value / 2).num()); |
| 627 | break; |
| 628 | } |
David Srbecky | 3dd7e5a | 2015-11-27 13:31:16 +0000 | [diff] [blame] | 629 | if (isa == kMips || isa == kMips64) { |
| 630 | // TODO: Find what the DWARF floating point register numbers are on MIPS. |
| 631 | break; |
| 632 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 633 | pos = WriteOpReg(pos, GetDwarfFpReg(isa, value).num()); |
| 634 | if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh && |
| 635 | reg_hi.GetValue() == reg_lo.GetValue()) { |
| 636 | break; // the high word is correctly implied by the low word. |
| 637 | } |
| 638 | } else if (kind == Kind::kConstant) { |
| 639 | *(pos++) = DW_OP_consts; |
| 640 | pos = EncodeSignedLeb128(pos, value); |
| 641 | *(pos++) = DW_OP_stack_value; |
| 642 | } else if (kind == Kind::kNone) { |
| 643 | break; |
| 644 | } else { |
| 645 | // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind(). |
| 646 | // kInRegisterHigh and kInFpuRegisterHigh should be handled by |
| 647 | // the special cases above and they should not occur alone. |
| 648 | LOG(ERROR) << "Unexpected register location kind: " |
| 649 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 650 | break; |
| 651 | } |
| 652 | if (is64bitValue) { |
| 653 | // Write the marker which is needed by split 64-bit values. |
| 654 | // This code is skipped by the special cases. |
| 655 | *(pos++) = DW_OP_piece; |
| 656 | pos = EncodeUnsignedLeb128(pos, 4); |
| 657 | } |
| 658 | } |
| 659 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 660 | // Check that the buffer is large enough; keep half of it empty for safety. |
| 661 | DCHECK_LE(static_cast<size_t>(pos - buffer), sizeof(buffer) / 2); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 662 | if (pos > buffer) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 663 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 664 | debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc); |
| 665 | debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 666 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 667 | debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc); |
| 668 | debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 669 | } |
| 670 | // Write the expression. |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 671 | debug_loc.PushUint16(pos - buffer); |
| 672 | debug_loc.PushData(buffer, pos - buffer); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 673 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 674 | // Do not generate .debug_loc if the location is not known. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | // Write end-of-list entry. |
| 678 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 679 | debug_loc.PushUint64(0); |
| 680 | debug_loc.PushUint64(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 681 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 682 | debug_loc.PushUint32(0); |
| 683 | debug_loc.PushUint32(0); |
| 684 | } |
| 685 | |
| 686 | // Write .debug_ranges entries. |
| 687 | // This includes ranges where the variable is in scope but the location is not known. |
| 688 | for (size_t i = 0; i < variable_locations.size(); i++) { |
| 689 | uint32_t low_pc = variable_locations[i].low_pc; |
| 690 | uint32_t high_pc = variable_locations[i].high_pc; |
| 691 | while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) { |
| 692 | // Merge address range with the next entry. |
| 693 | high_pc = variable_locations[++i].high_pc; |
| 694 | } |
| 695 | if (is64bit) { |
| 696 | debug_ranges.PushUint64(low_pc - compilation_unit_low_pc); |
| 697 | debug_ranges.PushUint64(high_pc - compilation_unit_low_pc); |
| 698 | } else { |
| 699 | debug_ranges.PushUint32(low_pc - compilation_unit_low_pc); |
| 700 | debug_ranges.PushUint32(high_pc - compilation_unit_low_pc); |
| 701 | } |
| 702 | } |
| 703 | // Write end-of-list entry. |
| 704 | if (is64bit) { |
| 705 | debug_ranges.PushUint64(0); |
| 706 | debug_ranges.PushUint64(0); |
| 707 | } else { |
| 708 | debug_ranges.PushUint32(0); |
| 709 | debug_ranges.PushUint32(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 713 | // Some types are difficult to define as we go since they need |
| 714 | // to be enclosed in the right set of namespaces. Therefore we |
| 715 | // just define all types lazily at the end of compilation unit. |
| 716 | void WriteLazyType(const char* type_descriptor) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 717 | if (type_descriptor != nullptr && type_descriptor[0] != 'V') { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 718 | lazy_types_.emplace(type_descriptor, info_.size()); |
| 719 | info_.WriteRef4(DW_AT_type, 0); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | void FinishLazyTypes() { |
| 724 | for (const auto& lazy_type : lazy_types_) { |
| 725 | info_.UpdateUint32(lazy_type.second, WriteType(lazy_type.first)); |
| 726 | } |
| 727 | lazy_types_.clear(); |
| 728 | } |
| 729 | |
| 730 | private: |
| 731 | void WriteName(const char* name) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 732 | if (name != nullptr) { |
| 733 | info_.WriteStrp(DW_AT_name, owner_->WriteString(name)); |
| 734 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 735 | } |
| 736 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 737 | // Helper which writes DWARF expression referencing a register. |
| 738 | static uint8_t* WriteOpReg(uint8_t* buffer, uint32_t dwarf_reg_num) { |
| 739 | if (dwarf_reg_num < 32) { |
| 740 | *(buffer++) = DW_OP_reg0 + dwarf_reg_num; |
| 741 | } else { |
| 742 | *(buffer++) = DW_OP_regx; |
| 743 | buffer = EncodeUnsignedLeb128(buffer, dwarf_reg_num); |
| 744 | } |
| 745 | return buffer; |
| 746 | } |
| 747 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 748 | // Convert dex type descriptor to DWARF. |
| 749 | // Returns offset in the compilation unit. |
| 750 | size_t WriteType(const char* desc) { |
| 751 | const auto& it = type_cache_.find(desc); |
| 752 | if (it != type_cache_.end()) { |
| 753 | return it->second; |
| 754 | } |
| 755 | |
| 756 | size_t offset; |
| 757 | if (*desc == 'L') { |
| 758 | // Class type. For example: Lpackage/name; |
| 759 | offset = StartClassTag(desc); |
| 760 | info_.WriteFlag(DW_AT_declaration, true); |
| 761 | EndClassTag(desc); |
| 762 | } else if (*desc == '[') { |
| 763 | // Array type. |
| 764 | size_t element_type = WriteType(desc + 1); |
| 765 | offset = info_.StartTag(DW_TAG_array_type); |
| 766 | info_.WriteRef(DW_AT_type, element_type); |
| 767 | info_.EndTag(); |
| 768 | } else { |
| 769 | // Primitive types. |
| 770 | const char* name; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 771 | uint32_t encoding; |
| 772 | uint32_t byte_size; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 773 | switch (*desc) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 774 | case 'B': |
| 775 | name = "byte"; |
| 776 | encoding = DW_ATE_signed; |
| 777 | byte_size = 1; |
| 778 | break; |
| 779 | case 'C': |
| 780 | name = "char"; |
| 781 | encoding = DW_ATE_UTF; |
| 782 | byte_size = 2; |
| 783 | break; |
| 784 | case 'D': |
| 785 | name = "double"; |
| 786 | encoding = DW_ATE_float; |
| 787 | byte_size = 8; |
| 788 | break; |
| 789 | case 'F': |
| 790 | name = "float"; |
| 791 | encoding = DW_ATE_float; |
| 792 | byte_size = 4; |
| 793 | break; |
| 794 | case 'I': |
| 795 | name = "int"; |
| 796 | encoding = DW_ATE_signed; |
| 797 | byte_size = 4; |
| 798 | break; |
| 799 | case 'J': |
| 800 | name = "long"; |
| 801 | encoding = DW_ATE_signed; |
| 802 | byte_size = 8; |
| 803 | break; |
| 804 | case 'S': |
| 805 | name = "short"; |
| 806 | encoding = DW_ATE_signed; |
| 807 | byte_size = 2; |
| 808 | break; |
| 809 | case 'Z': |
| 810 | name = "boolean"; |
| 811 | encoding = DW_ATE_boolean; |
| 812 | byte_size = 1; |
| 813 | break; |
| 814 | case 'V': |
| 815 | LOG(FATAL) << "Void type should not be encoded"; |
| 816 | UNREACHABLE(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 817 | default: |
| 818 | LOG(FATAL) << "Unknown dex type descriptor: " << desc; |
| 819 | UNREACHABLE(); |
| 820 | } |
| 821 | offset = info_.StartTag(DW_TAG_base_type); |
| 822 | WriteName(name); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 823 | info_.WriteData1(DW_AT_encoding, encoding); |
| 824 | info_.WriteData1(DW_AT_byte_size, byte_size); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 825 | info_.EndTag(); |
| 826 | } |
| 827 | |
| 828 | type_cache_.emplace(desc, offset); |
| 829 | return offset; |
| 830 | } |
| 831 | |
| 832 | // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags. |
| 833 | // Returns offset of the class tag in the compilation unit. |
| 834 | size_t StartClassTag(const char* desc) { |
| 835 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 836 | // Enclose the type in namespace tags. |
| 837 | const char* end; |
| 838 | for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) { |
| 839 | info_.StartTag(DW_TAG_namespace); |
| 840 | WriteName(std::string(desc, end - desc).c_str()); |
| 841 | } |
| 842 | // Start the class tag. |
| 843 | size_t offset = info_.StartTag(DW_TAG_class_type); |
| 844 | end = strchr(desc, ';'); |
| 845 | CHECK(end != nullptr); |
| 846 | WriteName(std::string(desc, end - desc).c_str()); |
| 847 | return offset; |
| 848 | } |
| 849 | |
| 850 | void EndClassTag(const char* desc) { |
| 851 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 852 | // End the class tag. |
| 853 | info_.EndTag(); |
| 854 | // Close namespace tags. |
| 855 | const char* end; |
| 856 | for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) { |
| 857 | info_.EndTag(); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | // For access to the ELF sections. |
| 862 | DebugInfoWriter<ElfTypes>* owner_; |
| 863 | // Debug abbrevs for this compilation unit only. |
| 864 | std::vector<uint8_t> debug_abbrev_; |
| 865 | // Temporary buffer to create and store the entries. |
| 866 | DebugInfoEntryWriter<> info_; |
| 867 | // Cache of already translated type descriptors. |
| 868 | std::map<const char*, size_t, CStringLess> type_cache_; // type_desc -> definition_offset. |
| 869 | // 32-bit references which need to be resolved to a type later. |
| 870 | std::multimap<const char*, size_t, CStringLess> lazy_types_; // type_desc -> patch_offset. |
| 871 | }; |
| 872 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 873 | public: |
| 874 | explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 875 | } |
| 876 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 877 | void Start() { |
| 878 | builder_->GetDebugInfo()->Start(); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 879 | } |
| 880 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 881 | void WriteCompilationUnit(const CompilationUnit& compilation_unit) { |
| 882 | CompilationUnitWriter writer(this); |
| 883 | writer.Write(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 884 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 885 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 886 | void End() { |
| 887 | builder_->GetDebugInfo()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 888 | builder_->WritePatches(".debug_info.oat_patches", |
| 889 | ArrayRef<const uintptr_t>(debug_info_patches_)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 890 | builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data()); |
| 891 | builder_->WriteSection(".debug_str", &debug_str_.Data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 892 | builder_->WriteSection(".debug_loc", &debug_loc_); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 893 | builder_->WriteSection(".debug_ranges", &debug_ranges_); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | private: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 897 | size_t WriteString(const char* str) { |
| 898 | return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1); |
| 899 | } |
| 900 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 901 | ElfBuilder<ElfTypes>* builder_; |
| 902 | std::vector<uintptr_t> debug_info_patches_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 903 | DedupVector debug_abbrev_; |
| 904 | DedupVector debug_str_; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 905 | std::vector<uint8_t> debug_loc_; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 906 | std::vector<uint8_t> debug_ranges_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 907 | |
| 908 | std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only. |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 909 | }; |
| 910 | |
| 911 | template<typename ElfTypes> |
| 912 | class DebugLineWriter { |
| 913 | typedef typename ElfTypes::Addr Elf_Addr; |
| 914 | |
| 915 | public: |
| 916 | explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
| 917 | } |
| 918 | |
| 919 | void Start() { |
| 920 | builder_->GetDebugLine()->Start(); |
| 921 | } |
| 922 | |
| 923 | // Write line table for given set of methods. |
| 924 | // Returns the number of bytes written. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 925 | size_t WriteCompilationUnit(CompilationUnit& compilation_unit) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 926 | const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); |
| 927 | const Elf_Addr text_address = builder_->GetText()->GetAddress(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 928 | |
| 929 | compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 930 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 931 | std::vector<FileEntry> files; |
| 932 | std::unordered_map<std::string, size_t> files_map; |
| 933 | std::vector<std::string> directories; |
| 934 | std::unordered_map<std::string, size_t> directories_map; |
| 935 | int code_factor_bits_ = 0; |
| 936 | int dwarf_isa = -1; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 937 | switch (builder_->GetIsa()) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 938 | case kArm: // arm actually means thumb2. |
| 939 | case kThumb2: |
| 940 | code_factor_bits_ = 1; // 16-bit instuctions |
| 941 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 942 | break; |
| 943 | case kArm64: |
| 944 | case kMips: |
| 945 | case kMips64: |
| 946 | code_factor_bits_ = 2; // 32-bit instructions |
| 947 | break; |
| 948 | case kNone: |
| 949 | case kX86: |
| 950 | case kX86_64: |
| 951 | break; |
| 952 | } |
David Srbecky | 297ed22 | 2015-04-15 01:18:12 +0100 | [diff] [blame] | 953 | DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 954 | for (const MethodDebugInfo* mi : compilation_unit.methods_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 955 | // Ignore function if we have already generated line table for the same address. |
| 956 | // It would confuse the debugger and the DWARF specification forbids it. |
| 957 | if (mi->deduped_) { |
| 958 | continue; |
| 959 | } |
| 960 | |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 961 | ArrayRef<const SrcMapElem> src_mapping_table; |
| 962 | std::vector<SrcMapElem> src_mapping_table_from_stack_maps; |
| 963 | if (IsFromOptimizingCompiler(mi)) { |
| 964 | // Use stack maps to create mapping table from pc to dex. |
| 965 | const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data()); |
| 966 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 967 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 968 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 969 | DCHECK(stack_map.IsValid()); |
| 970 | const uint32_t pc = stack_map.GetNativePcOffset(encoding); |
| 971 | const int32_t dex = stack_map.GetDexPc(encoding); |
| 972 | src_mapping_table_from_stack_maps.push_back({pc, dex}); |
| 973 | } |
| 974 | std::sort(src_mapping_table_from_stack_maps.begin(), |
| 975 | src_mapping_table_from_stack_maps.end()); |
| 976 | src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps); |
| 977 | } else { |
| 978 | // Use the mapping table provided by the quick compiler. |
| 979 | src_mapping_table = mi->compiled_method_->GetSrcMappingTable(); |
| 980 | } |
| 981 | |
| 982 | if (src_mapping_table.empty()) { |
| 983 | continue; |
| 984 | } |
| 985 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 986 | Elf_Addr method_address = text_address + mi->low_pc_; |
| 987 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 988 | PositionInfos position_infos; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 989 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 990 | if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) { |
| 991 | continue; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 992 | } |
| 993 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 994 | if (position_infos.empty()) { |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 995 | continue; |
| 996 | } |
| 997 | |
| 998 | opcodes.SetAddress(method_address); |
| 999 | if (dwarf_isa != -1) { |
| 1000 | opcodes.SetISA(dwarf_isa); |
| 1001 | } |
| 1002 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1003 | // Get and deduplicate directory and filename. |
| 1004 | int file_index = 0; // 0 - primary source file of the compilation. |
| 1005 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index_); |
| 1006 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 1007 | if (source_file != nullptr) { |
| 1008 | std::string file_name(source_file); |
| 1009 | size_t file_name_slash = file_name.find_last_of('/'); |
| 1010 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 1011 | size_t class_name_slash = class_name.find_last_of('/'); |
| 1012 | std::string full_path(file_name); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1013 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1014 | // Guess directory from package name. |
| 1015 | int directory_index = 0; // 0 - current directory of the compilation. |
| 1016 | if (file_name_slash == std::string::npos && // Just filename. |
| 1017 | class_name.front() == 'L' && // Type descriptor for a class. |
| 1018 | class_name_slash != std::string::npos) { // Has package name. |
| 1019 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 1020 | auto it = directories_map.find(package_name); |
| 1021 | if (it == directories_map.end()) { |
| 1022 | directory_index = 1 + directories.size(); |
| 1023 | directories_map.emplace(package_name, directory_index); |
| 1024 | directories.push_back(package_name); |
| 1025 | } else { |
| 1026 | directory_index = it->second; |
| 1027 | } |
| 1028 | full_path = package_name + "/" + file_name; |
| 1029 | } |
| 1030 | |
| 1031 | // Add file entry. |
| 1032 | auto it2 = files_map.find(full_path); |
| 1033 | if (it2 == files_map.end()) { |
| 1034 | file_index = 1 + files.size(); |
| 1035 | files_map.emplace(full_path, file_index); |
| 1036 | files.push_back(FileEntry { |
| 1037 | file_name, |
| 1038 | directory_index, |
| 1039 | 0, // Modification time - NA. |
| 1040 | 0, // File size - NA. |
| 1041 | }); |
| 1042 | } else { |
| 1043 | file_index = it2->second; |
| 1044 | } |
| 1045 | } |
| 1046 | opcodes.SetFile(file_index); |
| 1047 | |
| 1048 | // Generate mapping opcodes from PC to Java lines. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1049 | if (file_index != 0) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1050 | bool first = true; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1051 | for (SrcMapElem pc2dex : src_mapping_table) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1052 | uint32_t pc = pc2dex.from_; |
| 1053 | int dex_pc = pc2dex.to_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1054 | // Find mapping with address with is greater than our dex pc; then go back one step. |
| 1055 | auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc, |
| 1056 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 1057 | return address < entry.address_; |
| 1058 | }); |
| 1059 | if (ub != position_infos.begin()) { |
| 1060 | int line = (--ub)->line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1061 | if (first) { |
| 1062 | first = false; |
| 1063 | if (pc > 0) { |
| 1064 | // Assume that any preceding code is prologue. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1065 | int first_line = position_infos.front().line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1066 | // Prologue is not a sensible place for a breakpoint. |
| 1067 | opcodes.NegateStmt(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1068 | opcodes.AddRow(method_address, first_line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1069 | opcodes.NegateStmt(); |
| 1070 | opcodes.SetPrologueEnd(); |
| 1071 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1072 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1073 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1074 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1075 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1076 | } |
| 1077 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1078 | } else { |
| 1079 | // line 0 - instruction cannot be attributed to any source line. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1080 | opcodes.AddRow(method_address, 0); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1081 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1082 | |
| 1083 | opcodes.AdvancePC(text_address + mi->high_pc_); |
| 1084 | opcodes.EndSequence(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1085 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1086 | std::vector<uint8_t> buffer; |
| 1087 | buffer.reserve(opcodes.data()->size() + KB); |
| 1088 | size_t offset = builder_->GetDebugLine()->GetSize(); |
| 1089 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches); |
| 1090 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 1091 | return buffer.size(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1092 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1093 | |
| 1094 | void End() { |
| 1095 | builder_->GetDebugLine()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1096 | builder_->WritePatches(".debug_line.oat_patches", |
| 1097 | ArrayRef<const uintptr_t>(debug_line_patches)); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | private: |
| 1101 | ElfBuilder<ElfTypes>* builder_; |
| 1102 | std::vector<uintptr_t> debug_line_patches; |
| 1103 | }; |
| 1104 | |
| 1105 | template<typename ElfTypes> |
| 1106 | void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1107 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1108 | // Group the methods into compilation units based on source file. |
| 1109 | std::vector<CompilationUnit> compilation_units; |
| 1110 | const char* last_source_file = nullptr; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1111 | for (const MethodDebugInfo& mi : method_infos) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1112 | auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_); |
| 1113 | const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def); |
| 1114 | if (compilation_units.empty() || source_file != last_source_file) { |
| 1115 | compilation_units.push_back(CompilationUnit()); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1116 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1117 | CompilationUnit& cu = compilation_units.back(); |
| 1118 | cu.methods_.push_back(&mi); |
| 1119 | cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_); |
| 1120 | cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_); |
| 1121 | last_source_file = source_file; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | // Write .debug_line section. |
| 1125 | { |
| 1126 | DebugLineWriter<ElfTypes> line_writer(builder); |
| 1127 | line_writer.Start(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1128 | for (auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1129 | line_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1130 | } |
| 1131 | line_writer.End(); |
| 1132 | } |
| 1133 | |
| 1134 | // Write .debug_info section. |
| 1135 | { |
| 1136 | DebugInfoWriter<ElfTypes> info_writer(builder); |
| 1137 | info_writer.Start(); |
| 1138 | for (const auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1139 | info_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1140 | } |
| 1141 | info_writer.End(); |
| 1142 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1145 | template <typename ElfTypes> |
| 1146 | void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 1147 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
| 1148 | bool generated_mapping_symbol = false; |
| 1149 | auto* strtab = builder->GetStrTab(); |
| 1150 | auto* symtab = builder->GetSymTab(); |
| 1151 | |
| 1152 | if (method_infos.empty()) { |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | // Find all addresses (low_pc) which contain deduped methods. |
| 1157 | // The first instance of method is not marked deduped_, but the rest is. |
| 1158 | std::unordered_set<uint32_t> deduped_addresses; |
| 1159 | for (const MethodDebugInfo& info : method_infos) { |
| 1160 | if (info.deduped_) { |
| 1161 | deduped_addresses.insert(info.low_pc_); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | strtab->Start(); |
| 1166 | strtab->Write(""); // strtab should start with empty string. |
| 1167 | for (const MethodDebugInfo& info : method_infos) { |
| 1168 | if (info.deduped_) { |
| 1169 | continue; // Add symbol only for the first instance. |
| 1170 | } |
| 1171 | std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, true); |
| 1172 | if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) { |
| 1173 | name += " [DEDUPED]"; |
| 1174 | } |
| 1175 | |
| 1176 | uint32_t low_pc = info.low_pc_; |
| 1177 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 1178 | low_pc += info.compiled_method_->CodeDelta(); |
| 1179 | symtab->Add(strtab->Write(name), builder->GetText(), low_pc, |
| 1180 | true, info.high_pc_ - info.low_pc_, STB_GLOBAL, STT_FUNC); |
| 1181 | |
| 1182 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 1183 | // instructions, so that disassembler tools can correctly disassemble. |
| 1184 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 1185 | // requires it to match function symbol. Just address 0 does not work. |
| 1186 | if (info.compiled_method_->GetInstructionSet() == kThumb2) { |
| 1187 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
| 1188 | symtab->Add(strtab->Write("$t"), builder->GetText(), info.low_pc_ & ~1, |
| 1189 | true, 0, STB_LOCAL, STT_NOTYPE); |
| 1190 | generated_mapping_symbol = true; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | strtab->End(); |
| 1195 | |
| 1196 | // Symbols are buffered and written after names (because they are smaller). |
| 1197 | // We could also do two passes in this function to avoid the buffering. |
| 1198 | symtab->Start(); |
| 1199 | symtab->Write(); |
| 1200 | symtab->End(); |
| 1201 | } |
| 1202 | |
| 1203 | template <typename ElfTypes> |
| 1204 | void WriteDebugInfo(ElfBuilder<ElfTypes>* builder, |
| 1205 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 1206 | CFIFormat cfi_format) { |
| 1207 | if (!method_infos.empty()) { |
| 1208 | // Add methods to .symtab. |
| 1209 | WriteDebugSymbols(builder, method_infos); |
| 1210 | // Generate CFI (stack unwinding information). |
| 1211 | WriteCFISection(builder, method_infos, cfi_format); |
| 1212 | // Write DWARF .debug_* sections. |
| 1213 | WriteDebugSections(builder, method_infos); |
| 1214 | } |
| 1215 | } |
| 1216 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1217 | // Explicit instantiations |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1218 | template void WriteDebugInfo<ElfTypes32>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1219 | ElfBuilder<ElfTypes32>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1220 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1221 | CFIFormat cfi_format); |
| 1222 | template void WriteDebugInfo<ElfTypes64>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1223 | ElfBuilder<ElfTypes64>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1224 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1225 | CFIFormat cfi_format); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1226 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1227 | } // namespace dwarf |
| 1228 | } // namespace art |