blob: f6d9b169c4936458be423677584c53aa2f210529 [file] [log] [blame]
David Srbeckyc5bfa972016-02-05 15:49:10 +00001/*
2 * Copyright (C) 2016 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_DEBUG_ELF_DEBUG_FRAME_WRITER_H_
18#define ART_COMPILER_DEBUG_ELF_DEBUG_FRAME_WRITER_H_
19
20#include <vector>
21
22#include "arch/instruction_set.h"
23#include "debug/dwarf/debug_frame_opcode_writer.h"
24#include "debug/dwarf/dwarf_constants.h"
25#include "debug/dwarf/headers.h"
26#include "debug/method_debug_info.h"
27#include "elf_builder.h"
28
29namespace art {
30namespace debug {
31
32static void WriteCIE(InstructionSet isa,
33 dwarf::CFIFormat format,
34 std::vector<uint8_t>* buffer) {
35 using Reg = dwarf::Reg;
36 // Scratch registers should be marked as undefined. This tells the
37 // debugger that its value in the previous frame is not recoverable.
38 bool is64bit = Is64BitInstructionSet(isa);
39 switch (isa) {
40 case kArm:
41 case kThumb2: {
42 dwarf::DebugFrameOpCodeWriter<> opcodes;
43 opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
44 // core registers.
45 for (int reg = 0; reg < 13; reg++) {
46 if (reg < 4 || reg == 12) {
47 opcodes.Undefined(Reg::ArmCore(reg));
48 } else {
49 opcodes.SameValue(Reg::ArmCore(reg));
50 }
51 }
52 // fp registers.
53 for (int reg = 0; reg < 32; reg++) {
54 if (reg < 16) {
55 opcodes.Undefined(Reg::ArmFp(reg));
56 } else {
57 opcodes.SameValue(Reg::ArmFp(reg));
58 }
59 }
60 auto return_reg = Reg::ArmCore(14); // R14(LR).
61 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
62 return;
63 }
64 case kArm64: {
65 dwarf::DebugFrameOpCodeWriter<> opcodes;
66 opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
67 // core registers.
68 for (int reg = 0; reg < 30; reg++) {
69 if (reg < 8 || reg == 16 || reg == 17) {
70 opcodes.Undefined(Reg::Arm64Core(reg));
71 } else {
72 opcodes.SameValue(Reg::Arm64Core(reg));
73 }
74 }
75 // fp registers.
76 for (int reg = 0; reg < 32; reg++) {
77 if (reg < 8 || reg >= 16) {
78 opcodes.Undefined(Reg::Arm64Fp(reg));
79 } else {
80 opcodes.SameValue(Reg::Arm64Fp(reg));
81 }
82 }
83 auto return_reg = Reg::Arm64Core(30); // R30(LR).
84 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
85 return;
86 }
87 case kMips:
88 case kMips64: {
89 dwarf::DebugFrameOpCodeWriter<> opcodes;
90 opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
91 // core registers.
92 for (int reg = 1; reg < 26; reg++) {
93 if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
94 opcodes.Undefined(Reg::MipsCore(reg));
95 } else {
96 opcodes.SameValue(Reg::MipsCore(reg));
97 }
98 }
99 // fp registers.
100 for (int reg = 0; reg < 32; reg++) {
101 if (reg < 24) {
102 opcodes.Undefined(Reg::Mips64Fp(reg));
103 } else {
104 opcodes.SameValue(Reg::Mips64Fp(reg));
105 }
106 }
107 auto return_reg = Reg::MipsCore(31); // R31(RA).
108 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
109 return;
110 }
111 case kX86: {
112 // FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
113 constexpr bool generate_opcodes_for_x86_fp = false;
114 dwarf::DebugFrameOpCodeWriter<> opcodes;
115 opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
116 opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
117 // core registers.
118 for (int reg = 0; reg < 8; reg++) {
119 if (reg <= 3) {
120 opcodes.Undefined(Reg::X86Core(reg));
121 } else if (reg == 4) {
122 // Stack pointer.
123 } else {
124 opcodes.SameValue(Reg::X86Core(reg));
125 }
126 }
127 // fp registers.
128 if (generate_opcodes_for_x86_fp) {
129 for (int reg = 0; reg < 8; reg++) {
130 opcodes.Undefined(Reg::X86Fp(reg));
131 }
132 }
133 auto return_reg = Reg::X86Core(8); // R8(EIP).
134 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
135 return;
136 }
137 case kX86_64: {
138 dwarf::DebugFrameOpCodeWriter<> opcodes;
139 opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
140 opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
141 // core registers.
142 for (int reg = 0; reg < 16; reg++) {
143 if (reg == 4) {
144 // Stack pointer.
145 } else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
146 opcodes.Undefined(Reg::X86_64Core(reg));
147 } else {
148 opcodes.SameValue(Reg::X86_64Core(reg));
149 }
150 }
151 // fp registers.
152 for (int reg = 0; reg < 16; reg++) {
153 if (reg < 12) {
154 opcodes.Undefined(Reg::X86_64Fp(reg));
155 } else {
156 opcodes.SameValue(Reg::X86_64Fp(reg));
157 }
158 }
159 auto return_reg = Reg::X86_64Core(16); // R16(RIP).
160 WriteCIE(is64bit, return_reg, opcodes, format, buffer);
161 return;
162 }
163 case kNone:
164 break;
165 }
166 LOG(FATAL) << "Cannot write CIE frame for ISA " << isa;
167 UNREACHABLE();
168}
169
170template<typename ElfTypes>
171void WriteCFISection(ElfBuilder<ElfTypes>* builder,
172 const ArrayRef<const MethodDebugInfo>& method_infos,
173 dwarf::CFIFormat format,
174 bool write_oat_patches) {
175 CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT || format == dwarf::DW_EH_FRAME_FORMAT);
176 typedef typename ElfTypes::Addr Elf_Addr;
177
178 if (method_infos.empty()) {
179 return;
180 }
181
182 std::vector<uint32_t> binary_search_table;
183 std::vector<uintptr_t> patch_locations;
184 if (format == dwarf::DW_EH_FRAME_FORMAT) {
185 binary_search_table.reserve(2 * method_infos.size());
186 } else {
187 patch_locations.reserve(method_infos.size());
188 }
189
190 // The methods can be written in any order.
191 // Let's therefore sort them in the lexicographical order of the opcodes.
192 // This has no effect on its own. However, if the final .debug_frame section is
193 // compressed it reduces the size since similar opcodes sequences are grouped.
194 std::vector<const MethodDebugInfo*> sorted_method_infos;
195 sorted_method_infos.reserve(method_infos.size());
196 for (size_t i = 0; i < method_infos.size(); i++) {
197 sorted_method_infos.push_back(&method_infos[i]);
198 }
199 std::sort(
200 sorted_method_infos.begin(),
201 sorted_method_infos.end(),
202 [](const MethodDebugInfo* lhs, const MethodDebugInfo* rhs) {
203 ArrayRef<const uint8_t> l = lhs->compiled_method->GetCFIInfo();
204 ArrayRef<const uint8_t> r = rhs->compiled_method->GetCFIInfo();
205 return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
206 });
207
208 // Write .eh_frame/.debug_frame section.
209 auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT
210 ? builder->GetDebugFrame()
211 : builder->GetEhFrame());
212 {
213 cfi_section->Start();
214 const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
215 const Elf_Addr text_address = builder->GetText()->Exists()
216 ? builder->GetText()->GetAddress()
217 : 0;
218 const Elf_Addr cfi_address = cfi_section->GetAddress();
219 const Elf_Addr cie_address = cfi_address;
220 Elf_Addr buffer_address = cfi_address;
221 std::vector<uint8_t> buffer; // Small temporary buffer.
222 WriteCIE(builder->GetIsa(), format, &buffer);
223 cfi_section->WriteFully(buffer.data(), buffer.size());
224 buffer_address += buffer.size();
225 buffer.clear();
226 for (const MethodDebugInfo* mi : sorted_method_infos) {
227 if (!mi->deduped) { // Only one FDE per unique address.
228 ArrayRef<const uint8_t> opcodes = mi->compiled_method->GetCFIInfo();
229 if (!opcodes.empty()) {
230 const Elf_Addr code_address = text_address + mi->low_pc;
231 if (format == dwarf::DW_EH_FRAME_FORMAT) {
232 binary_search_table.push_back(
233 dchecked_integral_cast<uint32_t>(code_address));
234 binary_search_table.push_back(
235 dchecked_integral_cast<uint32_t>(buffer_address));
236 }
237 WriteFDE(is64bit, cfi_address, cie_address,
238 code_address, mi->high_pc - mi->low_pc,
239 opcodes, format, buffer_address, &buffer,
240 &patch_locations);
241 cfi_section->WriteFully(buffer.data(), buffer.size());
242 buffer_address += buffer.size();
243 buffer.clear();
244 }
245 }
246 }
247 cfi_section->End();
248 }
249
250 if (format == dwarf::DW_EH_FRAME_FORMAT) {
251 auto* header_section = builder->GetEhFrameHdr();
252 header_section->Start();
253 uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
254 // Write .eh_frame_hdr section.
255 std::vector<uint8_t> buffer;
256 dwarf::Writer<> header(&buffer);
257 header.PushUint8(1); // Version.
258 // Encoding of .eh_frame pointer - libunwind does not honor datarel here,
259 // so we have to use pcrel which means relative to the pointer's location.
260 header.PushUint8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
261 // Encoding of binary search table size.
262 header.PushUint8(dwarf::DW_EH_PE_udata4);
263 // Encoding of binary search table addresses - libunwind supports only this
264 // specific combination, which means relative to the start of .eh_frame_hdr.
265 header.PushUint8(dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4);
266 // .eh_frame pointer
267 header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
268 // Binary search table size (number of entries).
269 header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
270 header_section->WriteFully(buffer.data(), buffer.size());
271 // Binary search table.
272 for (size_t i = 0; i < binary_search_table.size(); i++) {
273 // Make addresses section-relative since we know the header address now.
274 binary_search_table[i] -= header_address;
275 }
276 header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
277 header_section->End();
278 } else {
279 if (write_oat_patches) {
280 builder->WritePatches(".debug_frame.oat_patches",
281 ArrayRef<const uintptr_t>(patch_locations));
282 }
283 }
284}
285
286} // namespace debug
287} // namespace art
288
289#endif // ART_COMPILER_DEBUG_ELF_DEBUG_FRAME_WRITER_H_
290