blob: 760f53c6e45acf65ac3a0a64038443ce4ff5aad5 [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"
25#include "dwarf/register.h"
26#include "dwarf/writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010027
28namespace art {
29namespace dwarf {
30
David Srbecky2f6cdb02015-04-11 00:17:53 +010031// Note that all headers start with 32-bit length.
32// DWARF also supports 64-bit lengths, but we never use that.
33// It is intended to support very large debug sections (>4GB),
34// and compilers are expected *not* to use it by default.
35// In particular, it is not related to machine architecture.
36
David Srbeckyb5362472015-04-08 19:37:39 +010037// Write common information entry (CIE) to .eh_frame section.
38template<typename Allocator>
39void WriteEhFrameCIE(bool is64bit, Reg return_address_register,
40 const DebugFrameOpCodeWriter<Allocator>& opcodes,
41 std::vector<uint8_t>* eh_frame) {
42 Writer<> writer(eh_frame);
43 size_t cie_header_start_ = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010044 writer.PushUint32(0); // Length placeholder.
45 writer.PushUint32(0); // CIE id.
David Srbeckyb5362472015-04-08 19:37:39 +010046 writer.PushUint8(1); // Version.
47 writer.PushString("zR");
48 writer.PushUleb128(DebugFrameOpCodeWriter<Allocator>::kCodeAlignmentFactor);
49 writer.PushSleb128(DebugFrameOpCodeWriter<Allocator>::kDataAlignmentFactor);
50 writer.PushUleb128(return_address_register.num()); // ubyte in DWARF2.
51 writer.PushUleb128(1); // z: Augmentation data size.
52 if (is64bit) {
53 writer.PushUint8(0x04); // R: ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata8).
54 } else {
55 writer.PushUint8(0x03); // R: ((DW_EH_PE_absptr << 4) | DW_EH_PE_udata4).
56 }
57 writer.PushData(opcodes.data());
58 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010059 writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010060}
61
62// Write frame description entry (FDE) to .eh_frame section.
63template<typename Allocator>
64void WriteEhFrameFDE(bool is64bit, size_t cie_offset,
65 uint64_t initial_address, uint64_t address_range,
66 const std::vector<uint8_t, Allocator>* opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +010067 std::vector<uint8_t>* eh_frame,
68 std::vector<uintptr_t>* eh_frame_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +010069 Writer<> writer(eh_frame);
70 size_t fde_header_start = writer.data()->size();
David Srbecky2f6cdb02015-04-11 00:17:53 +010071 writer.PushUint32(0); // Length placeholder.
72 uint32_t cie_pointer = writer.data()->size() - cie_offset;
73 writer.PushUint32(cie_pointer);
74 // Relocate initial_address, but not address_range (it is size).
75 eh_frame_patches->push_back(writer.data()->size());
David Srbeckyb5362472015-04-08 19:37:39 +010076 if (is64bit) {
77 writer.PushUint64(initial_address);
78 writer.PushUint64(address_range);
79 } else {
80 writer.PushUint32(initial_address);
81 writer.PushUint32(address_range);
82 }
83 writer.PushUleb128(0); // Augmentation data size.
84 writer.PushData(opcodes);
85 writer.Pad(is64bit ? 8 : 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +010086 writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
David Srbeckyb5362472015-04-08 19:37:39 +010087}
88
89// Write compilation unit (CU) to .debug_info section.
90template<typename Allocator>
91void WriteDebugInfoCU(uint32_t debug_abbrev_offset,
92 const DebugInfoEntryWriter<Allocator>& entries,
David Srbecky2f6cdb02015-04-11 00:17:53 +010093 std::vector<uint8_t>* debug_info,
94 std::vector<uintptr_t>* debug_info_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +010095 Writer<> writer(debug_info);
96 size_t start = writer.data()->size();
97 writer.PushUint32(0); // Length placeholder.
98 writer.PushUint16(3); // Version.
99 writer.PushUint32(debug_abbrev_offset);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100100 writer.PushUint8(entries.Is64bit() ? 8 : 4);
101 size_t entries_offset = writer.data()->size();
David Srbeckyb5362472015-04-08 19:37:39 +0100102 writer.PushData(entries.data());
103 writer.UpdateUint32(start, writer.data()->size() - start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100104 // Copy patch locations and make them relative to .debug_info section.
105 for (uintptr_t patch_location : entries.GetPatchLocations()) {
106 debug_info_patches->push_back(entries_offset + patch_location);
107 }
David Srbeckyb5362472015-04-08 19:37:39 +0100108}
109
110struct FileEntry {
111 std::string file_name;
112 int directory_index;
113 int modification_time;
114 int file_size;
115};
116
117// Write line table to .debug_line section.
118template<typename Allocator>
119void WriteDebugLineTable(const std::vector<std::string>& include_directories,
120 const std::vector<FileEntry>& files,
121 const DebugLineOpCodeWriter<Allocator>& opcodes,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100122 std::vector<uint8_t>* debug_line,
123 std::vector<uintptr_t>* debug_line_patches) {
David Srbeckyb5362472015-04-08 19:37:39 +0100124 Writer<> writer(debug_line);
125 size_t header_start = writer.data()->size();
126 writer.PushUint32(0); // Section-length placeholder.
127 // Claim DWARF-2 version even though we use some DWARF-3 features.
128 // DWARF-2 consumers will ignore the unknown opcodes.
129 // This is what clang currently does.
130 writer.PushUint16(2); // .debug_line version.
131 size_t header_length_pos = writer.data()->size();
132 writer.PushUint32(0); // Header-length placeholder.
133 writer.PushUint8(1 << opcodes.GetCodeFactorBits());
134 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kDefaultIsStmt ? 1 : 0);
135 writer.PushInt8(DebugLineOpCodeWriter<Allocator>::kLineBase);
136 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kLineRange);
137 writer.PushUint8(DebugLineOpCodeWriter<Allocator>::kOpcodeBase);
138 static const int opcode_lengths[DebugLineOpCodeWriter<Allocator>::kOpcodeBase] = {
139 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 };
140 for (int i = 1; i < DebugLineOpCodeWriter<Allocator>::kOpcodeBase; i++) {
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 Srbecky2f6cdb02015-04-11 00:17:53 +0100155 size_t opcodes_offset = writer.data()->size();
156 writer.PushData(opcodes.data());
David Srbeckyb5362472015-04-08 19:37:39 +0100157 writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100158 // Copy patch locations and make them relative to .debug_line section.
159 for (uintptr_t patch_location : opcodes.GetPatchLocations()) {
160 debug_line_patches->push_back(opcodes_offset + patch_location);
161 }
David Srbeckyb5362472015-04-08 19:37:39 +0100162}
163
164} // namespace dwarf
165} // namespace art
166
167#endif // ART_COMPILER_DWARF_HEADERS_H_