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 | |
| 20 | #include "debug_frame_opcode_writer.h" |
| 21 | #include "debug_info_entry_writer.h" |
| 22 | #include "debug_line_opcode_writer.h" |
| 23 | #include "register.h" |
| 24 | #include "writer.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace dwarf { |
| 28 | |
| 29 | // Write common information entry (CIE) to .eh_frame section. |
| 30 | template<typename Allocator> |
| 31 | void WriteEhFrameCIE(bool is64bit, Reg return_address_register, |
| 32 | const DebugFrameOpCodeWriter<Allocator>& opcodes, |
| 33 | std::vector<uint8_t>* eh_frame) { |
| 34 | Writer<> writer(eh_frame); |
| 35 | size_t cie_header_start_ = writer.data()->size(); |
| 36 | if (is64bit) { |
| 37 | // TODO: This is not related to being 64bit. |
| 38 | writer.PushUint32(0xffffffff); |
| 39 | writer.PushUint64(0); // Length placeholder. |
| 40 | writer.PushUint64(0); // CIE id. |
| 41 | } else { |
| 42 | writer.PushUint32(0); // Length placeholder. |
| 43 | writer.PushUint32(0); // CIE id. |
| 44 | } |
| 45 | writer.PushUint8(1); // Version. |
| 46 | writer.PushString("zR"); |
| 47 | writer.PushUleb128(DebugFrameOpCodeWriter<Allocator>::kCodeAlignmentFactor); |
| 48 | writer.PushSleb128(DebugFrameOpCodeWriter<Allocator>::kDataAlignmentFactor); |
| 49 | writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2. |
| 50 | writer.PushUleb128(1); // z: Augmentation data size. |
| 51 | if (is64bit) { |
| 52 | writer.PushUint8(0x04); // R: ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata8). |
| 53 | } else { |
| 54 | writer.PushUint8(0x03); // R: ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata4). |
| 55 | } |
| 56 | writer.PushData(opcodes.data()); |
| 57 | writer.Pad(is64bit ? 8 : 4); |
| 58 | if (is64bit) { |
| 59 | writer.UpdateUint64(cie_header_start_ + 4, writer.data()->size() - cie_header_start_ - 12); |
| 60 | } else { |
| 61 | writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Write frame description entry (FDE) to .eh_frame section. |
| 66 | template<typename Allocator> |
| 67 | void WriteEhFrameFDE(bool is64bit, size_t cie_offset, |
| 68 | uint64_t initial_address, uint64_t address_range, |
| 69 | const std::vector<uint8_t, Allocator>* opcodes, |
| 70 | std::vector<uint8_t>* eh_frame) { |
| 71 | Writer<> writer(eh_frame); |
| 72 | size_t fde_header_start = writer.data()->size(); |
| 73 | if (is64bit) { |
| 74 | // TODO: This is not related to being 64bit. |
| 75 | writer.PushUint32(0xffffffff); |
| 76 | writer.PushUint64(0); // Length placeholder. |
| 77 | uint64_t cie_pointer = writer.data()->size() - cie_offset; |
| 78 | writer.PushUint64(cie_pointer); |
| 79 | } else { |
| 80 | writer.PushUint32(0); // Length placeholder. |
| 81 | uint32_t cie_pointer = writer.data()->size() - cie_offset; |
| 82 | writer.PushUint32(cie_pointer); |
| 83 | } |
| 84 | if (is64bit) { |
| 85 | writer.PushUint64(initial_address); |
| 86 | writer.PushUint64(address_range); |
| 87 | } else { |
| 88 | writer.PushUint32(initial_address); |
| 89 | writer.PushUint32(address_range); |
| 90 | } |
| 91 | writer.PushUleb128(0); // Augmentation data size. |
| 92 | writer.PushData(opcodes); |
| 93 | writer.Pad(is64bit ? 8 : 4); |
| 94 | if (is64bit) { |
| 95 | writer.UpdateUint64(fde_header_start + 4, writer.data()->size() - fde_header_start - 12); |
| 96 | } else { |
| 97 | writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Write compilation unit (CU) to .debug_info section. |
| 102 | template<typename Allocator> |
| 103 | void WriteDebugInfoCU(uint32_t debug_abbrev_offset, |
| 104 | const DebugInfoEntryWriter<Allocator>& entries, |
| 105 | std::vector<uint8_t>* debug_info) { |
| 106 | Writer<> writer(debug_info); |
| 107 | size_t start = writer.data()->size(); |
| 108 | writer.PushUint32(0); // Length placeholder. |
| 109 | writer.PushUint16(3); // Version. |
| 110 | writer.PushUint32(debug_abbrev_offset); |
| 111 | writer.PushUint8(entries.is64bit() ? 8 : 4); |
| 112 | writer.PushData(entries.data()); |
| 113 | writer.UpdateUint32(start, writer.data()->size() - start - 4); |
| 114 | } |
| 115 | |
| 116 | struct FileEntry { |
| 117 | std::string file_name; |
| 118 | int directory_index; |
| 119 | int modification_time; |
| 120 | int file_size; |
| 121 | }; |
| 122 | |
| 123 | // Write line table to .debug_line section. |
| 124 | template<typename Allocator> |
| 125 | void WriteDebugLineTable(const std::vector<std::string>& include_directories, |
| 126 | const std::vector<FileEntry>& files, |
| 127 | const DebugLineOpCodeWriter<Allocator>& opcodes, |
| 128 | std::vector<uint8_t>* debug_line) { |
| 129 | Writer<> writer(debug_line); |
| 130 | size_t header_start = writer.data()->size(); |
| 131 | writer.PushUint32(0); // Section-length placeholder. |
| 132 | // Claim DWARF-2 version even though we use some DWARF-3 features. |
| 133 | // DWARF-2 consumers will ignore the unknown opcodes. |
| 134 | // This is what clang currently does. |
| 135 | writer.PushUint16(2); // .debug_line version. |
| 136 | size_t header_length_pos = writer.data()->size(); |
| 137 | writer.PushUint32(0); // Header-length placeholder. |
| 138 | writer.PushUint8(1 << opcodes.GetCodeFactorBits()); |
| 139 | writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kDefaultIsStmt ? 1 : 0); |
| 140 | writer.PushInt8(DebugLineOpCodeWriter<Allocator>::kLineBase); |
| 141 | writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kLineRange); |
| 142 | writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kOpcodeBase); |
| 143 | static const int opcode_lengths[DebugLineOpCodeWriter<Allocator>::kOpcodeBase] = { |
| 144 | 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 }; |
| 145 | for (int i = 1; i < DebugLineOpCodeWriter<Allocator>::kOpcodeBase; i++) { |
| 146 | writer.PushUint8(opcode_lengths[i]); |
| 147 | } |
| 148 | for (const std::string& directory : include_directories) { |
| 149 | writer.PushData(directory.data(), directory.size() + 1); |
| 150 | } |
| 151 | writer.PushUint8(0); // Terminate include_directories list. |
| 152 | for (const FileEntry& file : files) { |
| 153 | writer.PushData(file.file_name.data(), file.file_name.size() + 1); |
| 154 | writer.PushUleb128(file.directory_index); |
| 155 | writer.PushUleb128(file.modification_time); |
| 156 | writer.PushUleb128(file.file_size); |
| 157 | } |
| 158 | writer.PushUint8(0); // Terminate file list. |
| 159 | writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4); |
| 160 | writer.PushData(opcodes.data()->data(), opcodes.data()->size()); |
| 161 | writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4); |
| 162 | } |
| 163 | |
| 164 | } // namespace dwarf |
| 165 | } // namespace art |
| 166 | |
| 167 | #endif // ART_COMPILER_DWARF_HEADERS_H_ |