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 | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 19 | #include <algorithm> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 20 | #include <unordered_set> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 21 | #include <vector> |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 22 | #include <cstdio> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 23 | |
Andreas Gampe | e3d623e | 2015-05-01 16:11:04 -0700 | [diff] [blame] | 24 | #include "base/casts.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 25 | #include "base/stl_util.h" |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 26 | #include "linear_alloc.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 27 | #include "compiled_method.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 29 | #include "driver/compiler_driver.h" |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 30 | #include "dwarf/dedup_vector.h" |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 31 | #include "dwarf/expression.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 32 | #include "dwarf/headers.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 33 | #include "dwarf/method_debug_info.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 34 | #include "dwarf/register.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 35 | #include "elf_builder.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 36 | #include "linker/vector_output_stream.h" |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 37 | #include "mirror/array.h" |
| 38 | #include "mirror/class-inl.h" |
| 39 | #include "mirror/class.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 40 | #include "oat_writer.h" |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 41 | #include "stack_map.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 42 | #include "utils.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 43 | |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 44 | // liblzma. |
| 45 | #include "XzEnc.h" |
| 46 | #include "7zCrc.h" |
| 47 | #include "XzCrc64.h" |
| 48 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 49 | namespace art { |
| 50 | namespace dwarf { |
| 51 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 52 | // The ARM specification defines three special mapping symbols |
| 53 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 54 | // These symbols can be used by tools, for example, to pretty |
| 55 | // print instructions correctly. Objdump will use them if they |
| 56 | // exist, but it will still work well without them. |
| 57 | // However, these extra symbols take space, so let's just generate |
| 58 | // one symbol which marks the whole .text section as code. |
| 59 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 60 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 61 | static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) { |
| 62 | switch (isa) { |
| 63 | case kArm: |
| 64 | case kThumb2: |
| 65 | return Reg::ArmCore(machine_reg); |
| 66 | case kArm64: |
| 67 | return Reg::Arm64Core(machine_reg); |
| 68 | case kX86: |
| 69 | return Reg::X86Core(machine_reg); |
| 70 | case kX86_64: |
| 71 | return Reg::X86_64Core(machine_reg); |
| 72 | case kMips: |
| 73 | return Reg::MipsCore(machine_reg); |
| 74 | case kMips64: |
| 75 | return Reg::Mips64Core(machine_reg); |
| 76 | default: |
| 77 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 78 | UNREACHABLE(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) { |
| 83 | switch (isa) { |
| 84 | case kArm: |
| 85 | case kThumb2: |
| 86 | return Reg::ArmFp(machine_reg); |
| 87 | case kArm64: |
| 88 | return Reg::Arm64Fp(machine_reg); |
| 89 | case kX86: |
| 90 | return Reg::X86Fp(machine_reg); |
| 91 | case kX86_64: |
| 92 | return Reg::X86_64Fp(machine_reg); |
David Srbecky | ba70200 | 2016-02-01 18:15:29 +0000 | [diff] [blame] | 93 | case kMips: |
| 94 | return Reg::MipsFp(machine_reg); |
| 95 | case kMips64: |
| 96 | return Reg::Mips64Fp(machine_reg); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 97 | default: |
| 98 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 99 | UNREACHABLE(); |
| 100 | } |
| 101 | } |
| 102 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 103 | static void WriteCIE(InstructionSet isa, |
| 104 | CFIFormat format, |
| 105 | std::vector<uint8_t>* buffer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 106 | // Scratch registers should be marked as undefined. This tells the |
| 107 | // debugger that its value in the previous frame is not recoverable. |
| 108 | bool is64bit = Is64BitInstructionSet(isa); |
| 109 | switch (isa) { |
| 110 | case kArm: |
| 111 | case kThumb2: { |
| 112 | DebugFrameOpCodeWriter<> opcodes; |
| 113 | opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP). |
| 114 | // core registers. |
| 115 | for (int reg = 0; reg < 13; reg++) { |
| 116 | if (reg < 4 || reg == 12) { |
| 117 | opcodes.Undefined(Reg::ArmCore(reg)); |
| 118 | } else { |
| 119 | opcodes.SameValue(Reg::ArmCore(reg)); |
| 120 | } |
| 121 | } |
| 122 | // fp registers. |
| 123 | for (int reg = 0; reg < 32; reg++) { |
| 124 | if (reg < 16) { |
| 125 | opcodes.Undefined(Reg::ArmFp(reg)); |
| 126 | } else { |
| 127 | opcodes.SameValue(Reg::ArmFp(reg)); |
| 128 | } |
| 129 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 130 | auto return_reg = Reg::ArmCore(14); // R14(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 131 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | case kArm64: { |
| 135 | DebugFrameOpCodeWriter<> opcodes; |
| 136 | opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP). |
| 137 | // core registers. |
| 138 | for (int reg = 0; reg < 30; reg++) { |
| 139 | if (reg < 8 || reg == 16 || reg == 17) { |
| 140 | opcodes.Undefined(Reg::Arm64Core(reg)); |
| 141 | } else { |
| 142 | opcodes.SameValue(Reg::Arm64Core(reg)); |
| 143 | } |
| 144 | } |
| 145 | // fp registers. |
| 146 | for (int reg = 0; reg < 32; reg++) { |
| 147 | if (reg < 8 || reg >= 16) { |
| 148 | opcodes.Undefined(Reg::Arm64Fp(reg)); |
| 149 | } else { |
| 150 | opcodes.SameValue(Reg::Arm64Fp(reg)); |
| 151 | } |
| 152 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 153 | auto return_reg = Reg::Arm64Core(30); // R30(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 154 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | case kMips: |
| 158 | case kMips64: { |
| 159 | DebugFrameOpCodeWriter<> opcodes; |
| 160 | opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP). |
| 161 | // core registers. |
| 162 | for (int reg = 1; reg < 26; reg++) { |
| 163 | if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*. |
| 164 | opcodes.Undefined(Reg::MipsCore(reg)); |
| 165 | } else { |
| 166 | opcodes.SameValue(Reg::MipsCore(reg)); |
| 167 | } |
| 168 | } |
David Srbecky | ba70200 | 2016-02-01 18:15:29 +0000 | [diff] [blame] | 169 | // fp registers. |
| 170 | for (int reg = 0; reg < 32; reg++) { |
| 171 | if (reg < 24) { |
| 172 | opcodes.Undefined(Reg::Mips64Fp(reg)); |
| 173 | } else { |
| 174 | opcodes.SameValue(Reg::Mips64Fp(reg)); |
| 175 | } |
| 176 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 177 | auto return_reg = Reg::MipsCore(31); // R31(RA). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 178 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | case kX86: { |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 182 | // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296 |
| 183 | constexpr bool generate_opcodes_for_x86_fp = false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 184 | DebugFrameOpCodeWriter<> opcodes; |
| 185 | opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP). |
| 186 | opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP). |
| 187 | // core registers. |
| 188 | for (int reg = 0; reg < 8; reg++) { |
| 189 | if (reg <= 3) { |
| 190 | opcodes.Undefined(Reg::X86Core(reg)); |
| 191 | } else if (reg == 4) { |
| 192 | // Stack pointer. |
| 193 | } else { |
| 194 | opcodes.SameValue(Reg::X86Core(reg)); |
| 195 | } |
| 196 | } |
| 197 | // fp registers. |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 198 | if (generate_opcodes_for_x86_fp) { |
| 199 | for (int reg = 0; reg < 8; reg++) { |
| 200 | opcodes.Undefined(Reg::X86Fp(reg)); |
| 201 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 202 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 203 | auto return_reg = Reg::X86Core(8); // R8(EIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 204 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | case kX86_64: { |
| 208 | DebugFrameOpCodeWriter<> opcodes; |
| 209 | opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP). |
| 210 | opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP). |
| 211 | // core registers. |
| 212 | for (int reg = 0; reg < 16; reg++) { |
| 213 | if (reg == 4) { |
| 214 | // Stack pointer. |
| 215 | } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP. |
| 216 | opcodes.Undefined(Reg::X86_64Core(reg)); |
| 217 | } else { |
| 218 | opcodes.SameValue(Reg::X86_64Core(reg)); |
| 219 | } |
| 220 | } |
| 221 | // fp registers. |
| 222 | for (int reg = 0; reg < 16; reg++) { |
| 223 | if (reg < 12) { |
| 224 | opcodes.Undefined(Reg::X86_64Fp(reg)); |
| 225 | } else { |
| 226 | opcodes.SameValue(Reg::X86_64Fp(reg)); |
| 227 | } |
| 228 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 229 | auto return_reg = Reg::X86_64Core(16); // R16(RIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 230 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | case kNone: |
| 234 | break; |
| 235 | } |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 236 | LOG(FATAL) << "Cannot write CIE frame for ISA " << isa; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 237 | UNREACHABLE(); |
| 238 | } |
| 239 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 240 | template<typename ElfTypes> |
| 241 | void WriteCFISection(ElfBuilder<ElfTypes>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 242 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 243 | CFIFormat format, |
| 244 | bool write_oat_patches) { |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 245 | CHECK(format == DW_DEBUG_FRAME_FORMAT || format == DW_EH_FRAME_FORMAT); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 246 | typedef typename ElfTypes::Addr Elf_Addr; |
| 247 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 248 | if (method_infos.empty()) { |
| 249 | return; |
| 250 | } |
| 251 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 252 | std::vector<uint32_t> binary_search_table; |
| 253 | std::vector<uintptr_t> patch_locations; |
| 254 | if (format == DW_EH_FRAME_FORMAT) { |
| 255 | binary_search_table.reserve(2 * method_infos.size()); |
| 256 | } else { |
| 257 | patch_locations.reserve(method_infos.size()); |
| 258 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 259 | |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 260 | // The methods can be written any order. |
| 261 | // Let's therefore sort them in the lexicographical order of the opcodes. |
| 262 | // This has no effect on its own. However, if the final .debug_frame section is |
| 263 | // compressed it reduces the size since similar opcodes sequences are grouped. |
| 264 | std::vector<const MethodDebugInfo*> sorted_method_infos; |
| 265 | sorted_method_infos.reserve(method_infos.size()); |
| 266 | for (size_t i = 0; i < method_infos.size(); i++) { |
| 267 | sorted_method_infos.push_back(&method_infos[i]); |
| 268 | } |
| 269 | std::sort( |
| 270 | sorted_method_infos.begin(), |
| 271 | sorted_method_infos.end(), |
| 272 | [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) { |
| 273 | ArrayRef<const uint8_t> l = lhs->compiled_method_->GetCFIInfo(); |
| 274 | ArrayRef<const uint8_t> r = rhs->compiled_method_->GetCFIInfo(); |
| 275 | return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); |
| 276 | }); |
| 277 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 278 | // Write .eh_frame/.debug_frame section. |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 279 | auto* cfi_section = (format == DW_DEBUG_FRAME_FORMAT |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 280 | ? builder->GetDebugFrame() |
| 281 | : builder->GetEhFrame()); |
| 282 | { |
| 283 | cfi_section->Start(); |
| 284 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 285 | const Elf_Addr text_address = builder->GetText()->Exists() |
| 286 | ? builder->GetText()->GetAddress() |
| 287 | : 0; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 288 | const Elf_Addr cfi_address = cfi_section->GetAddress(); |
| 289 | const Elf_Addr cie_address = cfi_address; |
| 290 | Elf_Addr buffer_address = cfi_address; |
| 291 | std::vector<uint8_t> buffer; // Small temporary buffer. |
| 292 | WriteCIE(builder->GetIsa(), format, &buffer); |
| 293 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 294 | buffer_address += buffer.size(); |
| 295 | buffer.clear(); |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 296 | for (const MethodDebugInfo* mi : sorted_method_infos) { |
| 297 | if (!mi->deduped_) { // Only one FDE per unique address. |
| 298 | ArrayRef<const uint8_t> opcodes = mi->compiled_method_->GetCFIInfo(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 299 | if (!opcodes.empty()) { |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 300 | const Elf_Addr code_address = text_address + mi->low_pc_; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 301 | if (format == DW_EH_FRAME_FORMAT) { |
| 302 | binary_search_table.push_back( |
| 303 | dchecked_integral_cast<uint32_t>(code_address)); |
| 304 | binary_search_table.push_back( |
| 305 | dchecked_integral_cast<uint32_t>(buffer_address)); |
| 306 | } |
| 307 | WriteFDE(is64bit, cfi_address, cie_address, |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 308 | code_address, mi->high_pc_ - mi->low_pc_, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 309 | opcodes, format, buffer_address, &buffer, |
| 310 | &patch_locations); |
| 311 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 312 | buffer_address += buffer.size(); |
| 313 | buffer.clear(); |
| 314 | } |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 315 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 316 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 317 | cfi_section->End(); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 318 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 319 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 320 | if (format == DW_EH_FRAME_FORMAT) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 321 | auto* header_section = builder->GetEhFrameHdr(); |
| 322 | header_section->Start(); |
| 323 | uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 324 | // Write .eh_frame_hdr section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 325 | std::vector<uint8_t> buffer; |
| 326 | Writer<> header(&buffer); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 327 | header.PushUint8(1); // Version. |
| 328 | // Encoding of .eh_frame pointer - libunwind does not honor datarel here, |
| 329 | // so we have to use pcrel which means relative to the pointer's location. |
| 330 | header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
| 331 | // Encoding of binary search table size. |
| 332 | header.PushUint8(DW_EH_PE_udata4); |
| 333 | // Encoding of binary search table addresses - libunwind supports only this |
| 334 | // specific combination, which means relative to the start of .eh_frame_hdr. |
| 335 | header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 336 | // .eh_frame pointer |
| 337 | header.PushInt32(cfi_section->GetAddress() - (header_address + 4u)); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 338 | // Binary search table size (number of entries). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 339 | header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2)); |
| 340 | header_section->WriteFully(buffer.data(), buffer.size()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 341 | // Binary search table. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 342 | for (size_t i = 0; i < binary_search_table.size(); i++) { |
| 343 | // Make addresses section-relative since we know the header address now. |
| 344 | binary_search_table[i] -= header_address; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 345 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 346 | header_section->WriteFully(binary_search_table.data(), binary_search_table.size()); |
| 347 | header_section->End(); |
| 348 | } else { |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 349 | if (write_oat_patches) { |
| 350 | builder->WritePatches(".debug_frame.oat_patches", |
| 351 | ArrayRef<const uintptr_t>(patch_locations)); |
| 352 | } |
David Srbecky | 033d745 | 2015-04-30 19:57:35 +0100 | [diff] [blame] | 353 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 354 | } |
| 355 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 356 | namespace { |
| 357 | struct CompilationUnit { |
| 358 | std::vector<const MethodDebugInfo*> methods_; |
| 359 | size_t debug_line_offset_ = 0; |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 360 | uintptr_t low_pc_ = std::numeric_limits<uintptr_t>::max(); |
| 361 | uintptr_t high_pc_ = 0; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 364 | typedef std::vector<DexFile::LocalInfo> LocalInfos; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 365 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 366 | void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) { |
| 367 | static_cast<LocalInfos*>(ctx)->push_back(entry); |
| 368 | } |
| 369 | |
| 370 | typedef std::vector<DexFile::PositionInfo> PositionInfos; |
| 371 | |
| 372 | bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { |
| 373 | static_cast<PositionInfos*>(ctx)->push_back(entry); |
| 374 | return false; |
| 375 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 376 | |
| 377 | std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) { |
| 378 | std::vector<const char*> names; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 379 | if (mi->code_item_ != nullptr) { |
| 380 | const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_); |
| 381 | if (stream != nullptr) { |
| 382 | DecodeUnsignedLeb128(&stream); // line. |
| 383 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 384 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 385 | uint32_t id = DecodeUnsignedLeb128P1(&stream); |
| 386 | names.push_back(mi->dex_file_->StringDataByIdx(id)); |
| 387 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | return names; |
| 391 | } |
| 392 | |
| 393 | struct VariableLocation { |
| 394 | uint32_t low_pc; |
| 395 | uint32_t high_pc; |
| 396 | DexRegisterLocation reg_lo; // May be None if the location is unknown. |
| 397 | DexRegisterLocation reg_hi; // Most significant bits of 64-bit value. |
| 398 | }; |
| 399 | |
| 400 | // Get the location of given dex register (e.g. stack or machine register). |
| 401 | // Note that the location might be different based on the current pc. |
| 402 | // The result will cover all ranges where the variable is in scope. |
| 403 | std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info, |
| 404 | uint16_t vreg, |
| 405 | bool is64bitValue, |
| 406 | uint32_t dex_pc_low, |
| 407 | uint32_t dex_pc_high) { |
| 408 | std::vector<VariableLocation> variable_locations; |
| 409 | |
| 410 | // Get stack maps sorted by pc (they might not be sorted internally). |
| 411 | const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data()); |
| 412 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 413 | std::map<uint32_t, StackMap> stack_maps; |
| 414 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 415 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 416 | DCHECK(stack_map.IsValid()); |
| 417 | const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding); |
| 418 | DCHECK_LE(low_pc, method_info->high_pc_); |
| 419 | stack_maps.emplace(low_pc, stack_map); |
| 420 | } |
| 421 | |
| 422 | // Create entries for the requested register based on stack map data. |
| 423 | for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) { |
| 424 | const StackMap& stack_map = it->second; |
| 425 | const uint32_t low_pc = it->first; |
| 426 | auto next_it = it; |
| 427 | next_it++; |
| 428 | const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first |
| 429 | : method_info->high_pc_; |
| 430 | DCHECK_LE(low_pc, high_pc); |
| 431 | if (low_pc == high_pc) { |
| 432 | continue; // Ignore if the address range is empty. |
| 433 | } |
| 434 | |
| 435 | // Check that the stack map is in the requested range. |
| 436 | uint32_t dex_pc = stack_map.GetDexPc(encoding); |
| 437 | if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) { |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | // Find the location of the dex register. |
| 442 | DexRegisterLocation reg_lo = DexRegisterLocation::None(); |
| 443 | DexRegisterLocation reg_hi = DexRegisterLocation::None(); |
| 444 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 445 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf( |
| 446 | stack_map, encoding, method_info->code_item_->registers_size_); |
| 447 | reg_lo = dex_register_map.GetDexRegisterLocation( |
| 448 | vreg, method_info->code_item_->registers_size_, code_info, encoding); |
| 449 | if (is64bitValue) { |
| 450 | reg_hi = dex_register_map.GetDexRegisterLocation( |
| 451 | vreg + 1, method_info->code_item_->registers_size_, code_info, encoding); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // Add location entry for this address range. |
| 456 | if (!variable_locations.empty() && |
| 457 | variable_locations.back().reg_lo == reg_lo && |
| 458 | variable_locations.back().reg_hi == reg_hi && |
| 459 | variable_locations.back().high_pc == low_pc) { |
| 460 | // Merge with the previous entry (extend its range). |
| 461 | variable_locations.back().high_pc = high_pc; |
| 462 | } else { |
| 463 | variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi}); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return variable_locations; |
| 468 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 469 | |
| 470 | bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) { |
| 471 | return method_info->compiled_method_->GetQuickCode().size() > 0 && |
| 472 | method_info->compiled_method_->GetVmapTable().size() > 0 && |
| 473 | method_info->compiled_method_->GetGcMap().size() == 0 && |
| 474 | method_info->code_item_ != nullptr; |
| 475 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 476 | } // namespace |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 477 | |
| 478 | // Helper class to write .debug_info and its supporting sections. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 479 | template<typename ElfTypes> |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 480 | class DebugInfoWriter { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 481 | typedef typename ElfTypes::Addr Elf_Addr; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 482 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 483 | // Helper class to write one compilation unit. |
| 484 | // It holds helper methods and temporary state. |
| 485 | class CompilationUnitWriter { |
| 486 | public: |
| 487 | explicit CompilationUnitWriter(DebugInfoWriter* owner) |
| 488 | : owner_(owner), |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 489 | info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void Write(const CompilationUnit& compilation_unit) { |
| 493 | CHECK(!compilation_unit.methods_.empty()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 494 | const Elf_Addr text_address = owner_->builder_->GetText()->Exists() |
| 495 | ? owner_->builder_->GetText()->GetAddress() |
| 496 | : 0; |
| 497 | const uintptr_t cu_size = compilation_unit.high_pc_ - compilation_unit.low_pc_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 498 | |
| 499 | info_.StartTag(DW_TAG_compile_unit); |
| 500 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 501 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
Tamas Berghammer | 8c55712 | 2015-12-10 15:06:25 +0000 | [diff] [blame] | 502 | info_.WriteStrp(DW_AT_comp_dir, owner_->WriteString("$JAVA_SRC_ROOT")); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 503 | info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 504 | info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size)); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 505 | info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 506 | |
| 507 | const char* last_dex_class_desc = nullptr; |
| 508 | for (auto mi : compilation_unit.methods_) { |
| 509 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 510 | const DexFile::CodeItem* dex_code = mi->code_item_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 511 | const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_); |
| 512 | const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method); |
| 513 | const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto); |
| 514 | const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 515 | const bool is_static = (mi->access_flags_ & kAccStatic) != 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 516 | |
| 517 | // Enclose the method in correct class definition. |
| 518 | if (last_dex_class_desc != dex_class_desc) { |
| 519 | if (last_dex_class_desc != nullptr) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 520 | EndClassTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 521 | } |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 522 | // Write reference tag for the class we are about to declare. |
| 523 | size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type); |
| 524 | type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset); |
| 525 | size_t type_attrib_offset = info_.size(); |
| 526 | info_.WriteRef4(DW_AT_type, 0); |
| 527 | info_.EndTag(); |
| 528 | // Declare the class that owns this method. |
| 529 | size_t class_offset = StartClassTag(dex_class_desc); |
| 530 | info_.UpdateUint32(type_attrib_offset, class_offset); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 531 | info_.WriteFlagPresent(DW_AT_declaration); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 532 | // Check that each class is defined only once. |
| 533 | bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second; |
| 534 | CHECK(unique) << "Redefinition of " << dex_class_desc; |
| 535 | last_dex_class_desc = dex_class_desc; |
| 536 | } |
| 537 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 538 | int start_depth = info_.Depth(); |
| 539 | info_.StartTag(DW_TAG_subprogram); |
| 540 | WriteName(dex->GetMethodName(dex_method)); |
| 541 | info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 542 | info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_)); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 543 | std::vector<uint8_t> expr_buffer; |
| 544 | Expression expr(&expr_buffer); |
| 545 | expr.WriteOpCallFrameCfa(); |
| 546 | info_.WriteExprLoc(DW_AT_frame_base, expr); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 547 | WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto)); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 548 | |
| 549 | // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not |
| 550 | // guarantee order or uniqueness so it is safer to iterate over them manually. |
| 551 | // DecodeDebugLocalInfo might not also be available if there is no debug info. |
| 552 | std::vector<const char*> param_names = GetParamNames(mi); |
| 553 | uint32_t arg_reg = 0; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 554 | if (!is_static) { |
| 555 | info_.StartTag(DW_TAG_formal_parameter); |
| 556 | WriteName("this"); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 557 | info_.WriteFlagPresent(DW_AT_artificial); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 558 | WriteLazyType(dex_class_desc); |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 559 | if (dex_code != nullptr) { |
| 560 | // Write the stack location of the parameter. |
| 561 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 562 | const bool is64bitValue = false; |
| 563 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 564 | } |
| 565 | arg_reg++; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 566 | info_.EndTag(); |
| 567 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 568 | if (dex_params != nullptr) { |
| 569 | for (uint32_t i = 0; i < dex_params->Size(); ++i) { |
| 570 | info_.StartTag(DW_TAG_formal_parameter); |
| 571 | // Parameter names may not be always available. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 572 | if (i < param_names.size()) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 573 | WriteName(param_names[i]); |
| 574 | } |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 575 | // Write the type. |
| 576 | const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_); |
| 577 | WriteLazyType(type_desc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 578 | const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J'; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 579 | if (dex_code != nullptr) { |
| 580 | // Write the stack location of the parameter. |
| 581 | const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg; |
| 582 | WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_); |
| 583 | } |
| 584 | arg_reg += is64bitValue ? 2 : 1; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 585 | info_.EndTag(); |
| 586 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 587 | if (dex_code != nullptr) { |
| 588 | DCHECK_EQ(arg_reg, dex_code->ins_size_); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 589 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 590 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 591 | |
| 592 | // Write local variables. |
| 593 | LocalInfos local_infos; |
| 594 | if (dex->DecodeDebugLocalInfo(dex_code, |
| 595 | is_static, |
| 596 | mi->dex_method_index_, |
| 597 | LocalInfoCallback, |
| 598 | &local_infos)) { |
| 599 | for (const DexFile::LocalInfo& var : local_infos) { |
| 600 | if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) { |
| 601 | info_.StartTag(DW_TAG_variable); |
| 602 | WriteName(var.name_); |
| 603 | WriteLazyType(var.descriptor_); |
| 604 | bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J'; |
| 605 | WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc_, |
| 606 | var.start_address_, var.end_address_); |
| 607 | info_.EndTag(); |
| 608 | } |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 611 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 612 | info_.EndTag(); |
| 613 | CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end. |
| 614 | } |
| 615 | if (last_dex_class_desc != nullptr) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 616 | EndClassTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 617 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 618 | FinishLazyTypes(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 619 | CloseNamespacesAboveDepth(0); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 620 | info_.EndTag(); // DW_TAG_compile_unit |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 621 | CHECK_EQ(info_.Depth(), 0); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 622 | std::vector<uint8_t> buffer; |
| 623 | buffer.reserve(info_.data()->size() + KB); |
| 624 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 625 | // All compilation units share single table which is at the start of .debug_abbrev. |
| 626 | const size_t debug_abbrev_offset = 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 627 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 628 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 629 | } |
| 630 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 631 | void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 632 | info_.StartTag(DW_TAG_compile_unit); |
| 633 | info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat")); |
| 634 | info_.WriteData1(DW_AT_language, DW_LANG_Java); |
| 635 | |
David Srbecky | c7eecf9 | 2016-02-01 14:53:48 +0000 | [diff] [blame] | 636 | // Base class references to be patched at the end. |
| 637 | std::map<size_t, mirror::Class*> base_class_references; |
| 638 | |
| 639 | // Already written declarations or definitions. |
| 640 | std::map<mirror::Class*, size_t> class_declarations; |
| 641 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 642 | std::vector<uint8_t> expr_buffer; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 643 | for (mirror::Class* type : types) { |
| 644 | if (type->IsPrimitive()) { |
| 645 | // For primitive types the definition and the declaration is the same. |
| 646 | if (type->GetPrimitiveType() != Primitive::kPrimVoid) { |
| 647 | WriteTypeDeclaration(type->GetDescriptor(nullptr)); |
| 648 | } |
| 649 | } else if (type->IsArrayClass()) { |
| 650 | mirror::Class* element_type = type->GetComponentType(); |
| 651 | uint32_t component_size = type->GetComponentSize(); |
| 652 | uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value(); |
| 653 | uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value(); |
| 654 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 655 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 656 | info_.StartTag(DW_TAG_array_type); |
| 657 | std::string descriptor_string; |
| 658 | WriteLazyType(element_type->GetDescriptor(&descriptor_string)); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 659 | WriteLinkageName(type); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 660 | info_.WriteUdata(DW_AT_data_member_location, data_offset); |
| 661 | info_.StartTag(DW_TAG_subrange_type); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 662 | Expression count_expr(&expr_buffer); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 663 | count_expr.WriteOpPushObjectAddress(); |
| 664 | count_expr.WriteOpPlusUconst(length_offset); |
| 665 | count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide. |
| 666 | info_.WriteExprLoc(DW_AT_count, count_expr); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 667 | info_.EndTag(); // DW_TAG_subrange_type. |
| 668 | info_.EndTag(); // DW_TAG_array_type. |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 669 | } else if (type->IsInterface()) { |
| 670 | // Skip. Variables cannot have an interface as a dynamic type. |
| 671 | // We do not expose the interface information to the debugger in any way. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 672 | } else { |
| 673 | std::string descriptor_string; |
| 674 | const char* desc = type->GetDescriptor(&descriptor_string); |
David Srbecky | c7eecf9 | 2016-02-01 14:53:48 +0000 | [diff] [blame] | 675 | size_t class_offset = StartClassTag(desc); |
| 676 | class_declarations.emplace(type, class_offset); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 677 | |
| 678 | if (!type->IsVariableSize()) { |
| 679 | info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize()); |
| 680 | } |
| 681 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 682 | WriteLinkageName(type); |
| 683 | |
| 684 | if (type->IsObjectClass()) { |
| 685 | // Generate artificial member which is used to get the dynamic type of variable. |
| 686 | // The run-time value of this field will correspond to linkage name of some type. |
| 687 | // We need to do it only once in j.l.Object since all other types inherit it. |
| 688 | info_.StartTag(DW_TAG_member); |
| 689 | WriteName(".dynamic_type"); |
| 690 | WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I"); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 691 | info_.WriteFlagPresent(DW_AT_artificial); |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 692 | // Create DWARF expression to get the value of the methods_ field. |
| 693 | Expression expr(&expr_buffer); |
| 694 | // The address of the object has been implicitly pushed on the stack. |
| 695 | // Dereference the klass_ field of Object (32-bit; possibly poisoned). |
| 696 | DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u); |
| 697 | DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u); |
| 698 | expr.WriteOpDerefSize(4); |
| 699 | if (kPoisonHeapReferences) { |
| 700 | expr.WriteOpNeg(); |
| 701 | // DWARF stack is pointer sized. Ensure that the high bits are clear. |
| 702 | expr.WriteOpConstu(0xFFFFFFFF); |
| 703 | expr.WriteOpAnd(); |
| 704 | } |
| 705 | // Add offset to the methods_ field. |
| 706 | expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value()); |
| 707 | // Top of stack holds the location of the field now. |
| 708 | info_.WriteExprLoc(DW_AT_data_member_location, expr); |
| 709 | info_.EndTag(); // DW_TAG_member. |
| 710 | } |
| 711 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 712 | // Base class. |
David Srbecky | c7eecf9 | 2016-02-01 14:53:48 +0000 | [diff] [blame] | 713 | mirror::Class* base_class = type->GetSuperClass(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 714 | if (base_class != nullptr) { |
| 715 | info_.StartTag(DW_TAG_inheritance); |
David Srbecky | c7eecf9 | 2016-02-01 14:53:48 +0000 | [diff] [blame] | 716 | base_class_references.emplace(info_.size(), base_class); |
| 717 | info_.WriteRef4(DW_AT_type, 0); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 718 | info_.WriteUdata(DW_AT_data_member_location, 0); |
| 719 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 720 | info_.EndTag(); // DW_TAG_inheritance. |
| 721 | } |
| 722 | |
| 723 | // Member variables. |
| 724 | for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) { |
| 725 | ArtField* field = type->GetInstanceField(i); |
| 726 | info_.StartTag(DW_TAG_member); |
| 727 | WriteName(field->GetName()); |
| 728 | WriteLazyType(field->GetTypeDescriptor()); |
| 729 | info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value()); |
| 730 | uint32_t access_flags = field->GetAccessFlags(); |
| 731 | if (access_flags & kAccPublic) { |
| 732 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public); |
| 733 | } else if (access_flags & kAccProtected) { |
| 734 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected); |
| 735 | } else if (access_flags & kAccPrivate) { |
| 736 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 737 | } |
| 738 | info_.EndTag(); // DW_TAG_member. |
| 739 | } |
| 740 | |
Tamas Berghammer | 03c941f | 2016-01-15 13:39:57 +0000 | [diff] [blame] | 741 | if (type->IsStringClass()) { |
| 742 | // Emit debug info about an artifical class member for java.lang.String which represents |
| 743 | // the first element of the data stored in a string instance. Consumers of the debug |
| 744 | // info will be able to read the content of java.lang.String based on the count (real |
| 745 | // field) and based on the location of this data member. |
| 746 | info_.StartTag(DW_TAG_member); |
| 747 | WriteName("value"); |
| 748 | // We don't support fields with C like array types so we just say its type is java char. |
| 749 | WriteLazyType("C"); // char. |
| 750 | info_.WriteUdata(DW_AT_data_member_location, |
| 751 | mirror::String::ValueOffset().Uint32Value()); |
| 752 | info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private); |
| 753 | info_.EndTag(); // DW_TAG_member. |
| 754 | } |
| 755 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 756 | EndClassTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
David Srbecky | c7eecf9 | 2016-02-01 14:53:48 +0000 | [diff] [blame] | 760 | // Write base class declarations. |
| 761 | for (const auto& base_class_reference : base_class_references) { |
| 762 | size_t reference_offset = base_class_reference.first; |
| 763 | mirror::Class* base_class = base_class_reference.second; |
| 764 | const auto& it = class_declarations.find(base_class); |
| 765 | if (it != class_declarations.end()) { |
| 766 | info_.UpdateUint32(reference_offset, it->second); |
| 767 | } else { |
| 768 | // Declare base class. We can not use the standard WriteLazyType |
| 769 | // since we want to avoid the DW_TAG_reference_tag wrapping. |
| 770 | std::string tmp_storage; |
| 771 | const char* base_class_desc = base_class->GetDescriptor(&tmp_storage); |
| 772 | size_t base_class_declaration_offset = StartClassTag(base_class_desc); |
| 773 | info_.WriteFlagPresent(DW_AT_declaration); |
| 774 | WriteLinkageName(base_class); |
| 775 | EndClassTag(); |
| 776 | class_declarations.emplace(base_class, base_class_declaration_offset); |
| 777 | info_.UpdateUint32(reference_offset, base_class_declaration_offset); |
| 778 | } |
| 779 | } |
| 780 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 781 | FinishLazyTypes(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 782 | CloseNamespacesAboveDepth(0); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 783 | info_.EndTag(); // DW_TAG_compile_unit. |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 784 | CHECK_EQ(info_.Depth(), 0); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 785 | std::vector<uint8_t> buffer; |
| 786 | buffer.reserve(info_.data()->size() + KB); |
| 787 | const size_t offset = owner_->builder_->GetDebugInfo()->GetSize(); |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 788 | // All compilation units share single table which is at the start of .debug_abbrev. |
| 789 | const size_t debug_abbrev_offset = 0; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 790 | WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_); |
| 791 | owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size()); |
| 792 | } |
| 793 | |
David Srbecky | 9b3607d | 2016-01-14 18:15:54 +0000 | [diff] [blame] | 794 | // Linkage name uniquely identifies type. |
| 795 | // It is used to determine the dynamic type of objects. |
| 796 | // We use the methods_ field of class since it is unique and it is not moved by the GC. |
| 797 | void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 798 | auto* methods_ptr = type->GetMethodsPtr(); |
| 799 | if (methods_ptr == nullptr) { |
| 800 | // Some types might have no methods. Allocate empty array instead. |
| 801 | LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc(); |
| 802 | void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>)); |
| 803 | methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0); |
| 804 | type->SetMethodsPtr(methods_ptr, 0, 0); |
| 805 | DCHECK(type->GetMethodsPtr() != nullptr); |
| 806 | } |
| 807 | char name[32]; |
| 808 | snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr)); |
| 809 | info_.WriteString(DW_AT_linkage_name, name); |
| 810 | } |
| 811 | |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 812 | // Write table into .debug_loc which describes location of dex register. |
| 813 | // The dex register might be valid only at some points and it might |
| 814 | // move between machine registers and stack. |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 815 | void WriteRegLocation(const MethodDebugInfo* method_info, |
| 816 | uint16_t vreg, |
| 817 | bool is64bitValue, |
| 818 | uint32_t compilation_unit_low_pc, |
| 819 | uint32_t dex_pc_low = 0, |
| 820 | uint32_t dex_pc_high = 0xFFFFFFFF) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 821 | using Kind = DexRegisterLocation::Kind; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 822 | if (!IsFromOptimizingCompiler(method_info)) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 823 | return; |
| 824 | } |
| 825 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 826 | Writer<> debug_loc(&owner_->debug_loc_); |
| 827 | Writer<> debug_ranges(&owner_->debug_ranges_); |
| 828 | info_.WriteSecOffset(DW_AT_location, debug_loc.size()); |
| 829 | info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 830 | |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 831 | std::vector<VariableLocation> variable_locations = GetVariableLocations( |
| 832 | method_info, |
| 833 | vreg, |
| 834 | is64bitValue, |
| 835 | dex_pc_low, |
| 836 | dex_pc_high); |
| 837 | |
| 838 | // Write .debug_loc entries. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 839 | const InstructionSet isa = owner_->builder_->GetIsa(); |
| 840 | const bool is64bit = Is64BitInstructionSet(isa); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 841 | std::vector<uint8_t> expr_buffer; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 842 | for (const VariableLocation& variable_location : variable_locations) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 843 | // Translate dex register location to DWARF expression. |
| 844 | // Note that 64-bit value might be split to two distinct locations. |
| 845 | // (for example, two 32-bit machine registers, or even stack and register) |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 846 | Expression expr(&expr_buffer); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 847 | DexRegisterLocation reg_lo = variable_location.reg_lo; |
| 848 | DexRegisterLocation reg_hi = variable_location.reg_hi; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 849 | for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) { |
| 850 | DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi); |
| 851 | const Kind kind = reg_loc.GetKind(); |
| 852 | const int32_t value = reg_loc.GetValue(); |
| 853 | if (kind == Kind::kInStack) { |
| 854 | const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes(); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 855 | // The stack offset is relative to SP. Make it relative to CFA. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 856 | expr.WriteOpFbreg(value - frame_size); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 857 | if (piece == 0 && reg_hi.GetKind() == Kind::kInStack && |
| 858 | reg_hi.GetValue() == value + 4) { |
| 859 | break; // the high word is correctly implied by the low word. |
| 860 | } |
| 861 | } else if (kind == Kind::kInRegister) { |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 862 | expr.WriteOpReg(GetDwarfCoreReg(isa, value).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 863 | if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh && |
| 864 | reg_hi.GetValue() == value) { |
| 865 | break; // the high word is correctly implied by the low word. |
| 866 | } |
| 867 | } else if (kind == Kind::kInFpuRegister) { |
| 868 | if ((isa == kArm || isa == kThumb2) && |
| 869 | piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister && |
| 870 | reg_hi.GetValue() == value + 1 && value % 2 == 0) { |
| 871 | // Translate S register pair to D register (e.g. S4+S5 to D2). |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 872 | expr.WriteOpReg(Reg::ArmDp(value / 2).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 873 | break; |
| 874 | } |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 875 | expr.WriteOpReg(GetDwarfFpReg(isa, value).num()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 876 | if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh && |
| 877 | reg_hi.GetValue() == reg_lo.GetValue()) { |
| 878 | break; // the high word is correctly implied by the low word. |
| 879 | } |
| 880 | } else if (kind == Kind::kConstant) { |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 881 | expr.WriteOpConsts(value); |
| 882 | expr.WriteOpStackValue(); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 883 | } else if (kind == Kind::kNone) { |
| 884 | break; |
| 885 | } else { |
| 886 | // kInStackLargeOffset and kConstantLargeValue are hidden by GetKind(). |
| 887 | // kInRegisterHigh and kInFpuRegisterHigh should be handled by |
| 888 | // the special cases above and they should not occur alone. |
| 889 | LOG(ERROR) << "Unexpected register location kind: " |
| 890 | << DexRegisterLocation::PrettyDescriptor(kind); |
| 891 | break; |
| 892 | } |
| 893 | if (is64bitValue) { |
| 894 | // Write the marker which is needed by split 64-bit values. |
| 895 | // This code is skipped by the special cases. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 896 | expr.WriteOpPiece(4); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 900 | if (expr.size() > 0) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 901 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 902 | debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc); |
| 903 | debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 904 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 905 | debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc); |
| 906 | debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 907 | } |
| 908 | // Write the expression. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 909 | debug_loc.PushUint16(expr.size()); |
| 910 | debug_loc.PushData(expr.data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 911 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 912 | // Do not generate .debug_loc if the location is not known. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | // Write end-of-list entry. |
| 916 | if (is64bit) { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 917 | debug_loc.PushUint64(0); |
| 918 | debug_loc.PushUint64(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 919 | } else { |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 920 | debug_loc.PushUint32(0); |
| 921 | debug_loc.PushUint32(0); |
| 922 | } |
| 923 | |
| 924 | // Write .debug_ranges entries. |
| 925 | // This includes ranges where the variable is in scope but the location is not known. |
| 926 | for (size_t i = 0; i < variable_locations.size(); i++) { |
| 927 | uint32_t low_pc = variable_locations[i].low_pc; |
| 928 | uint32_t high_pc = variable_locations[i].high_pc; |
| 929 | while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) { |
| 930 | // Merge address range with the next entry. |
| 931 | high_pc = variable_locations[++i].high_pc; |
| 932 | } |
| 933 | if (is64bit) { |
| 934 | debug_ranges.PushUint64(low_pc - compilation_unit_low_pc); |
| 935 | debug_ranges.PushUint64(high_pc - compilation_unit_low_pc); |
| 936 | } else { |
| 937 | debug_ranges.PushUint32(low_pc - compilation_unit_low_pc); |
| 938 | debug_ranges.PushUint32(high_pc - compilation_unit_low_pc); |
| 939 | } |
| 940 | } |
| 941 | // Write end-of-list entry. |
| 942 | if (is64bit) { |
| 943 | debug_ranges.PushUint64(0); |
| 944 | debug_ranges.PushUint64(0); |
| 945 | } else { |
| 946 | debug_ranges.PushUint32(0); |
| 947 | debug_ranges.PushUint32(0); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 951 | // Some types are difficult to define as we go since they need |
| 952 | // to be enclosed in the right set of namespaces. Therefore we |
| 953 | // just define all types lazily at the end of compilation unit. |
| 954 | void WriteLazyType(const char* type_descriptor) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 955 | if (type_descriptor != nullptr && type_descriptor[0] != 'V') { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 956 | lazy_types_.emplace(std::string(type_descriptor), info_.size()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 957 | info_.WriteRef4(DW_AT_type, 0); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void FinishLazyTypes() { |
| 962 | for (const auto& lazy_type : lazy_types_) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 963 | info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first)); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 964 | } |
| 965 | lazy_types_.clear(); |
| 966 | } |
| 967 | |
| 968 | private: |
| 969 | void WriteName(const char* name) { |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 970 | if (name != nullptr) { |
| 971 | info_.WriteStrp(DW_AT_name, owner_->WriteString(name)); |
| 972 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | // Convert dex type descriptor to DWARF. |
| 976 | // Returns offset in the compilation unit. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 977 | size_t WriteTypeDeclaration(const std::string& desc) { |
| 978 | DCHECK(!desc.empty()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 979 | const auto& it = type_cache_.find(desc); |
| 980 | if (it != type_cache_.end()) { |
| 981 | return it->second; |
| 982 | } |
| 983 | |
| 984 | size_t offset; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 985 | if (desc[0] == 'L') { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 986 | // Class type. For example: Lpackage/name; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 987 | size_t class_offset = StartClassTag(desc.c_str()); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 988 | info_.WriteFlagPresent(DW_AT_declaration); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 989 | EndClassTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 990 | // Reference to the class type. |
| 991 | offset = info_.StartTag(DW_TAG_reference_type); |
| 992 | info_.WriteRef(DW_AT_type, class_offset); |
| 993 | info_.EndTag(); |
| 994 | } else if (desc[0] == '[') { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 995 | // Array type. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 996 | size_t element_type = WriteTypeDeclaration(desc.substr(1)); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 997 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 998 | size_t array_type = info_.StartTag(DW_TAG_array_type); |
David Srbecky | fa5ec2b | 2016-01-29 15:13:19 +0000 | [diff] [blame] | 999 | info_.WriteFlagPresent(DW_AT_declaration); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1000 | info_.WriteRef(DW_AT_type, element_type); |
| 1001 | info_.EndTag(); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1002 | offset = info_.StartTag(DW_TAG_reference_type); |
| 1003 | info_.WriteRef4(DW_AT_type, array_type); |
| 1004 | info_.EndTag(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1005 | } else { |
| 1006 | // Primitive types. |
Tamas Berghammer | 03c941f | 2016-01-15 13:39:57 +0000 | [diff] [blame] | 1007 | DCHECK_EQ(desc.size(), 1u); |
| 1008 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1009 | const char* name; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1010 | uint32_t encoding; |
| 1011 | uint32_t byte_size; |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1012 | switch (desc[0]) { |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1013 | case 'B': |
| 1014 | name = "byte"; |
| 1015 | encoding = DW_ATE_signed; |
| 1016 | byte_size = 1; |
| 1017 | break; |
| 1018 | case 'C': |
| 1019 | name = "char"; |
| 1020 | encoding = DW_ATE_UTF; |
| 1021 | byte_size = 2; |
| 1022 | break; |
| 1023 | case 'D': |
| 1024 | name = "double"; |
| 1025 | encoding = DW_ATE_float; |
| 1026 | byte_size = 8; |
| 1027 | break; |
| 1028 | case 'F': |
| 1029 | name = "float"; |
| 1030 | encoding = DW_ATE_float; |
| 1031 | byte_size = 4; |
| 1032 | break; |
| 1033 | case 'I': |
| 1034 | name = "int"; |
| 1035 | encoding = DW_ATE_signed; |
| 1036 | byte_size = 4; |
| 1037 | break; |
| 1038 | case 'J': |
| 1039 | name = "long"; |
| 1040 | encoding = DW_ATE_signed; |
| 1041 | byte_size = 8; |
| 1042 | break; |
| 1043 | case 'S': |
| 1044 | name = "short"; |
| 1045 | encoding = DW_ATE_signed; |
| 1046 | byte_size = 2; |
| 1047 | break; |
| 1048 | case 'Z': |
| 1049 | name = "boolean"; |
| 1050 | encoding = DW_ATE_boolean; |
| 1051 | byte_size = 1; |
| 1052 | break; |
| 1053 | case 'V': |
| 1054 | LOG(FATAL) << "Void type should not be encoded"; |
| 1055 | UNREACHABLE(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1056 | default: |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1057 | LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\""; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1058 | UNREACHABLE(); |
| 1059 | } |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1060 | CloseNamespacesAboveDepth(0); // Declare in root namespace. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1061 | offset = info_.StartTag(DW_TAG_base_type); |
| 1062 | WriteName(name); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1063 | info_.WriteData1(DW_AT_encoding, encoding); |
| 1064 | info_.WriteData1(DW_AT_byte_size, byte_size); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1065 | info_.EndTag(); |
| 1066 | } |
| 1067 | |
| 1068 | type_cache_.emplace(desc, offset); |
| 1069 | return offset; |
| 1070 | } |
| 1071 | |
| 1072 | // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags. |
| 1073 | // Returns offset of the class tag in the compilation unit. |
| 1074 | size_t StartClassTag(const char* desc) { |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1075 | std::string name = SetNamespaceForClass(desc); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1076 | size_t offset = info_.StartTag(DW_TAG_class_type); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1077 | WriteName(name.c_str()); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1078 | return offset; |
| 1079 | } |
| 1080 | |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1081 | void EndClassTag() { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1082 | info_.EndTag(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Set the current namespace nesting to one required by the given class. |
| 1086 | // Returns the class name with namespaces, 'L', and ';' stripped. |
| 1087 | std::string SetNamespaceForClass(const char* desc) { |
| 1088 | DCHECK(desc != nullptr && desc[0] == 'L'); |
| 1089 | desc++; // Skip the initial 'L'. |
| 1090 | size_t depth = 0; |
| 1091 | for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) { |
| 1092 | // Check whether the name at this depth is already what we need. |
| 1093 | if (depth < current_namespace_.size()) { |
| 1094 | const std::string& name = current_namespace_[depth]; |
| 1095 | if (name.compare(0, name.size(), desc, end - desc) == 0) { |
| 1096 | continue; |
| 1097 | } |
| 1098 | } |
| 1099 | // Otherwise we need to open a new namespace tag at this depth. |
| 1100 | CloseNamespacesAboveDepth(depth); |
| 1101 | info_.StartTag(DW_TAG_namespace); |
| 1102 | std::string name(desc, end - desc); |
| 1103 | WriteName(name.c_str()); |
| 1104 | current_namespace_.push_back(std::move(name)); |
| 1105 | } |
| 1106 | CloseNamespacesAboveDepth(depth); |
| 1107 | return std::string(desc, strchr(desc, ';') - desc); |
| 1108 | } |
| 1109 | |
| 1110 | // Close namespace tags to reach the given nesting depth. |
| 1111 | void CloseNamespacesAboveDepth(size_t depth) { |
| 1112 | DCHECK_LE(depth, current_namespace_.size()); |
| 1113 | while (current_namespace_.size() > depth) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1114 | info_.EndTag(); |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1115 | current_namespace_.pop_back(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | // For access to the ELF sections. |
| 1120 | DebugInfoWriter<ElfTypes>* owner_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1121 | // Temporary buffer to create and store the entries. |
| 1122 | DebugInfoEntryWriter<> info_; |
| 1123 | // Cache of already translated type descriptors. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1124 | std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1125 | // 32-bit references which need to be resolved to a type later. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1126 | // Given type may be used multiple times. Therefore we need a multimap. |
| 1127 | std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset. |
David Srbecky | 7138d45 | 2016-01-29 15:02:44 +0000 | [diff] [blame] | 1128 | // The current set of open namespace tags which are active and not closed yet. |
| 1129 | std::vector<std::string> current_namespace_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1130 | }; |
| 1131 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1132 | public: |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 1133 | explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) |
| 1134 | : builder_(builder), |
| 1135 | debug_abbrev_(&debug_abbrev_buffer_) { |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 1136 | } |
| 1137 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1138 | void Start() { |
| 1139 | builder_->GetDebugInfo()->Start(); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1142 | void WriteCompilationUnit(const CompilationUnit& compilation_unit) { |
| 1143 | CompilationUnitWriter writer(this); |
| 1144 | writer.Write(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1145 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1146 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1147 | void WriteTypes(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1148 | CompilationUnitWriter writer(this); |
| 1149 | writer.Write(types); |
| 1150 | } |
| 1151 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1152 | void End() { |
| 1153 | builder_->GetDebugInfo()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1154 | builder_->WritePatches(".debug_info.oat_patches", |
| 1155 | ArrayRef<const uintptr_t>(debug_info_patches_)); |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 1156 | builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1157 | builder_->WriteSection(".debug_str", &debug_str_.Data()); |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1158 | builder_->WriteSection(".debug_loc", &debug_loc_); |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 1159 | builder_->WriteSection(".debug_ranges", &debug_ranges_); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | private: |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1163 | size_t WriteString(const char* str) { |
| 1164 | return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1); |
| 1165 | } |
| 1166 | |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1167 | ElfBuilder<ElfTypes>* builder_; |
| 1168 | std::vector<uintptr_t> debug_info_patches_; |
David Srbecky | 24868a1 | 2016-01-29 18:59:56 +0000 | [diff] [blame^] | 1169 | std::vector<uint8_t> debug_abbrev_buffer_; |
| 1170 | DebugAbbrevWriter<> debug_abbrev_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1171 | DedupVector debug_str_; |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 1172 | std::vector<uint8_t> debug_loc_; |
David Srbecky | 996ed0b | 2015-11-27 10:27:11 +0000 | [diff] [blame] | 1173 | std::vector<uint8_t> debug_ranges_; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1174 | |
| 1175 | std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only. |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1176 | }; |
| 1177 | |
| 1178 | template<typename ElfTypes> |
| 1179 | class DebugLineWriter { |
| 1180 | typedef typename ElfTypes::Addr Elf_Addr; |
| 1181 | |
| 1182 | public: |
| 1183 | explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { |
| 1184 | } |
| 1185 | |
| 1186 | void Start() { |
| 1187 | builder_->GetDebugLine()->Start(); |
| 1188 | } |
| 1189 | |
| 1190 | // Write line table for given set of methods. |
| 1191 | // Returns the number of bytes written. |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1192 | size_t WriteCompilationUnit(CompilationUnit& compilation_unit) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1193 | const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1194 | const Elf_Addr text_address = builder_->GetText()->Exists() |
| 1195 | ? builder_->GetText()->GetAddress() |
| 1196 | : 0; |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1197 | |
| 1198 | compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1199 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1200 | std::vector<FileEntry> files; |
| 1201 | std::unordered_map<std::string, size_t> files_map; |
| 1202 | std::vector<std::string> directories; |
| 1203 | std::unordered_map<std::string, size_t> directories_map; |
| 1204 | int code_factor_bits_ = 0; |
| 1205 | int dwarf_isa = -1; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1206 | switch (builder_->GetIsa()) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1207 | case kArm: // arm actually means thumb2. |
| 1208 | case kThumb2: |
| 1209 | code_factor_bits_ = 1; // 16-bit instuctions |
| 1210 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 1211 | break; |
| 1212 | case kArm64: |
| 1213 | case kMips: |
| 1214 | case kMips64: |
| 1215 | code_factor_bits_ = 2; // 32-bit instructions |
| 1216 | break; |
| 1217 | case kNone: |
| 1218 | case kX86: |
| 1219 | case kX86_64: |
| 1220 | break; |
| 1221 | } |
David Srbecky | 297ed22 | 2015-04-15 01:18:12 +0100 | [diff] [blame] | 1222 | DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1223 | for (const MethodDebugInfo* mi : compilation_unit.methods_) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1224 | // Ignore function if we have already generated line table for the same address. |
| 1225 | // It would confuse the debugger and the DWARF specification forbids it. |
| 1226 | if (mi->deduped_) { |
| 1227 | continue; |
| 1228 | } |
| 1229 | |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1230 | ArrayRef<const SrcMapElem> src_mapping_table; |
| 1231 | std::vector<SrcMapElem> src_mapping_table_from_stack_maps; |
| 1232 | if (IsFromOptimizingCompiler(mi)) { |
| 1233 | // Use stack maps to create mapping table from pc to dex. |
| 1234 | const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data()); |
| 1235 | const StackMapEncoding encoding = code_info.ExtractEncoding(); |
| 1236 | for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) { |
| 1237 | StackMap stack_map = code_info.GetStackMapAt(s, encoding); |
| 1238 | DCHECK(stack_map.IsValid()); |
David Srbecky | 4f50ee2 | 2016-01-25 17:00:24 +0000 | [diff] [blame] | 1239 | // Emit only locations where we have local-variable information. |
| 1240 | // In particular, skip mappings inside the prologue. |
| 1241 | if (stack_map.HasDexRegisterMap(encoding)) { |
| 1242 | const uint32_t pc = stack_map.GetNativePcOffset(encoding); |
| 1243 | const int32_t dex = stack_map.GetDexPc(encoding); |
| 1244 | src_mapping_table_from_stack_maps.push_back({pc, dex}); |
| 1245 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1246 | } |
| 1247 | std::sort(src_mapping_table_from_stack_maps.begin(), |
| 1248 | src_mapping_table_from_stack_maps.end()); |
| 1249 | src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps); |
| 1250 | } else { |
| 1251 | // Use the mapping table provided by the quick compiler. |
| 1252 | src_mapping_table = mi->compiled_method_->GetSrcMappingTable(); |
| 1253 | } |
| 1254 | |
| 1255 | if (src_mapping_table.empty()) { |
| 1256 | continue; |
| 1257 | } |
| 1258 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1259 | Elf_Addr method_address = text_address + mi->low_pc_; |
| 1260 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1261 | PositionInfos position_infos; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1262 | const DexFile* dex = mi->dex_file_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1263 | if (!dex->DecodeDebugPositionInfo(mi->code_item_, PositionInfoCallback, &position_infos)) { |
| 1264 | continue; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1267 | if (position_infos.empty()) { |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1268 | continue; |
| 1269 | } |
| 1270 | |
| 1271 | opcodes.SetAddress(method_address); |
| 1272 | if (dwarf_isa != -1) { |
| 1273 | opcodes.SetISA(dwarf_isa); |
| 1274 | } |
| 1275 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1276 | // Get and deduplicate directory and filename. |
| 1277 | int file_index = 0; // 0 - primary source file of the compilation. |
| 1278 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index_); |
| 1279 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 1280 | if (source_file != nullptr) { |
| 1281 | std::string file_name(source_file); |
| 1282 | size_t file_name_slash = file_name.find_last_of('/'); |
| 1283 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 1284 | size_t class_name_slash = class_name.find_last_of('/'); |
| 1285 | std::string full_path(file_name); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1286 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1287 | // Guess directory from package name. |
| 1288 | int directory_index = 0; // 0 - current directory of the compilation. |
| 1289 | if (file_name_slash == std::string::npos && // Just filename. |
| 1290 | class_name.front() == 'L' && // Type descriptor for a class. |
| 1291 | class_name_slash != std::string::npos) { // Has package name. |
| 1292 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 1293 | auto it = directories_map.find(package_name); |
| 1294 | if (it == directories_map.end()) { |
| 1295 | directory_index = 1 + directories.size(); |
| 1296 | directories_map.emplace(package_name, directory_index); |
| 1297 | directories.push_back(package_name); |
| 1298 | } else { |
| 1299 | directory_index = it->second; |
| 1300 | } |
| 1301 | full_path = package_name + "/" + file_name; |
| 1302 | } |
| 1303 | |
| 1304 | // Add file entry. |
| 1305 | auto it2 = files_map.find(full_path); |
| 1306 | if (it2 == files_map.end()) { |
| 1307 | file_index = 1 + files.size(); |
| 1308 | files_map.emplace(full_path, file_index); |
| 1309 | files.push_back(FileEntry { |
| 1310 | file_name, |
| 1311 | directory_index, |
| 1312 | 0, // Modification time - NA. |
| 1313 | 0, // File size - NA. |
| 1314 | }); |
| 1315 | } else { |
| 1316 | file_index = it2->second; |
| 1317 | } |
| 1318 | } |
| 1319 | opcodes.SetFile(file_index); |
| 1320 | |
| 1321 | // Generate mapping opcodes from PC to Java lines. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1322 | if (file_index != 0) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1323 | bool first = true; |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1324 | for (SrcMapElem pc2dex : src_mapping_table) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1325 | uint32_t pc = pc2dex.from_; |
| 1326 | int dex_pc = pc2dex.to_; |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1327 | // Find mapping with address with is greater than our dex pc; then go back one step. |
| 1328 | auto ub = std::upper_bound(position_infos.begin(), position_infos.end(), dex_pc, |
| 1329 | [](uint32_t address, const DexFile::PositionInfo& entry) { |
| 1330 | return address < entry.address_; |
| 1331 | }); |
| 1332 | if (ub != position_infos.begin()) { |
| 1333 | int line = (--ub)->line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1334 | if (first) { |
| 1335 | first = false; |
| 1336 | if (pc > 0) { |
| 1337 | // Assume that any preceding code is prologue. |
David Srbecky | b06e28e | 2015-12-10 13:15:00 +0000 | [diff] [blame] | 1338 | int first_line = position_infos.front().line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1339 | // Prologue is not a sensible place for a breakpoint. |
| 1340 | opcodes.NegateStmt(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1341 | opcodes.AddRow(method_address, first_line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1342 | opcodes.NegateStmt(); |
| 1343 | opcodes.SetPrologueEnd(); |
| 1344 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1345 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1346 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1347 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1348 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1349 | } |
| 1350 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 1351 | } else { |
| 1352 | // line 0 - instruction cannot be attributed to any source line. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1353 | opcodes.AddRow(method_address, 0); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1354 | } |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 1355 | |
| 1356 | opcodes.AdvancePC(text_address + mi->high_pc_); |
| 1357 | opcodes.EndSequence(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1358 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1359 | std::vector<uint8_t> buffer; |
| 1360 | buffer.reserve(opcodes.data()->size() + KB); |
| 1361 | size_t offset = builder_->GetDebugLine()->GetSize(); |
| 1362 | WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches); |
| 1363 | builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); |
| 1364 | return buffer.size(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1365 | } |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1366 | |
| 1367 | void End() { |
| 1368 | builder_->GetDebugLine()->End(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1369 | builder_->WritePatches(".debug_line.oat_patches", |
| 1370 | ArrayRef<const uintptr_t>(debug_line_patches)); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | private: |
| 1374 | ElfBuilder<ElfTypes>* builder_; |
| 1375 | std::vector<uintptr_t> debug_line_patches; |
| 1376 | }; |
| 1377 | |
| 1378 | template<typename ElfTypes> |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1379 | static void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1380 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1381 | // Group the methods into compilation units based on source file. |
| 1382 | std::vector<CompilationUnit> compilation_units; |
| 1383 | const char* last_source_file = nullptr; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1384 | for (const MethodDebugInfo& mi : method_infos) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1385 | auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_); |
| 1386 | const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def); |
| 1387 | if (compilation_units.empty() || source_file != last_source_file) { |
| 1388 | compilation_units.push_back(CompilationUnit()); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1389 | } |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1390 | CompilationUnit& cu = compilation_units.back(); |
| 1391 | cu.methods_.push_back(&mi); |
| 1392 | cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_); |
| 1393 | cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_); |
| 1394 | last_source_file = source_file; |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | // Write .debug_line section. |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1398 | if (!compilation_units.empty()) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1399 | DebugLineWriter<ElfTypes> line_writer(builder); |
| 1400 | line_writer.Start(); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1401 | for (auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1402 | line_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1403 | } |
| 1404 | line_writer.End(); |
| 1405 | } |
| 1406 | |
| 1407 | // Write .debug_info section. |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1408 | if (!compilation_units.empty()) { |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1409 | DebugInfoWriter<ElfTypes> info_writer(builder); |
| 1410 | info_writer.Start(); |
| 1411 | for (const auto& compilation_unit : compilation_units) { |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 1412 | info_writer.WriteCompilationUnit(compilation_unit); |
David Srbecky | b851b49 | 2015-11-11 20:19:38 +0000 | [diff] [blame] | 1413 | } |
| 1414 | info_writer.End(); |
| 1415 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1418 | template <typename ElfTypes> |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1419 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 1420 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 1421 | bool with_signature) { |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1422 | bool generated_mapping_symbol = false; |
| 1423 | auto* strtab = builder->GetStrTab(); |
| 1424 | auto* symtab = builder->GetSymTab(); |
| 1425 | |
| 1426 | if (method_infos.empty()) { |
| 1427 | return; |
| 1428 | } |
| 1429 | |
| 1430 | // Find all addresses (low_pc) which contain deduped methods. |
| 1431 | // The first instance of method is not marked deduped_, but the rest is. |
| 1432 | std::unordered_set<uint32_t> deduped_addresses; |
| 1433 | for (const MethodDebugInfo& info : method_infos) { |
| 1434 | if (info.deduped_) { |
| 1435 | deduped_addresses.insert(info.low_pc_); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | strtab->Start(); |
| 1440 | strtab->Write(""); // strtab should start with empty string. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1441 | std::string last_name; |
| 1442 | size_t last_name_offset = 0; |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1443 | for (const MethodDebugInfo& info : method_infos) { |
| 1444 | if (info.deduped_) { |
| 1445 | continue; // Add symbol only for the first instance. |
| 1446 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1447 | std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, with_signature); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1448 | if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) { |
| 1449 | name += " [DEDUPED]"; |
| 1450 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1451 | // If we write method names without signature, we might see the same name multiple times. |
| 1452 | size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name)); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1453 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1454 | const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr; |
| 1455 | const bool is_relative = (text != nullptr); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1456 | uint32_t low_pc = info.low_pc_; |
| 1457 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 1458 | low_pc += info.compiled_method_->CodeDelta(); |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1459 | symtab->Add(name_offset, |
| 1460 | text, |
| 1461 | low_pc, |
| 1462 | is_relative, |
| 1463 | info.high_pc_ - info.low_pc_, |
| 1464 | STB_GLOBAL, |
| 1465 | STT_FUNC); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1466 | |
| 1467 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 1468 | // instructions, so that disassembler tools can correctly disassemble. |
| 1469 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 1470 | // requires it to match function symbol. Just address 0 does not work. |
| 1471 | if (info.compiled_method_->GetInstructionSet() == kThumb2) { |
| 1472 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1473 | symtab->Add(strtab->Write("$t"), text, info.low_pc_ & ~1, |
| 1474 | is_relative, 0, STB_LOCAL, STT_NOTYPE); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1475 | generated_mapping_symbol = true; |
| 1476 | } |
| 1477 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1478 | |
| 1479 | last_name = std::move(name); |
| 1480 | last_name_offset = name_offset; |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1481 | } |
| 1482 | strtab->End(); |
| 1483 | |
| 1484 | // Symbols are buffered and written after names (because they are smaller). |
| 1485 | // We could also do two passes in this function to avoid the buffering. |
| 1486 | symtab->Start(); |
| 1487 | symtab->Write(); |
| 1488 | symtab->End(); |
| 1489 | } |
| 1490 | |
| 1491 | template <typename ElfTypes> |
| 1492 | void WriteDebugInfo(ElfBuilder<ElfTypes>* builder, |
| 1493 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 1494 | CFIFormat cfi_format) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1495 | // Add methods to .symtab. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1496 | WriteDebugSymbols(builder, method_infos, true /* with_signature */); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1497 | // Generate CFI (stack unwinding information). |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 1498 | WriteCFISection(builder, method_infos, cfi_format, true /* write_oat_patches */); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1499 | // Write DWARF .debug_* sections. |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1500 | WriteDebugSections(builder, method_infos); |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1503 | static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) { |
| 1504 | // Configure the compression library. |
| 1505 | CrcGenerateTable(); |
| 1506 | Crc64GenerateTable(); |
| 1507 | CLzma2EncProps lzma2Props; |
| 1508 | Lzma2EncProps_Init(&lzma2Props); |
| 1509 | lzma2Props.lzmaProps.level = 1; // Fast compression. |
| 1510 | Lzma2EncProps_Normalize(&lzma2Props); |
| 1511 | CXzProps props; |
| 1512 | XzProps_Init(&props); |
| 1513 | props.lzma2Props = &lzma2Props; |
| 1514 | // Implement the required interface for communication (written in C so no virtual methods). |
| 1515 | struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress { |
| 1516 | static SRes ReadImpl(void* p, void* buf, size_t* size) { |
| 1517 | auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqInStream*>(p)); |
| 1518 | *size = std::min(*size, ctx->src_->size() - ctx->src_pos_); |
| 1519 | memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size); |
| 1520 | ctx->src_pos_ += *size; |
| 1521 | return SZ_OK; |
| 1522 | } |
| 1523 | static size_t WriteImpl(void* p, const void* buf, size_t size) { |
| 1524 | auto* ctx = static_cast<XzCallbacks*>(reinterpret_cast<ISeqOutStream*>(p)); |
| 1525 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf); |
| 1526 | ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size); |
| 1527 | return size; |
| 1528 | } |
| 1529 | static SRes ProgressImpl(void* , UInt64, UInt64) { |
| 1530 | return SZ_OK; |
| 1531 | } |
| 1532 | size_t src_pos_; |
| 1533 | const std::vector<uint8_t>* src_; |
| 1534 | std::vector<uint8_t>* dst_; |
| 1535 | }; |
| 1536 | XzCallbacks callbacks; |
| 1537 | callbacks.Read = XzCallbacks::ReadImpl; |
| 1538 | callbacks.Write = XzCallbacks::WriteImpl; |
| 1539 | callbacks.Progress = XzCallbacks::ProgressImpl; |
| 1540 | callbacks.src_pos_ = 0; |
| 1541 | callbacks.src_ = src; |
| 1542 | callbacks.dst_ = dst; |
| 1543 | // Compress. |
| 1544 | SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks); |
| 1545 | CHECK_EQ(res, SZ_OK); |
| 1546 | } |
| 1547 | |
| 1548 | template <typename ElfTypes> |
| 1549 | void WriteMiniDebugInfo(ElfBuilder<ElfTypes>* parent_builder, |
| 1550 | const ArrayRef<const MethodDebugInfo>& method_infos) { |
| 1551 | const InstructionSet isa = parent_builder->GetIsa(); |
| 1552 | std::vector<uint8_t> buffer; |
| 1553 | buffer.reserve(KB); |
| 1554 | VectorOutputStream out("Mini-debug-info ELF file", &buffer); |
| 1555 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1556 | builder->Start(); |
| 1557 | // Write .rodata and .text as NOBITS sections. |
| 1558 | // This allows tools to detect virtual address relocation of the parent ELF file. |
| 1559 | builder->SetVirtualAddress(parent_builder->GetRoData()->GetAddress()); |
| 1560 | builder->GetRoData()->WriteNoBitsSection(parent_builder->GetRoData()->GetSize()); |
| 1561 | builder->SetVirtualAddress(parent_builder->GetText()->GetAddress()); |
| 1562 | builder->GetText()->WriteNoBitsSection(parent_builder->GetText()->GetSize()); |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 1563 | WriteDebugSymbols(builder.get(), method_infos, false /* with_signature */); |
David Srbecky | 5e974a6 | 2016-01-22 14:25:03 +0000 | [diff] [blame] | 1564 | WriteCFISection(builder.get(), method_infos, DW_DEBUG_FRAME_FORMAT, false /* write_oat_paches */); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1565 | builder->End(); |
| 1566 | CHECK(builder->Good()); |
| 1567 | std::vector<uint8_t> compressed_buffer; |
| 1568 | compressed_buffer.reserve(buffer.size() / 4); |
| 1569 | XzCompress(&buffer, &compressed_buffer); |
| 1570 | parent_builder->WriteSection(".gnu_debugdata", &compressed_buffer); |
| 1571 | } |
| 1572 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1573 | template <typename ElfTypes> |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1574 | static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal( |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1575 | const dwarf::MethodDebugInfo& method_info) { |
| 1576 | const InstructionSet isa = method_info.compiled_method_->GetInstructionSet(); |
| 1577 | std::vector<uint8_t> buffer; |
| 1578 | buffer.reserve(KB); |
| 1579 | VectorOutputStream out("Debug ELF file", &buffer); |
| 1580 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1581 | builder->Start(); |
| 1582 | WriteDebugInfo(builder.get(), |
| 1583 | ArrayRef<const MethodDebugInfo>(&method_info, 1), |
| 1584 | DW_DEBUG_FRAME_FORMAT); |
| 1585 | builder->End(); |
| 1586 | CHECK(builder->Good()); |
| 1587 | // Make a copy of the buffer. We want to shrink it anyway. |
| 1588 | uint8_t* result = new uint8_t[buffer.size()]; |
| 1589 | CHECK(result != nullptr); |
| 1590 | memcpy(result, buffer.data(), buffer.size()); |
| 1591 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 1592 | } |
| 1593 | |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1594 | ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const dwarf::MethodDebugInfo& method_info) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1595 | const InstructionSet isa = method_info.compiled_method_->GetInstructionSet(); |
| 1596 | if (Is64BitInstructionSet(isa)) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1597 | return WriteDebugElfFileForMethodInternal<ElfTypes64>(method_info); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1598 | } else { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1599 | return WriteDebugElfFileForMethodInternal<ElfTypes32>(method_info); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | template <typename ElfTypes> |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1604 | static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal( |
| 1605 | const InstructionSet isa, const ArrayRef<mirror::Class*>& types) |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1606 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1607 | std::vector<uint8_t> buffer; |
| 1608 | buffer.reserve(KB); |
| 1609 | VectorOutputStream out("Debug ELF file", &buffer); |
| 1610 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 1611 | builder->Start(); |
| 1612 | |
| 1613 | DebugInfoWriter<ElfTypes> info_writer(builder.get()); |
| 1614 | info_writer.Start(); |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1615 | info_writer.WriteTypes(types); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1616 | info_writer.End(); |
| 1617 | |
| 1618 | builder->End(); |
| 1619 | CHECK(builder->Good()); |
| 1620 | // Make a copy of the buffer. We want to shrink it anyway. |
| 1621 | uint8_t* result = new uint8_t[buffer.size()]; |
| 1622 | CHECK(result != nullptr); |
| 1623 | memcpy(result, buffer.data(), buffer.size()); |
| 1624 | return ArrayRef<const uint8_t>(result, buffer.size()); |
| 1625 | } |
| 1626 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1627 | ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa, |
| 1628 | const ArrayRef<mirror::Class*>& types) { |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1629 | if (Is64BitInstructionSet(isa)) { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1630 | return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, types); |
Tamas Berghammer | 86e4278 | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 1631 | } else { |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 1632 | return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, types); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1636 | // Explicit instantiations |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1637 | template void WriteDebugInfo<ElfTypes32>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1638 | ElfBuilder<ElfTypes32>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1639 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1640 | CFIFormat cfi_format); |
| 1641 | template void WriteDebugInfo<ElfTypes64>( |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1642 | ElfBuilder<ElfTypes64>* builder, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 1643 | const ArrayRef<const MethodDebugInfo>& method_infos, |
David Srbecky | e0febdf | 2015-12-17 20:53:07 +0000 | [diff] [blame] | 1644 | CFIFormat cfi_format); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 1645 | template void WriteMiniDebugInfo<ElfTypes32>( |
| 1646 | ElfBuilder<ElfTypes32>* builder, |
| 1647 | const ArrayRef<const MethodDebugInfo>& method_infos); |
| 1648 | template void WriteMiniDebugInfo<ElfTypes64>( |
| 1649 | ElfBuilder<ElfTypes64>* builder, |
| 1650 | const ArrayRef<const MethodDebugInfo>& method_infos); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 1651 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1652 | } // namespace dwarf |
| 1653 | } // namespace art |