blob: ad315ee351e80bafd78642f199278d3155f183d4 [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"
David Srbeckyb5362472015-04-08 19:37:39 +010028
29namespace art {
30namespace dwarf {
31
David Srbecky2f6cdb02015-04-11 00:17:53 +010032// 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 Srbeckyad5fa8c2015-05-06 18:27:35 +010038// Write common information entry (CIE) to .debug_frame or .eh_frame section.
David Srbeckyb5362472015-04-08 19:37:39 +010039template<typename Allocator>
David Srbeckyad5fa8c2015-05-06 18:27:35 +010040void WriteDebugFrameCIE(bool is64bit,
41 ExceptionHeaderValueApplication address_type,
42 Reg return_address_register,
43 const DebugFrameOpCodeWriter<Allocator>& opcodes,
44 CFIFormat format,
45 std::vector<uint8_t>* debug_frame) {
46 Writer<> writer(debug_frame);
David Srbeckyb5362472015-04-08 19:37:39 +010047 size_t cie_header_start_ = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010048 writer.PushUint32(0); // Length placeholder.
David Srbeckyad5fa8c2015-05-06 18:27:35 +010049 writer.PushUint32((format == DW_EH_FRAME_FORMAT) ? 0 : 0xFFFFFFFF); // CIE id.
David Srbeckyb5362472015-04-08 19:37:39 +010050 writer.PushUint8(1); // Version.
51 writer.PushString("zR");
52 writer.PushUleb128(DebugFrameOpCodeWriter<Allocator>::kCodeAlignmentFactor);
53 writer.PushSleb128(DebugFrameOpCodeWriter<Allocator>::kDataAlignmentFactor);
54 writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2.
55 writer.PushUleb128(1); // z: Augmentation data size.
56 if (is64bit) {
David Srbecky527c9c72015-04-17 21:14:10 +010057 writer.PushUint8(address_type | DW_EH_PE_udata8); // R: Pointer encoding.
David Srbeckyb5362472015-04-08 19:37:39 +010058 } else {
David Srbecky527c9c72015-04-17 21:14:10 +010059 writer.PushUint8(address_type | DW_EH_PE_udata4); // R: Pointer encoding.
David Srbeckyb5362472015-04-08 19:37:39 +010060 }
61 writer.PushData(opcodes.data());
62 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010063 writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010064}
65
David Srbeckyad5fa8c2015-05-06 18:27:35 +010066// Write frame description entry (FDE) to .debug_frame or .eh_frame section.
David Srbeckyb5362472015-04-08 19:37:39 +010067template<typename Allocator>
David Srbeckyad5fa8c2015-05-06 18:27:35 +010068void WriteDebugFrameFDE(bool is64bit, size_t cie_offset,
69 uint64_t initial_address, uint64_t address_range,
70 const std::vector<uint8_t, Allocator>* opcodes,
71 CFIFormat format,
72 std::vector<uint8_t>* debug_frame,
73 std::vector<uintptr_t>* debug_frame_patches) {
74 Writer<> writer(debug_frame);
David Srbeckyb5362472015-04-08 19:37:39 +010075 size_t fde_header_start = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010076 writer.PushUint32(0); // Length placeholder.
David Srbeckyad5fa8c2015-05-06 18:27:35 +010077 if (format == DW_EH_FRAME_FORMAT) {
78 uint32_t cie_pointer = writer.data()->size() - cie_offset;
79 writer.PushUint32(cie_pointer);
80 } else {
81 uint32_t cie_pointer = cie_offset;
82 writer.PushUint32(cie_pointer);
83 }
David Srbecky2f6cdb02015-04-11 00:17:53 +010084 // Relocate initial_address, but not address_range (it is size).
David Srbeckyad5fa8c2015-05-06 18:27:35 +010085 debug_frame_patches->push_back(writer.data()->size());
David Srbeckyb5362472015-04-08 19:37:39 +010086 if (is64bit) {
87 writer.PushUint64(initial_address);
88 writer.PushUint64(address_range);
89 } else {
90 writer.PushUint32(initial_address);
91 writer.PushUint32(address_range);
92 }
93 writer.PushUleb128(0); // Augmentation data size.
94 writer.PushData(opcodes);
95 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010096 writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010097}
98
99// Write compilation unit (CU) to .debug_info section.
100template<typename Allocator>
101void WriteDebugInfoCU(uint32_t debug_abbrev_offset,
102 const DebugInfoEntryWriter<Allocator>& entries,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100103 std::vector<uint8_t>* debug_info,
104 std::vector<uintptr_t>* debug_info_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +0100105 Writer<> writer(debug_info);
106 size_t start = writer.data()->size();
107 writer.PushUint32(0); // Length placeholder.
108 writer.PushUint16(3); // Version.
109 writer.PushUint32(debug_abbrev_offset);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100110 writer.PushUint8(entries.Is64bit() ? 8 : 4);
111 size_t entries_offset = writer.data()->size();
David Srbeckyb5362472015-04-08 19:37:39 +0100112 writer.PushData(entries.data());
113 writer.UpdateUint32(start, writer.data()->size() - start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100114 // Copy patch locations and make them relative to .debug_info section.
115 for (uintptr_t patch_location : entries.GetPatchLocations()) {
116 debug_info_patches->push_back(entries_offset + patch_location);
117 }
David Srbeckyb5362472015-04-08 19:37:39 +0100118}
119
120struct FileEntry {
121 std::string file_name;
122 int directory_index;
123 int modification_time;
124 int file_size;
125};
126
127// Write line table to .debug_line section.
128template<typename Allocator>
129void WriteDebugLineTable(const std::vector<std::string>& include_directories,
130 const std::vector<FileEntry>& files,
131 const DebugLineOpCodeWriter<Allocator>& opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100132 std::vector<uint8_t>* debug_line,
133 std::vector<uintptr_t>* debug_line_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +0100134 Writer<> writer(debug_line);
135 size_t header_start = writer.data()->size();
136 writer.PushUint32(0); // Section-length placeholder.
137 // Claim DWARF-2 version even though we use some DWARF-3 features.
138 // DWARF-2 consumers will ignore the unknown opcodes.
139 // This is what clang currently does.
140 writer.PushUint16(2); // .debug_line version.
141 size_t header_length_pos = writer.data()->size();
142 writer.PushUint32(0); // Header-length placeholder.
143 writer.PushUint8(1 << opcodes.GetCodeFactorBits());
144 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kDefaultIsStmt ? 1 : 0);
145 writer.PushInt8(DebugLineOpCodeWriter<Allocator>::kLineBase);
146 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kLineRange);
147 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kOpcodeBase);
148 static const int opcode_lengths[DebugLineOpCodeWriter<Allocator>::kOpcodeBase] = {
149 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 };
150 for (int i = 1; i < DebugLineOpCodeWriter<Allocator>::kOpcodeBase; i++) {
151 writer.PushUint8(opcode_lengths[i]);
152 }
153 for (const std::string& directory : include_directories) {
154 writer.PushData(directory.data(), directory.size() + 1);
155 }
156 writer.PushUint8(0); // Terminate include_directories list.
157 for (const FileEntry& file : files) {
158 writer.PushData(file.file_name.data(), file.file_name.size() + 1);
159 writer.PushUleb128(file.directory_index);
160 writer.PushUleb128(file.modification_time);
161 writer.PushUleb128(file.file_size);
162 }
163 writer.PushUint8(0); // Terminate file list.
164 writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100165 size_t opcodes_offset = writer.data()->size();
166 writer.PushData(opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100167 writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100168 // Copy patch locations and make them relative to .debug_line section.
169 for (uintptr_t patch_location : opcodes.GetPatchLocations()) {
170 debug_line_patches->push_back(opcodes_offset + patch_location);
171 }
David Srbeckyb5362472015-04-08 19:37:39 +0100172}
173
174} // namespace dwarf
175} // namespace art
176
177#endif // ART_COMPILER_DWARF_HEADERS_H_