David Srbecky | b536247 | 2015-04-08 19:37:39 +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 | #ifndef ART_COMPILER_DWARF_HEADERS_H_ |
| 18 | #define ART_COMPILER_DWARF_HEADERS_H_ |
| 19 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | |
David Srbecky | 7c869b3 | 2015-04-12 08:47:47 +0100 | [diff] [blame] | 22 | #include "dwarf/debug_frame_opcode_writer.h" |
| 23 | #include "dwarf/debug_info_entry_writer.h" |
| 24 | #include "dwarf/debug_line_opcode_writer.h" |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 25 | #include "dwarf/dwarf_constants.h" |
David Srbecky | 7c869b3 | 2015-04-12 08:47:47 +0100 | [diff] [blame] | 26 | #include "dwarf/register.h" |
| 27 | #include "dwarf/writer.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace dwarf { |
| 31 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 32 | // Note that all headers start with 32-bit length. |
| 33 | // DWARF also supports 64-bit lengths, but we never use that. |
| 34 | // It is intended to support very large debug sections (>4GB), |
| 35 | // and compilers are expected *not* to use it by default. |
| 36 | // In particular, it is not related to machine architecture. |
| 37 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 38 | // Write common information entry (CIE) to .debug_frame or .eh_frame section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 39 | template<typename Vector> |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 40 | void WriteDebugFrameCIE(bool is64bit, |
| 41 | ExceptionHeaderValueApplication address_type, |
| 42 | Reg return_address_register, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 43 | const DebugFrameOpCodeWriter<Vector>& opcodes, |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 44 | CFIFormat format, |
| 45 | std::vector<uint8_t>* debug_frame) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 46 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 47 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 48 | Writer<> writer(debug_frame); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 49 | size_t cie_header_start_ = writer.data()->size(); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 50 | writer.PushUint32(0); // Length placeholder. |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 51 | writer.PushUint32((format == DW_EH_FRAME_FORMAT) ? 0 : 0xFFFFFFFF); // CIE id. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 52 | writer.PushUint8(1); // Version. |
| 53 | writer.PushString("zR"); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 54 | writer.PushUleb128(DebugFrameOpCodeWriter<Vector>::kCodeAlignmentFactor); |
| 55 | writer.PushSleb128(DebugFrameOpCodeWriter<Vector>::kDataAlignmentFactor); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 56 | writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2. |
| 57 | writer.PushUleb128(1); // z: Augmentation data size. |
| 58 | if (is64bit) { |
David Srbecky | 1706588 | 2015-06-20 05:01:22 +0100 | [diff] [blame] | 59 | if (address_type == DW_EH_PE_pcrel) { |
| 60 | writer.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata8); // R: Pointer encoding. |
| 61 | } else { |
| 62 | DCHECK(address_type == DW_EH_PE_absptr); |
| 63 | writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata8); // R: Pointer encoding. |
| 64 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 65 | } else { |
David Srbecky | 1706588 | 2015-06-20 05:01:22 +0100 | [diff] [blame] | 66 | if (address_type == DW_EH_PE_pcrel) { |
| 67 | writer.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // R: Pointer encoding. |
| 68 | } else { |
| 69 | DCHECK(address_type == DW_EH_PE_absptr); |
| 70 | writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata4); // R: Pointer encoding. |
| 71 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 72 | } |
| 73 | writer.PushData(opcodes.data()); |
| 74 | writer.Pad(is64bit ? 8 : 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 75 | writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 76 | } |
| 77 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 78 | // Write frame description entry (FDE) to .debug_frame or .eh_frame section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 79 | template<typename Vector> |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 80 | void WriteDebugFrameFDE(bool is64bit, size_t cie_offset, |
| 81 | uint64_t initial_address, uint64_t address_range, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 82 | const Vector* opcodes, |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 83 | CFIFormat format, |
| 84 | std::vector<uint8_t>* debug_frame, |
| 85 | std::vector<uintptr_t>* debug_frame_patches) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 86 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 87 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 88 | Writer<> writer(debug_frame); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 89 | size_t fde_header_start = writer.data()->size(); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 90 | writer.PushUint32(0); // Length placeholder. |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 91 | if (format == DW_EH_FRAME_FORMAT) { |
| 92 | uint32_t cie_pointer = writer.data()->size() - cie_offset; |
| 93 | writer.PushUint32(cie_pointer); |
| 94 | } else { |
| 95 | uint32_t cie_pointer = cie_offset; |
| 96 | writer.PushUint32(cie_pointer); |
| 97 | } |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 98 | // Relocate initial_address, but not address_range (it is size). |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 99 | debug_frame_patches->push_back(writer.data()->size()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 100 | if (is64bit) { |
| 101 | writer.PushUint64(initial_address); |
| 102 | writer.PushUint64(address_range); |
| 103 | } else { |
| 104 | writer.PushUint32(initial_address); |
| 105 | writer.PushUint32(address_range); |
| 106 | } |
| 107 | writer.PushUleb128(0); // Augmentation data size. |
| 108 | writer.PushData(opcodes); |
| 109 | writer.Pad(is64bit ? 8 : 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 110 | writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Write compilation unit (CU) to .debug_info section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 114 | template<typename Vector> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 115 | void WriteDebugInfoCU(uint32_t debug_abbrev_offset, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 116 | const DebugInfoEntryWriter<Vector>& entries, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 117 | std::vector<uint8_t>* debug_info, |
| 118 | std::vector<uintptr_t>* debug_info_patches) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 119 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 120 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 121 | Writer<> writer(debug_info); |
| 122 | size_t start = writer.data()->size(); |
| 123 | writer.PushUint32(0); // Length placeholder. |
| 124 | writer.PushUint16(3); // Version. |
| 125 | writer.PushUint32(debug_abbrev_offset); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 126 | writer.PushUint8(entries.Is64bit() ? 8 : 4); |
| 127 | size_t entries_offset = writer.data()->size(); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 128 | writer.PushData(entries.data()); |
| 129 | writer.UpdateUint32(start, writer.data()->size() - start - 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 130 | // Copy patch locations and make them relative to .debug_info section. |
| 131 | for (uintptr_t patch_location : entries.GetPatchLocations()) { |
| 132 | debug_info_patches->push_back(entries_offset + patch_location); |
| 133 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | struct FileEntry { |
| 137 | std::string file_name; |
| 138 | int directory_index; |
| 139 | int modification_time; |
| 140 | int file_size; |
| 141 | }; |
| 142 | |
| 143 | // Write line table to .debug_line section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 144 | template<typename Vector> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 145 | void WriteDebugLineTable(const std::vector<std::string>& include_directories, |
| 146 | const std::vector<FileEntry>& files, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 147 | const DebugLineOpCodeWriter<Vector>& opcodes, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 148 | std::vector<uint8_t>* debug_line, |
| 149 | std::vector<uintptr_t>* debug_line_patches) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 150 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 151 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 152 | Writer<> writer(debug_line); |
| 153 | size_t header_start = writer.data()->size(); |
| 154 | writer.PushUint32(0); // Section-length placeholder. |
| 155 | // Claim DWARF-2 version even though we use some DWARF-3 features. |
| 156 | // DWARF-2 consumers will ignore the unknown opcodes. |
| 157 | // This is what clang currently does. |
| 158 | writer.PushUint16(2); // .debug_line version. |
| 159 | size_t header_length_pos = writer.data()->size(); |
| 160 | writer.PushUint32(0); // Header-length placeholder. |
| 161 | writer.PushUint8(1 << opcodes.GetCodeFactorBits()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 162 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kDefaultIsStmt ? 1 : 0); |
| 163 | writer.PushInt8(DebugLineOpCodeWriter<Vector>::kLineBase); |
| 164 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kLineRange); |
| 165 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kOpcodeBase); |
| 166 | static const int opcode_lengths[DebugLineOpCodeWriter<Vector>::kOpcodeBase] = { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 167 | 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 }; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 168 | for (int i = 1; i < DebugLineOpCodeWriter<Vector>::kOpcodeBase; i++) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 169 | writer.PushUint8(opcode_lengths[i]); |
| 170 | } |
| 171 | for (const std::string& directory : include_directories) { |
| 172 | writer.PushData(directory.data(), directory.size() + 1); |
| 173 | } |
| 174 | writer.PushUint8(0); // Terminate include_directories list. |
| 175 | for (const FileEntry& file : files) { |
| 176 | writer.PushData(file.file_name.data(), file.file_name.size() + 1); |
| 177 | writer.PushUleb128(file.directory_index); |
| 178 | writer.PushUleb128(file.modification_time); |
| 179 | writer.PushUleb128(file.file_size); |
| 180 | } |
| 181 | writer.PushUint8(0); // Terminate file list. |
| 182 | writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 183 | size_t opcodes_offset = writer.data()->size(); |
| 184 | writer.PushData(opcodes.data()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 185 | writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 186 | // Copy patch locations and make them relative to .debug_line section. |
| 187 | for (uintptr_t patch_location : opcodes.GetPatchLocations()) { |
| 188 | debug_line_patches->push_back(opcodes_offset + patch_location); |
| 189 | } |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace dwarf |
| 193 | } // namespace art |
| 194 | |
| 195 | #endif // ART_COMPILER_DWARF_HEADERS_H_ |