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 | |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 17 | #ifndef ART_LIBELFFILE_DWARF_HEADERS_H_ |
| 18 | #define ART_LIBELFFILE_DWARF_HEADERS_H_ |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 19 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 22 | #include "base/array_ref.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 23 | #include "dwarf/debug_frame_opcode_writer.h" |
| 24 | #include "dwarf/debug_info_entry_writer.h" |
| 25 | #include "dwarf/debug_line_opcode_writer.h" |
| 26 | #include "dwarf/dwarf_constants.h" |
| 27 | #include "dwarf/register.h" |
| 28 | #include "dwarf/writer.h" |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace dwarf { |
| 32 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 33 | // Note that all headers start with 32-bit length. |
| 34 | // DWARF also supports 64-bit lengths, but we never use that. |
| 35 | // It is intended to support very large debug sections (>4GB), |
| 36 | // and compilers are expected *not* to use it by default. |
| 37 | // In particular, it is not related to machine architecture. |
| 38 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 39 | // Write common information entry (CIE) to .debug_frame or .eh_frame section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 40 | template<typename Vector> |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 41 | void WriteCIE(bool is64bit, |
| 42 | Reg return_address_register, |
| 43 | const DebugFrameOpCodeWriter<Vector>& opcodes, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 44 | std::vector<uint8_t>* buffer) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 45 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 46 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 47 | Writer<> writer(buffer); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 48 | size_t cie_header_start_ = writer.data()->size(); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 49 | writer.PushUint32(0); // Length placeholder. |
David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 50 | writer.PushUint32(0xFFFFFFFF); // CIE id. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 51 | writer.PushUint8(1); // Version. |
| 52 | writer.PushString("zR"); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 53 | writer.PushUleb128(DebugFrameOpCodeWriter<Vector>::kCodeAlignmentFactor); |
| 54 | writer.PushSleb128(DebugFrameOpCodeWriter<Vector>::kDataAlignmentFactor); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 55 | writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2. |
| 56 | writer.PushUleb128(1); // z: Augmentation data size. |
| 57 | if (is64bit) { |
David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 58 | writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata8); // R: Pointer encoding. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 59 | } else { |
David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 60 | writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata4); // R: Pointer encoding. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 61 | } |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 62 | writer.PushData(opcodes.data()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 63 | writer.Pad(is64bit ? 8 : 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 64 | writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 65 | } |
| 66 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 67 | // Write frame description entry (FDE) to .debug_frame or .eh_frame section. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 68 | inline |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 69 | void WriteFDE(bool is64bit, |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 70 | uint64_t cie_pointer, // Offset of relevant CIE in debug_frame setcion. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 71 | uint64_t code_address, |
| 72 | uint64_t code_size, |
| 73 | const ArrayRef<const uint8_t>& opcodes, |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 74 | /*inout*/ std::vector<uint8_t>* buffer) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 75 | Writer<> writer(buffer); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 76 | size_t fde_header_start = writer.data()->size(); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 77 | writer.PushUint32(0); // Length placeholder. |
David Srbecky | 91b2900 | 2019-02-08 15:51:31 +0000 | [diff] [blame] | 78 | writer.PushUint32(cie_pointer); |
| 79 | // Relocate code_address if it has absolute value. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 80 | if (is64bit) { |
| 81 | writer.PushUint64(code_address); |
| 82 | writer.PushUint64(code_size); |
| 83 | } else { |
| 84 | writer.PushUint32(code_address); |
| 85 | writer.PushUint32(code_size); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 86 | } |
| 87 | writer.PushUleb128(0); // Augmentation data size. |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 88 | writer.PushData(opcodes.data(), opcodes.size()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 89 | writer.Pad(is64bit ? 8 : 4); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 90 | writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Write compilation unit (CU) to .debug_info section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 94 | template<typename Vector> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 95 | void WriteDebugInfoCU(uint32_t debug_abbrev_offset, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 96 | const DebugInfoEntryWriter<Vector>& entries, |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 97 | std::vector<uint8_t>* debug_info) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 98 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 99 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 100 | Writer<> writer(debug_info); |
| 101 | size_t start = writer.data()->size(); |
| 102 | writer.PushUint32(0); // Length placeholder. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 103 | writer.PushUint16(4); // Version. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 104 | writer.PushUint32(debug_abbrev_offset); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 105 | writer.PushUint8(entries.Is64bit() ? 8 : 4); |
| 106 | size_t entries_offset = writer.data()->size(); |
David Srbecky | 04b0526 | 2015-11-09 18:05:48 +0000 | [diff] [blame] | 107 | DCHECK_EQ(entries_offset, DebugInfoEntryWriter<Vector>::kCompilationUnitHeaderSize); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 108 | writer.PushData(entries.data()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 109 | writer.UpdateUint32(start, writer.data()->size() - start - 4); |
| 110 | } |
| 111 | |
| 112 | struct FileEntry { |
| 113 | std::string file_name; |
| 114 | int directory_index; |
| 115 | int modification_time; |
| 116 | int file_size; |
| 117 | }; |
| 118 | |
| 119 | // Write line table to .debug_line section. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 120 | template<typename Vector> |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 121 | void WriteDebugLineTable(const std::vector<std::string>& include_directories, |
| 122 | const std::vector<FileEntry>& files, |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 123 | const DebugLineOpCodeWriter<Vector>& opcodes, |
David Srbecky | 7370d92 | 2019-02-12 14:00:30 +0000 | [diff] [blame] | 124 | std::vector<uint8_t>* debug_line) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 125 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 126 | |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 127 | Writer<> writer(debug_line); |
| 128 | size_t header_start = writer.data()->size(); |
| 129 | writer.PushUint32(0); // Section-length placeholder. |
David Srbecky | 0fd295f | 2015-11-16 16:39:10 +0000 | [diff] [blame] | 130 | writer.PushUint16(3); // .debug_line version. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 131 | size_t header_length_pos = writer.data()->size(); |
| 132 | writer.PushUint32(0); // Header-length placeholder. |
| 133 | writer.PushUint8(1 << opcodes.GetCodeFactorBits()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 134 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kDefaultIsStmt ? 1 : 0); |
| 135 | writer.PushInt8(DebugLineOpCodeWriter<Vector>::kLineBase); |
| 136 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kLineRange); |
| 137 | writer.PushUint8(DebugLineOpCodeWriter<Vector>::kOpcodeBase); |
| 138 | static const int opcode_lengths[DebugLineOpCodeWriter<Vector>::kOpcodeBase] = { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 139 | 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] | 140 | for (int i = 1; i < DebugLineOpCodeWriter<Vector>::kOpcodeBase; i++) { |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 141 | writer.PushUint8(opcode_lengths[i]); |
| 142 | } |
| 143 | for (const std::string& directory : include_directories) { |
| 144 | writer.PushData(directory.data(), directory.size() + 1); |
| 145 | } |
| 146 | writer.PushUint8(0); // Terminate include_directories list. |
| 147 | for (const FileEntry& file : files) { |
| 148 | writer.PushData(file.file_name.data(), file.file_name.size() + 1); |
| 149 | writer.PushUleb128(file.directory_index); |
| 150 | writer.PushUleb128(file.modification_time); |
| 151 | writer.PushUleb128(file.file_size); |
| 152 | } |
| 153 | writer.PushUint8(0); // Terminate file list. |
| 154 | writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4); |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 155 | writer.PushData(opcodes.data()); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 156 | writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4); |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace dwarf |
| 160 | } // namespace art |
| 161 | |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 162 | #endif // ART_LIBELFFILE_DWARF_HEADERS_H_ |