blob: 7fa6e146c5de2a3b16694fe051bed5428346e63a [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#include "elf_debug_writer.h"
18
19#include <vector>
20
David Brazdild9c90372016-09-14 16:53:55 +010021#include "base/array_ref.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000022#include "debug/dwarf/dwarf_constants.h"
23#include "debug/elf_compilation_unit.h"
24#include "debug/elf_debug_frame_writer.h"
25#include "debug/elf_debug_info_writer.h"
26#include "debug/elf_debug_line_writer.h"
27#include "debug/elf_debug_loc_writer.h"
28#include "debug/elf_gnu_debugdata_writer.h"
29#include "debug/elf_symtab_writer.h"
30#include "debug/method_debug_info.h"
31#include "elf_builder.h"
32#include "linker/vector_output_stream.h"
Andreas Gamped4901292017-05-30 18:41:34 -070033#include "oat.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000034
35namespace art {
36namespace debug {
37
38template <typename ElfTypes>
39void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
40 const ArrayRef<const MethodDebugInfo>& method_infos,
41 dwarf::CFIFormat cfi_format,
42 bool write_oat_patches) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000043 // Write .strtab and .symtab.
David Srbeckyc5bfa972016-02-05 15:49:10 +000044 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
David Srbeckyc5bfa972016-02-05 15:49:10 +000045
David Srbecky09c2a6b2016-03-11 17:11:44 +000046 // Write .debug_frame.
47 WriteCFISection(builder, method_infos, cfi_format, write_oat_patches);
48
David Srbeckyc5bfa972016-02-05 15:49:10 +000049 // Group the methods into compilation units based on source file.
50 std::vector<ElfCompilationUnit> compilation_units;
51 const char* last_source_file = nullptr;
52 for (const MethodDebugInfo& mi : method_infos) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000053 if (mi.dex_file != nullptr) {
54 auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index);
55 const char* source_file = mi.dex_file->GetSourceFile(dex_class_def);
56 if (compilation_units.empty() || source_file != last_source_file) {
57 compilation_units.push_back(ElfCompilationUnit());
58 }
59 ElfCompilationUnit& cu = compilation_units.back();
60 cu.methods.push_back(&mi);
61 // All methods must have the same addressing mode otherwise the min/max below does not work.
62 DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative);
63 cu.is_code_address_text_relative = mi.is_code_address_text_relative;
64 cu.code_address = std::min(cu.code_address, mi.code_address);
65 cu.code_end = std::max(cu.code_end, mi.code_address + mi.code_size);
66 last_source_file = source_file;
David Srbeckyc5bfa972016-02-05 15:49:10 +000067 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000068 }
69
70 // Write .debug_line section.
71 if (!compilation_units.empty()) {
72 ElfDebugLineWriter<ElfTypes> line_writer(builder);
73 line_writer.Start();
74 for (auto& compilation_unit : compilation_units) {
75 line_writer.WriteCompilationUnit(compilation_unit);
76 }
77 line_writer.End(write_oat_patches);
78 }
79
80 // Write .debug_info section.
81 if (!compilation_units.empty()) {
82 ElfDebugInfoWriter<ElfTypes> info_writer(builder);
83 info_writer.Start();
84 for (const auto& compilation_unit : compilation_units) {
85 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
86 cu_writer.Write(compilation_unit);
87 }
88 info_writer.End(write_oat_patches);
89 }
90}
91
92std::vector<uint8_t> MakeMiniDebugInfo(
93 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000094 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +000095 size_t rodata_size,
96 size_t text_size,
97 const ArrayRef<const MethodDebugInfo>& method_infos) {
98 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +000099 return MakeMiniDebugInfoInternal<ElfTypes64>(isa,
100 features,
101 rodata_size,
102 text_size,
103 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000104 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000105 return MakeMiniDebugInfoInternal<ElfTypes32>(isa,
106 features,
107 rodata_size,
108 text_size,
109 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000110 }
111}
112
113template <typename ElfTypes>
Vladimir Marko93205e32016-04-13 11:59:46 +0100114static std::vector<uint8_t> WriteDebugElfFileForMethodsInternal(
David Srbeckyfe736b72016-03-09 11:44:44 +0000115 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000116 const InstructionSetFeatures* features,
David Srbeckyfe736b72016-03-09 11:44:44 +0000117 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000118 std::vector<uint8_t> buffer;
119 buffer.reserve(KB);
120 VectorOutputStream out("Debug ELF file", &buffer);
David Srbecky5d811202016-03-08 13:21:22 +0000121 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000122 // No program headers since the ELF file is not linked and has no allocated sections.
123 builder->Start(false /* write_program_headers */);
124 WriteDebugInfo(builder.get(),
David Srbeckyfe736b72016-03-09 11:44:44 +0000125 method_infos,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000126 dwarf::DW_DEBUG_FRAME_FORMAT,
127 false /* write_oat_patches */);
128 builder->End();
129 CHECK(builder->Good());
Vladimir Marko93205e32016-04-13 11:59:46 +0100130 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000131}
132
Vladimir Marko93205e32016-04-13 11:59:46 +0100133std::vector<uint8_t> WriteDebugElfFileForMethods(
David Srbeckyfe736b72016-03-09 11:44:44 +0000134 InstructionSet isa,
135 const InstructionSetFeatures* features,
136 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000137 if (Is64BitInstructionSet(isa)) {
David Srbeckyfe736b72016-03-09 11:44:44 +0000138 return WriteDebugElfFileForMethodsInternal<ElfTypes64>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000139 } else {
David Srbeckyfe736b72016-03-09 11:44:44 +0000140 return WriteDebugElfFileForMethodsInternal<ElfTypes32>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000141 }
142}
143
144template <typename ElfTypes>
Vladimir Marko93205e32016-04-13 11:59:46 +0100145static std::vector<uint8_t> WriteDebugElfFileForClassesInternal(
David Srbeckyfe736b72016-03-09 11:44:44 +0000146 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000147 const InstructionSetFeatures* features,
148 const ArrayRef<mirror::Class*>& types)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000150 std::vector<uint8_t> buffer;
151 buffer.reserve(KB);
152 VectorOutputStream out("Debug ELF file", &buffer);
David Srbecky5d811202016-03-08 13:21:22 +0000153 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000154 // No program headers since the ELF file is not linked and has no allocated sections.
155 builder->Start(false /* write_program_headers */);
156 ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
157 info_writer.Start();
158 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
159 cu_writer.Write(types);
160 info_writer.End(false /* write_oat_patches */);
161
162 builder->End();
163 CHECK(builder->Good());
Vladimir Marko93205e32016-04-13 11:59:46 +0100164 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000165}
166
Vladimir Marko93205e32016-04-13 11:59:46 +0100167std::vector<uint8_t> WriteDebugElfFileForClasses(InstructionSet isa,
168 const InstructionSetFeatures* features,
169 const ArrayRef<mirror::Class*>& types) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000170 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000171 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000172 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000173 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000174 }
175}
176
David Srbecky09c2a6b2016-03-11 17:11:44 +0000177std::vector<MethodDebugInfo> MakeTrampolineInfos(const OatHeader& header) {
178 std::map<const char*, uint32_t> trampolines = {
179 { "interpreterToInterpreterBridge", header.GetInterpreterToInterpreterBridgeOffset() },
180 { "interpreterToCompiledCodeBridge", header.GetInterpreterToCompiledCodeBridgeOffset() },
181 { "jniDlsymLookup", header.GetJniDlsymLookupOffset() },
182 { "quickGenericJniTrampoline", header.GetQuickGenericJniTrampolineOffset() },
183 { "quickImtConflictTrampoline", header.GetQuickImtConflictTrampolineOffset() },
184 { "quickResolutionTrampoline", header.GetQuickResolutionTrampolineOffset() },
185 { "quickToInterpreterBridge", header.GetQuickToInterpreterBridgeOffset() },
186 };
187 std::vector<MethodDebugInfo> result;
188 for (const auto& it : trampolines) {
189 if (it.second != 0) {
190 MethodDebugInfo info = MethodDebugInfo();
191 info.trampoline_name = it.first;
192 info.isa = header.GetInstructionSet();
193 info.is_code_address_text_relative = true;
194 info.code_address = it.second - header.GetExecutableOffset();
195 info.code_size = 0; // The symbol lasts until the next symbol.
196 result.push_back(std::move(info));
197 }
198 }
199 return result;
200}
201
David Srbeckyc5bfa972016-02-05 15:49:10 +0000202// Explicit instantiations
203template void WriteDebugInfo<ElfTypes32>(
204 ElfBuilder<ElfTypes32>* builder,
205 const ArrayRef<const MethodDebugInfo>& method_infos,
206 dwarf::CFIFormat cfi_format,
207 bool write_oat_patches);
208template void WriteDebugInfo<ElfTypes64>(
209 ElfBuilder<ElfTypes64>* builder,
210 const ArrayRef<const MethodDebugInfo>& method_infos,
211 dwarf::CFIFormat cfi_format,
212 bool write_oat_patches);
213
214} // namespace debug
215} // namespace art