blob: 9f64766e18d0b471db00073a3278357b09ab3b38 [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 Srbeckyb5362472015-04-08 19:37:39 +010038// Write common information entry (CIE) to .eh_frame section.
39template<typename Allocator>
David Srbecky527c9c72015-04-17 21:14:10 +010040void WriteEhFrameCIE(bool is64bit,
41 ExceptionHeaderValueApplication address_type,
42 Reg return_address_register,
David Srbeckyb5362472015-04-08 19:37:39 +010043 const DebugFrameOpCodeWriter<Allocator>& opcodes,
44 std::vector<uint8_t>* eh_frame) {
45 Writer<> writer(eh_frame);
46 size_t cie_header_start_ = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010047 writer.PushUint32(0); // Length placeholder.
48 writer.PushUint32(0); // CIE id.
David Srbeckyb5362472015-04-08 19:37:39 +010049 writer.PushUint8(1); // Version.
50 writer.PushString("zR");
51 writer.PushUleb128(DebugFrameOpCodeWriter<Allocator>::kCodeAlignmentFactor);
52 writer.PushSleb128(DebugFrameOpCodeWriter<Allocator>::kDataAlignmentFactor);
53 writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2.
54 writer.PushUleb128(1); // z: Augmentation data size.
55 if (is64bit) {
David Srbecky527c9c72015-04-17 21:14:10 +010056 writer.PushUint8(address_type | DW_EH_PE_udata8); // R: Pointer encoding.
David Srbeckyb5362472015-04-08 19:37:39 +010057 } else {
David Srbecky527c9c72015-04-17 21:14:10 +010058 writer.PushUint8(address_type | DW_EH_PE_udata4); // R: Pointer encoding.
David Srbeckyb5362472015-04-08 19:37:39 +010059 }
60 writer.PushData(opcodes.data());
61 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010062 writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010063}
64
65// Write frame description entry (FDE) to .eh_frame section.
66template<typename Allocator>
67void WriteEhFrameFDE(bool is64bit, size_t cie_offset,
68 uint64_t initial_address, uint64_t address_range,
69 const std::vector<uint8_t, Allocator>* opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +010070 std::vector<uint8_t>* eh_frame,
71 std::vector<uintptr_t>* eh_frame_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +010072 Writer<> writer(eh_frame);
73 size_t fde_header_start = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010074 writer.PushUint32(0); // Length placeholder.
75 uint32_t cie_pointer = writer.data()->size() - cie_offset;
76 writer.PushUint32(cie_pointer);
77 // Relocate initial_address, but not address_range (it is size).
78 eh_frame_patches->push_back(writer.data()->size());
David Srbeckyb5362472015-04-08 19:37:39 +010079 if (is64bit) {
80 writer.PushUint64(initial_address);
81 writer.PushUint64(address_range);
82 } else {
83 writer.PushUint32(initial_address);
84 writer.PushUint32(address_range);
85 }
86 writer.PushUleb128(0); // Augmentation data size.
87 writer.PushData(opcodes);
88 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010089 writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010090}
91
92// Write compilation unit (CU) to .debug_info section.
93template<typename Allocator>
94void WriteDebugInfoCU(uint32_t debug_abbrev_offset,
95 const DebugInfoEntryWriter<Allocator>& entries,
David Srbecky2f6cdb02015-04-11 00:17:53 +010096 std::vector<uint8_t>* debug_info,
97 std::vector<uintptr_t>* debug_info_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +010098 Writer<> writer(debug_info);
99 size_t start = writer.data()->size();
100 writer.PushUint32(0); // Length placeholder.
101 writer.PushUint16(3); // Version.
102 writer.PushUint32(debug_abbrev_offset);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100103 writer.PushUint8(entries.Is64bit() ? 8 : 4);
104 size_t entries_offset = writer.data()->size();
David Srbeckyb5362472015-04-08 19:37:39 +0100105 writer.PushData(entries.data());
106 writer.UpdateUint32(start, writer.data()->size() - start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100107 // Copy patch locations and make them relative to .debug_info section.
108 for (uintptr_t patch_location : entries.GetPatchLocations()) {
109 debug_info_patches->push_back(entries_offset + patch_location);
110 }
David Srbeckyb5362472015-04-08 19:37:39 +0100111}
112
113struct FileEntry {
114 std::string file_name;
115 int directory_index;
116 int modification_time;
117 int file_size;
118};
119
120// Write line table to .debug_line section.
121template<typename Allocator>
122void WriteDebugLineTable(const std::vector<std::string>& include_directories,
123 const std::vector<FileEntry>& files,
124 const DebugLineOpCodeWriter<Allocator>& opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100125 std::vector<uint8_t>* debug_line,
126 std::vector<uintptr_t>* debug_line_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +0100127 Writer<> writer(debug_line);
128 size_t header_start = writer.data()->size();
129 writer.PushUint32(0); // Section-length placeholder.
130 // Claim DWARF-2 version even though we use some DWARF-3 features.
131 // DWARF-2 consumers will ignore the unknown opcodes.
132 // This is what clang currently does.
133 writer.PushUint16(2); // .debug_line version.
134 size_t header_length_pos = writer.data()->size();
135 writer.PushUint32(0); // Header-length placeholder.
136 writer.PushUint8(1 << opcodes.GetCodeFactorBits());
137 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kDefaultIsStmt ? 1 : 0);
138 writer.PushInt8(DebugLineOpCodeWriter<Allocator>::kLineBase);
139 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kLineRange);
140 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kOpcodeBase);
141 static const int opcode_lengths[DebugLineOpCodeWriter<Allocator>::kOpcodeBase] = {
142 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 };
143 for (int i = 1; i < DebugLineOpCodeWriter<Allocator>::kOpcodeBase; i++) {
144 writer.PushUint8(opcode_lengths[i]);
145 }
146 for (const std::string& directory : include_directories) {
147 writer.PushData(directory.data(), directory.size() + 1);
148 }
149 writer.PushUint8(0); // Terminate include_directories list.
150 for (const FileEntry& file : files) {
151 writer.PushData(file.file_name.data(), file.file_name.size() + 1);
152 writer.PushUleb128(file.directory_index);
153 writer.PushUleb128(file.modification_time);
154 writer.PushUleb128(file.file_size);
155 }
156 writer.PushUint8(0); // Terminate file list.
157 writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100158 size_t opcodes_offset = writer.data()->size();
159 writer.PushData(opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100160 writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100161 // Copy patch locations and make them relative to .debug_line section.
162 for (uintptr_t patch_location : opcodes.GetPatchLocations()) {
163 debug_line_patches->push_back(opcodes_offset + patch_location);
164 }
David Srbeckyb5362472015-04-08 19:37:39 +0100165}
166
167} // namespace dwarf
168} // namespace art
169
170#endif // ART_COMPILER_DWARF_HEADERS_H_