blob: f3fba4b1fa30cf53ed053f5d520f8e967c42856d [file] [log] [blame]
David Srbeckyb5362472015-04-08 19:37:39 +01001/*
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 Srbecky2f6cdb02015-04-11 00:17:53 +010020#include <cstdint>
21
David Srbecky7c869b32015-04-12 08:47:47 +010022#include "dwarf/debug_frame_opcode_writer.h"
23#include "dwarf/debug_info_entry_writer.h"
24#include "dwarf/debug_line_opcode_writer.h"
David Srbecky527c9c72015-04-17 21:14:10 +010025#include "dwarf/dwarf_constants.h"
David Srbecky7c869b32015-04-12 08:47:47 +010026#include "dwarf/register.h"
27#include "dwarf/writer.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010028#include "utils/array_ref.h"
David Srbeckyb5362472015-04-08 19:37:39 +010029
30namespace art {
31namespace dwarf {
32
David Srbecky2f6cdb02015-04-11 00:17:53 +010033// 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 Srbeckyad5fa8c2015-05-06 18:27:35 +010039// Write common information entry (CIE) to .debug_frame or .eh_frame section.
Vladimir Markoec7802a2015-10-01 20:57:57 +010040template<typename Vector>
David Srbeckyad5fa8c2015-05-06 18:27:35 +010041void WriteDebugFrameCIE(bool is64bit,
42 ExceptionHeaderValueApplication address_type,
43 Reg return_address_register,
Vladimir Markoec7802a2015-10-01 20:57:57 +010044 const DebugFrameOpCodeWriter<Vector>& opcodes,
David Srbeckyad5fa8c2015-05-06 18:27:35 +010045 CFIFormat format,
46 std::vector<uint8_t>* debug_frame) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010047 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
48
David Srbeckyad5fa8c2015-05-06 18:27:35 +010049 Writer<> writer(debug_frame);
David Srbeckyb5362472015-04-08 19:37:39 +010050 size_t cie_header_start_ = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010051 writer.PushUint32(0); // Length placeholder.
David Srbeckyad5fa8c2015-05-06 18:27:35 +010052 writer.PushUint32((format == DW_EH_FRAME_FORMAT) ? 0 : 0xFFFFFFFF); // CIE id.
David Srbeckyb5362472015-04-08 19:37:39 +010053 writer.PushUint8(1); // Version.
54 writer.PushString("zR");
Vladimir Markoec7802a2015-10-01 20:57:57 +010055 writer.PushUleb128(DebugFrameOpCodeWriter<Vector>::kCodeAlignmentFactor);
56 writer.PushSleb128(DebugFrameOpCodeWriter<Vector>::kDataAlignmentFactor);
David Srbeckyb5362472015-04-08 19:37:39 +010057 writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2.
58 writer.PushUleb128(1); // z: Augmentation data size.
59 if (is64bit) {
David Srbecky17065882015-06-20 05:01:22 +010060 if (address_type == DW_EH_PE_pcrel) {
61 writer.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata8); // R: Pointer encoding.
62 } else {
63 DCHECK(address_type == DW_EH_PE_absptr);
64 writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata8); // R: Pointer encoding.
65 }
David Srbeckyb5362472015-04-08 19:37:39 +010066 } else {
David Srbecky17065882015-06-20 05:01:22 +010067 if (address_type == DW_EH_PE_pcrel) {
68 writer.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4); // R: Pointer encoding.
69 } else {
70 DCHECK(address_type == DW_EH_PE_absptr);
71 writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata4); // R: Pointer encoding.
72 }
David Srbeckyb5362472015-04-08 19:37:39 +010073 }
Vladimir Marko35831e82015-09-11 11:59:18 +010074 writer.PushData(*opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +010075 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010076 writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010077}
78
David Srbeckyad5fa8c2015-05-06 18:27:35 +010079// Write frame description entry (FDE) to .debug_frame or .eh_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +010080inline
David Srbeckyad5fa8c2015-05-06 18:27:35 +010081void WriteDebugFrameFDE(bool is64bit, size_t cie_offset,
82 uint64_t initial_address, uint64_t address_range,
Vladimir Marko35831e82015-09-11 11:59:18 +010083 const ArrayRef<const uint8_t>& opcodes,
David Srbeckyad5fa8c2015-05-06 18:27:35 +010084 CFIFormat format,
85 std::vector<uint8_t>* debug_frame,
86 std::vector<uintptr_t>* debug_frame_patches) {
87 Writer<> writer(debug_frame);
David Srbeckyb5362472015-04-08 19:37:39 +010088 size_t fde_header_start = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010089 writer.PushUint32(0); // Length placeholder.
David Srbeckyad5fa8c2015-05-06 18:27:35 +010090 if (format == DW_EH_FRAME_FORMAT) {
91 uint32_t cie_pointer = writer.data()->size() - cie_offset;
92 writer.PushUint32(cie_pointer);
93 } else {
94 uint32_t cie_pointer = cie_offset;
95 writer.PushUint32(cie_pointer);
96 }
David Srbecky2f6cdb02015-04-11 00:17:53 +010097 // Relocate initial_address, but not address_range (it is size).
David Srbeckyad5fa8c2015-05-06 18:27:35 +010098 debug_frame_patches->push_back(writer.data()->size());
David Srbeckyb5362472015-04-08 19:37:39 +010099 if (is64bit) {
100 writer.PushUint64(initial_address);
101 writer.PushUint64(address_range);
102 } else {
103 writer.PushUint32(initial_address);
104 writer.PushUint32(address_range);
105 }
106 writer.PushUleb128(0); // Augmentation data size.
107 writer.PushData(opcodes);
108 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100109 writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +0100110}
111
112// Write compilation unit (CU) to .debug_info section.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100113template<typename Vector>
David Srbeckyb5362472015-04-08 19:37:39 +0100114void WriteDebugInfoCU(uint32_t debug_abbrev_offset,
Vladimir Markoec7802a2015-10-01 20:57:57 +0100115 const DebugInfoEntryWriter<Vector>& entries,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100116 std::vector<uint8_t>* debug_info,
117 std::vector<uintptr_t>* debug_info_patches) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100118 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
119
David Srbeckyb5362472015-04-08 19:37:39 +0100120 Writer<> writer(debug_info);
121 size_t start = writer.data()->size();
122 writer.PushUint32(0); // Length placeholder.
123 writer.PushUint16(3); // Version.
124 writer.PushUint32(debug_abbrev_offset);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100125 writer.PushUint8(entries.Is64bit() ? 8 : 4);
126 size_t entries_offset = writer.data()->size();
Vladimir Marko35831e82015-09-11 11:59:18 +0100127 writer.PushData(*entries.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100128 writer.UpdateUint32(start, writer.data()->size() - start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100129 // Copy patch locations and make them relative to .debug_info section.
130 for (uintptr_t patch_location : entries.GetPatchLocations()) {
131 debug_info_patches->push_back(entries_offset + patch_location);
132 }
David Srbeckyb5362472015-04-08 19:37:39 +0100133}
134
135struct FileEntry {
136 std::string file_name;
137 int directory_index;
138 int modification_time;
139 int file_size;
140};
141
142// Write line table to .debug_line section.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100143template<typename Vector>
David Srbeckyb5362472015-04-08 19:37:39 +0100144void WriteDebugLineTable(const std::vector<std::string>& include_directories,
145 const std::vector<FileEntry>& files,
Vladimir Markoec7802a2015-10-01 20:57:57 +0100146 const DebugLineOpCodeWriter<Vector>& opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100147 std::vector<uint8_t>* debug_line,
148 std::vector<uintptr_t>* debug_line_patches) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100149 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
150
David Srbeckyb5362472015-04-08 19:37:39 +0100151 Writer<> writer(debug_line);
152 size_t header_start = writer.data()->size();
153 writer.PushUint32(0); // Section-length placeholder.
154 // Claim DWARF-2 version even though we use some DWARF-3 features.
155 // DWARF-2 consumers will ignore the unknown opcodes.
156 // This is what clang currently does.
157 writer.PushUint16(2); // .debug_line version.
158 size_t header_length_pos = writer.data()->size();
159 writer.PushUint32(0); // Header-length placeholder.
160 writer.PushUint8(1 << opcodes.GetCodeFactorBits());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100161 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kDefaultIsStmt ? 1 : 0);
162 writer.PushInt8(DebugLineOpCodeWriter<Vector>::kLineBase);
163 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kLineRange);
164 writer.PushUint8(DebugLineOpCodeWriter<Vector>::kOpcodeBase);
165 static const int opcode_lengths[DebugLineOpCodeWriter<Vector>::kOpcodeBase] = {
David Srbeckyb5362472015-04-08 19:37:39 +0100166 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 };
Vladimir Markoec7802a2015-10-01 20:57:57 +0100167 for (int i = 1; i < DebugLineOpCodeWriter<Vector>::kOpcodeBase; i++) {
David Srbeckyb5362472015-04-08 19:37:39 +0100168 writer.PushUint8(opcode_lengths[i]);
169 }
170 for (const std::string& directory : include_directories) {
171 writer.PushData(directory.data(), directory.size() + 1);
172 }
173 writer.PushUint8(0); // Terminate include_directories list.
174 for (const FileEntry& file : files) {
175 writer.PushData(file.file_name.data(), file.file_name.size() + 1);
176 writer.PushUleb128(file.directory_index);
177 writer.PushUleb128(file.modification_time);
178 writer.PushUleb128(file.file_size);
179 }
180 writer.PushUint8(0); // Terminate file list.
181 writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100182 size_t opcodes_offset = writer.data()->size();
Vladimir Marko35831e82015-09-11 11:59:18 +0100183 writer.PushData(*opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100184 writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100185 // Copy patch locations and make them relative to .debug_line section.
186 for (uintptr_t patch_location : opcodes.GetPatchLocations()) {
187 debug_line_patches->push_back(opcodes_offset + patch_location);
188 }
David Srbeckyb5362472015-04-08 19:37:39 +0100189}
190
191} // namespace dwarf
192} // namespace art
193
194#endif // ART_COMPILER_DWARF_HEADERS_H_