David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "elf_writer_debug.h" |
| 18 | |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 19 | #include <unordered_set> |
| 20 | |
Andreas Gampe | e3d623e | 2015-05-01 16:11:04 -0700 | [diff] [blame] | 21 | #include "base/casts.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 22 | #include "compiled_method.h" |
| 23 | #include "driver/compiler_driver.h" |
| 24 | #include "dex_file-inl.h" |
| 25 | #include "dwarf/headers.h" |
| 26 | #include "dwarf/register.h" |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 27 | #include "elf_builder.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 28 | #include "oat_writer.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | #include "utils.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace dwarf { |
| 33 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 34 | static void WriteCIE(InstructionSet isa, |
| 35 | CFIFormat format, |
| 36 | std::vector<uint8_t>* buffer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 37 | // Scratch registers should be marked as undefined. This tells the |
| 38 | // debugger that its value in the previous frame is not recoverable. |
| 39 | bool is64bit = Is64BitInstructionSet(isa); |
| 40 | switch (isa) { |
| 41 | case kArm: |
| 42 | case kThumb2: { |
| 43 | DebugFrameOpCodeWriter<> opcodes; |
| 44 | opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP). |
| 45 | // core registers. |
| 46 | for (int reg = 0; reg < 13; reg++) { |
| 47 | if (reg < 4 || reg == 12) { |
| 48 | opcodes.Undefined(Reg::ArmCore(reg)); |
| 49 | } else { |
| 50 | opcodes.SameValue(Reg::ArmCore(reg)); |
| 51 | } |
| 52 | } |
| 53 | // fp registers. |
| 54 | for (int reg = 0; reg < 32; reg++) { |
| 55 | if (reg < 16) { |
| 56 | opcodes.Undefined(Reg::ArmFp(reg)); |
| 57 | } else { |
| 58 | opcodes.SameValue(Reg::ArmFp(reg)); |
| 59 | } |
| 60 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 61 | auto return_reg = Reg::ArmCore(14); // R14(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 62 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | case kArm64: { |
| 66 | DebugFrameOpCodeWriter<> opcodes; |
| 67 | opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP). |
| 68 | // core registers. |
| 69 | for (int reg = 0; reg < 30; reg++) { |
| 70 | if (reg < 8 || reg == 16 || reg == 17) { |
| 71 | opcodes.Undefined(Reg::Arm64Core(reg)); |
| 72 | } else { |
| 73 | opcodes.SameValue(Reg::Arm64Core(reg)); |
| 74 | } |
| 75 | } |
| 76 | // fp registers. |
| 77 | for (int reg = 0; reg < 32; reg++) { |
| 78 | if (reg < 8 || reg >= 16) { |
| 79 | opcodes.Undefined(Reg::Arm64Fp(reg)); |
| 80 | } else { |
| 81 | opcodes.SameValue(Reg::Arm64Fp(reg)); |
| 82 | } |
| 83 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 84 | auto return_reg = Reg::Arm64Core(30); // R30(LR). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 85 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | case kMips: |
| 89 | case kMips64: { |
| 90 | DebugFrameOpCodeWriter<> opcodes; |
| 91 | opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP). |
| 92 | // core registers. |
| 93 | for (int reg = 1; reg < 26; reg++) { |
| 94 | if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*. |
| 95 | opcodes.Undefined(Reg::MipsCore(reg)); |
| 96 | } else { |
| 97 | opcodes.SameValue(Reg::MipsCore(reg)); |
| 98 | } |
| 99 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 100 | auto return_reg = Reg::MipsCore(31); // R31(RA). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 101 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | case kX86: { |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 105 | // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296 |
| 106 | constexpr bool generate_opcodes_for_x86_fp = false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 107 | DebugFrameOpCodeWriter<> opcodes; |
| 108 | opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP). |
| 109 | opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP). |
| 110 | // core registers. |
| 111 | for (int reg = 0; reg < 8; reg++) { |
| 112 | if (reg <= 3) { |
| 113 | opcodes.Undefined(Reg::X86Core(reg)); |
| 114 | } else if (reg == 4) { |
| 115 | // Stack pointer. |
| 116 | } else { |
| 117 | opcodes.SameValue(Reg::X86Core(reg)); |
| 118 | } |
| 119 | } |
| 120 | // fp registers. |
David Srbecky | 8a813f7 | 2015-04-20 16:43:52 +0100 | [diff] [blame] | 121 | if (generate_opcodes_for_x86_fp) { |
| 122 | for (int reg = 0; reg < 8; reg++) { |
| 123 | opcodes.Undefined(Reg::X86Fp(reg)); |
| 124 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 125 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 126 | auto return_reg = Reg::X86Core(8); // R8(EIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 127 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | case kX86_64: { |
| 131 | DebugFrameOpCodeWriter<> opcodes; |
| 132 | opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP). |
| 133 | opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP). |
| 134 | // core registers. |
| 135 | for (int reg = 0; reg < 16; reg++) { |
| 136 | if (reg == 4) { |
| 137 | // Stack pointer. |
| 138 | } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP. |
| 139 | opcodes.Undefined(Reg::X86_64Core(reg)); |
| 140 | } else { |
| 141 | opcodes.SameValue(Reg::X86_64Core(reg)); |
| 142 | } |
| 143 | } |
| 144 | // fp registers. |
| 145 | for (int reg = 0; reg < 16; reg++) { |
| 146 | if (reg < 12) { |
| 147 | opcodes.Undefined(Reg::X86_64Fp(reg)); |
| 148 | } else { |
| 149 | opcodes.SameValue(Reg::X86_64Fp(reg)); |
| 150 | } |
| 151 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 152 | auto return_reg = Reg::X86_64Core(16); // R16(RIP). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 153 | WriteCIE(is64bit, return_reg, opcodes, format, buffer); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | case kNone: |
| 157 | break; |
| 158 | } |
| 159 | LOG(FATAL) << "Can not write CIE frame for ISA " << isa; |
| 160 | UNREACHABLE(); |
| 161 | } |
| 162 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 163 | template<typename ElfTypes> |
| 164 | void WriteCFISection(ElfBuilder<ElfTypes>* builder, |
| 165 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 166 | CFIFormat format) { |
| 167 | CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT || |
| 168 | format == dwarf::DW_EH_FRAME_FORMAT); |
| 169 | typedef typename ElfTypes::Addr Elf_Addr; |
| 170 | |
| 171 | std::vector<uint32_t> binary_search_table; |
| 172 | std::vector<uintptr_t> patch_locations; |
| 173 | if (format == DW_EH_FRAME_FORMAT) { |
| 174 | binary_search_table.reserve(2 * method_infos.size()); |
| 175 | } else { |
| 176 | patch_locations.reserve(method_infos.size()); |
| 177 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 178 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 179 | // Write .eh_frame/.debug_frame section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 180 | auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT |
| 181 | ? builder->GetDebugFrame() |
| 182 | : builder->GetEhFrame()); |
| 183 | { |
| 184 | cfi_section->Start(); |
| 185 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
| 186 | const Elf_Addr text_address = builder->GetText()->GetAddress(); |
| 187 | const Elf_Addr cfi_address = cfi_section->GetAddress(); |
| 188 | const Elf_Addr cie_address = cfi_address; |
| 189 | Elf_Addr buffer_address = cfi_address; |
| 190 | std::vector<uint8_t> buffer; // Small temporary buffer. |
| 191 | WriteCIE(builder->GetIsa(), format, &buffer); |
| 192 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 193 | buffer_address += buffer.size(); |
| 194 | buffer.clear(); |
| 195 | for (const OatWriter::DebugInfo& mi : method_infos) { |
| 196 | if (!mi.deduped_) { // Only one FDE per unique address. |
| 197 | ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo(); |
| 198 | if (!opcodes.empty()) { |
| 199 | const Elf_Addr code_address = text_address + mi.low_pc_; |
| 200 | if (format == DW_EH_FRAME_FORMAT) { |
| 201 | binary_search_table.push_back( |
| 202 | dchecked_integral_cast<uint32_t>(code_address)); |
| 203 | binary_search_table.push_back( |
| 204 | dchecked_integral_cast<uint32_t>(buffer_address)); |
| 205 | } |
| 206 | WriteFDE(is64bit, cfi_address, cie_address, |
| 207 | code_address, mi.high_pc_ - mi.low_pc_, |
| 208 | opcodes, format, buffer_address, &buffer, |
| 209 | &patch_locations); |
| 210 | cfi_section->WriteFully(buffer.data(), buffer.size()); |
| 211 | buffer_address += buffer.size(); |
| 212 | buffer.clear(); |
| 213 | } |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 214 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 215 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 216 | cfi_section->End(); |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 217 | } |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 218 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 219 | if (format == DW_EH_FRAME_FORMAT) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 220 | auto* header_section = builder->GetEhFrameHdr(); |
| 221 | header_section->Start(); |
| 222 | uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 223 | // Write .eh_frame_hdr section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 224 | std::vector<uint8_t> buffer; |
| 225 | Writer<> header(&buffer); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 226 | header.PushUint8(1); // Version. |
| 227 | // Encoding of .eh_frame pointer - libunwind does not honor datarel here, |
| 228 | // so we have to use pcrel which means relative to the pointer's location. |
| 229 | header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); |
| 230 | // Encoding of binary search table size. |
| 231 | header.PushUint8(DW_EH_PE_udata4); |
| 232 | // Encoding of binary search table addresses - libunwind supports only this |
| 233 | // specific combination, which means relative to the start of .eh_frame_hdr. |
| 234 | header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 235 | // .eh_frame pointer |
| 236 | header.PushInt32(cfi_section->GetAddress() - (header_address + 4u)); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 237 | // Binary search table size (number of entries). |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 238 | header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2)); |
| 239 | header_section->WriteFully(buffer.data(), buffer.size()); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 240 | // Binary search table. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 241 | for (size_t i = 0; i < binary_search_table.size(); i++) { |
| 242 | // Make addresses section-relative since we know the header address now. |
| 243 | binary_search_table[i] -= header_address; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 244 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 245 | header_section->WriteFully(binary_search_table.data(), binary_search_table.size()); |
| 246 | header_section->End(); |
| 247 | } else { |
| 248 | builder->WritePatches(".debug_frame.oat_patches", &patch_locations); |
David Srbecky | 033d745 | 2015-04-30 19:57:35 +0100 | [diff] [blame] | 249 | } |
David Srbecky | 8dc7324 | 2015-04-12 11:40:39 +0100 | [diff] [blame] | 250 | } |
| 251 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 252 | template<typename ElfTypes> |
| 253 | void WriteDebugSections(ElfBuilder<ElfTypes>* builder, |
| 254 | const std::vector<OatWriter::DebugInfo>& method_infos) { |
| 255 | typedef typename ElfTypes::Addr Elf_Addr; |
| 256 | const bool is64bit = Is64BitInstructionSet(builder->GetIsa()); |
| 257 | Elf_Addr text_address = builder->GetText()->GetAddress(); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 258 | |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 259 | // Find all addresses (low_pc) which contain deduped methods. |
| 260 | // The first instance of method is not marked deduped_, but the rest is. |
| 261 | std::unordered_set<uint32_t> deduped_addresses; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 262 | for (const OatWriter::DebugInfo& mi : method_infos) { |
| 263 | if (mi.deduped_) { |
| 264 | deduped_addresses.insert(mi.low_pc_); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 268 | // Group the methods into compilation units based on source file. |
| 269 | std::vector<std::vector<const OatWriter::DebugInfo*>> compilation_units; |
| 270 | const char* last_source_file = nullptr; |
Nicolas Geoffray | c04c800 | 2015-07-14 11:37:54 +0100 | [diff] [blame] | 271 | for (const OatWriter::DebugInfo& mi : method_infos) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 272 | // Attribute given instruction range only to single method. |
| 273 | // Otherwise the debugger might get really confused. |
| 274 | if (!mi.deduped_) { |
| 275 | auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_); |
| 276 | const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def); |
| 277 | if (compilation_units.empty() || source_file != last_source_file) { |
| 278 | compilation_units.push_back(std::vector<const OatWriter::DebugInfo*>()); |
| 279 | } |
| 280 | compilation_units.back().push_back(&mi); |
| 281 | last_source_file = source_file; |
| 282 | } |
| 283 | } |
| 284 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 285 | // Write .debug_info section. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 286 | std::vector<uint8_t> debug_info; |
| 287 | std::vector<uintptr_t> debug_info_patches; |
| 288 | std::vector<uint8_t> debug_abbrev; |
| 289 | std::vector<uint8_t> debug_str; |
| 290 | std::vector<uint8_t> debug_line; |
| 291 | std::vector<uintptr_t> debug_line_patches; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 292 | for (const auto& compilation_unit : compilation_units) { |
| 293 | uint32_t cunit_low_pc = 0xFFFFFFFFU; |
| 294 | uint32_t cunit_high_pc = 0; |
| 295 | for (auto method_info : compilation_unit) { |
| 296 | cunit_low_pc = std::min(cunit_low_pc, method_info->low_pc_); |
| 297 | cunit_high_pc = std::max(cunit_high_pc, method_info->high_pc_); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 298 | } |
| 299 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 300 | size_t debug_abbrev_offset = debug_abbrev.size(); |
| 301 | DebugInfoEntryWriter<> info(is64bit, &debug_abbrev); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 302 | info.StartTag(DW_TAG_compile_unit, DW_CHILDREN_yes); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 303 | info.WriteStrp(DW_AT_producer, "Android dex2oat", &debug_str); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 304 | info.WriteData1(DW_AT_language, DW_LANG_Java); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 305 | info.WriteAddr(DW_AT_low_pc, text_address + cunit_low_pc); |
| 306 | info.WriteAddr(DW_AT_high_pc, text_address + cunit_high_pc); |
| 307 | info.WriteData4(DW_AT_stmt_list, debug_line.size()); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 308 | for (auto method_info : compilation_unit) { |
| 309 | std::string method_name = PrettyMethod(method_info->dex_method_index_, |
| 310 | *method_info->dex_file_, true); |
| 311 | if (deduped_addresses.find(method_info->low_pc_) != deduped_addresses.end()) { |
| 312 | method_name += " [DEDUPED]"; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 313 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 314 | info.StartTag(DW_TAG_subprogram, DW_CHILDREN_no); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 315 | info.WriteStrp(DW_AT_name, method_name.data(), &debug_str); |
| 316 | info.WriteAddr(DW_AT_low_pc, text_address + method_info->low_pc_); |
| 317 | info.WriteAddr(DW_AT_high_pc, text_address + method_info->high_pc_); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 318 | info.EndTag(); // DW_TAG_subprogram |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 319 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 320 | info.EndTag(); // DW_TAG_compile_unit |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 321 | WriteDebugInfoCU(debug_abbrev_offset, info, &debug_info, &debug_info_patches); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 322 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 323 | // Write .debug_line section. |
| 324 | std::vector<FileEntry> files; |
| 325 | std::unordered_map<std::string, size_t> files_map; |
| 326 | std::vector<std::string> directories; |
| 327 | std::unordered_map<std::string, size_t> directories_map; |
| 328 | int code_factor_bits_ = 0; |
| 329 | int dwarf_isa = -1; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 330 | switch (builder->GetIsa()) { |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 331 | case kArm: // arm actually means thumb2. |
| 332 | case kThumb2: |
| 333 | code_factor_bits_ = 1; // 16-bit instuctions |
| 334 | dwarf_isa = 1; // DW_ISA_ARM_thumb. |
| 335 | break; |
| 336 | case kArm64: |
| 337 | case kMips: |
| 338 | case kMips64: |
| 339 | code_factor_bits_ = 2; // 32-bit instructions |
| 340 | break; |
| 341 | case kNone: |
| 342 | case kX86: |
| 343 | case kX86_64: |
| 344 | break; |
| 345 | } |
David Srbecky | 297ed22 | 2015-04-15 01:18:12 +0100 | [diff] [blame] | 346 | DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 347 | opcodes.SetAddress(text_address + cunit_low_pc); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 348 | if (dwarf_isa != -1) { |
| 349 | opcodes.SetISA(dwarf_isa); |
| 350 | } |
| 351 | for (const OatWriter::DebugInfo* mi : compilation_unit) { |
| 352 | struct DebugInfoCallbacks { |
| 353 | static bool NewPosition(void* ctx, uint32_t address, uint32_t line) { |
| 354 | auto* context = reinterpret_cast<DebugInfoCallbacks*>(ctx); |
| 355 | context->dex2line_.push_back({address, static_cast<int32_t>(line)}); |
| 356 | return false; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 357 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 358 | DefaultSrcMap dex2line_; |
| 359 | } debug_info_callbacks; |
| 360 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 361 | Elf_Addr method_address = text_address + mi->low_pc_; |
| 362 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 363 | const DexFile* dex = mi->dex_file_; |
| 364 | if (mi->code_item_ != nullptr) { |
| 365 | dex->DecodeDebugInfo(mi->code_item_, |
| 366 | (mi->access_flags_ & kAccStatic) != 0, |
| 367 | mi->dex_method_index_, |
| 368 | DebugInfoCallbacks::NewPosition, |
| 369 | nullptr, |
| 370 | &debug_info_callbacks); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 371 | } |
| 372 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 373 | // Get and deduplicate directory and filename. |
| 374 | int file_index = 0; // 0 - primary source file of the compilation. |
| 375 | auto& dex_class_def = dex->GetClassDef(mi->class_def_index_); |
| 376 | const char* source_file = dex->GetSourceFile(dex_class_def); |
| 377 | if (source_file != nullptr) { |
| 378 | std::string file_name(source_file); |
| 379 | size_t file_name_slash = file_name.find_last_of('/'); |
| 380 | std::string class_name(dex->GetClassDescriptor(dex_class_def)); |
| 381 | size_t class_name_slash = class_name.find_last_of('/'); |
| 382 | std::string full_path(file_name); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 383 | |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 384 | // Guess directory from package name. |
| 385 | int directory_index = 0; // 0 - current directory of the compilation. |
| 386 | if (file_name_slash == std::string::npos && // Just filename. |
| 387 | class_name.front() == 'L' && // Type descriptor for a class. |
| 388 | class_name_slash != std::string::npos) { // Has package name. |
| 389 | std::string package_name = class_name.substr(1, class_name_slash - 1); |
| 390 | auto it = directories_map.find(package_name); |
| 391 | if (it == directories_map.end()) { |
| 392 | directory_index = 1 + directories.size(); |
| 393 | directories_map.emplace(package_name, directory_index); |
| 394 | directories.push_back(package_name); |
| 395 | } else { |
| 396 | directory_index = it->second; |
| 397 | } |
| 398 | full_path = package_name + "/" + file_name; |
| 399 | } |
| 400 | |
| 401 | // Add file entry. |
| 402 | auto it2 = files_map.find(full_path); |
| 403 | if (it2 == files_map.end()) { |
| 404 | file_index = 1 + files.size(); |
| 405 | files_map.emplace(full_path, file_index); |
| 406 | files.push_back(FileEntry { |
| 407 | file_name, |
| 408 | directory_index, |
| 409 | 0, // Modification time - NA. |
| 410 | 0, // File size - NA. |
| 411 | }); |
| 412 | } else { |
| 413 | file_index = it2->second; |
| 414 | } |
| 415 | } |
| 416 | opcodes.SetFile(file_index); |
| 417 | |
| 418 | // Generate mapping opcodes from PC to Java lines. |
| 419 | const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_; |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 420 | if (file_index != 0 && !dex2line_map.empty()) { |
| 421 | bool first = true; |
| 422 | for (SrcMapElem pc2dex : mi->compiled_method_->GetSrcMappingTable()) { |
| 423 | uint32_t pc = pc2dex.from_; |
| 424 | int dex_pc = pc2dex.to_; |
| 425 | auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc)); |
| 426 | if (dex2line.first) { |
| 427 | int line = dex2line.second; |
| 428 | if (first) { |
| 429 | first = false; |
| 430 | if (pc > 0) { |
| 431 | // Assume that any preceding code is prologue. |
| 432 | int first_line = dex2line_map.front().to_; |
| 433 | // Prologue is not a sensible place for a breakpoint. |
| 434 | opcodes.NegateStmt(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 435 | opcodes.AddRow(method_address, first_line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 436 | opcodes.NegateStmt(); |
| 437 | opcodes.SetPrologueEnd(); |
| 438 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 439 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 440 | } else if (line != opcodes.CurrentLine()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 441 | opcodes.AddRow(method_address + pc, line); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 442 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 445 | } else { |
| 446 | // line 0 - instruction cannot be attributed to any source line. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 447 | opcodes.AddRow(method_address, 0); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 448 | } |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 449 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 450 | opcodes.AdvancePC(text_address + cunit_high_pc); |
David Srbecky | 799b8c4 | 2015-04-14 01:57:43 +0100 | [diff] [blame] | 451 | opcodes.EndSequence(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 452 | WriteDebugLineTable(directories, files, opcodes, &debug_line, &debug_line_patches); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 453 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 454 | builder->WriteSection(".debug_info", &debug_info); |
| 455 | builder->WritePatches(".debug_info.oat_patches", &debug_info_patches); |
| 456 | builder->WriteSection(".debug_abbrev", &debug_abbrev); |
| 457 | builder->WriteSection(".debug_str", &debug_str); |
| 458 | builder->WriteSection(".debug_line", &debug_line); |
| 459 | builder->WritePatches(".debug_line.oat_patches", &debug_line_patches); |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 460 | } |
| 461 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 462 | // Explicit instantiations |
| 463 | template void WriteCFISection<ElfTypes32>( |
| 464 | ElfBuilder<ElfTypes32>* builder, |
| 465 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 466 | CFIFormat format); |
| 467 | template void WriteCFISection<ElfTypes64>( |
| 468 | ElfBuilder<ElfTypes64>* builder, |
| 469 | const std::vector<OatWriter::DebugInfo>& method_infos, |
| 470 | CFIFormat format); |
| 471 | template void WriteDebugSections<ElfTypes32>( |
| 472 | ElfBuilder<ElfTypes32>* builder, |
| 473 | const std::vector<OatWriter::DebugInfo>& method_infos); |
| 474 | template void WriteDebugSections<ElfTypes64>( |
| 475 | ElfBuilder<ElfTypes64>* builder, |
| 476 | const std::vector<OatWriter::DebugInfo>& method_infos); |
| 477 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 478 | } // namespace dwarf |
| 479 | } // namespace art |