blob: 0ca7370f989e4ae4aa93f96740f07697257220cd [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
21#include "debug/dwarf/dwarf_constants.h"
22#include "debug/elf_compilation_unit.h"
23#include "debug/elf_debug_frame_writer.h"
24#include "debug/elf_debug_info_writer.h"
25#include "debug/elf_debug_line_writer.h"
26#include "debug/elf_debug_loc_writer.h"
27#include "debug/elf_gnu_debugdata_writer.h"
28#include "debug/elf_symtab_writer.h"
29#include "debug/method_debug_info.h"
30#include "elf_builder.h"
31#include "linker/vector_output_stream.h"
32#include "utils/array_ref.h"
33
34namespace art {
35namespace debug {
36
37template <typename ElfTypes>
38void WriteDebugInfo(ElfBuilder<ElfTypes>* builder,
39 const ArrayRef<const MethodDebugInfo>& method_infos,
40 dwarf::CFIFormat cfi_format,
41 bool write_oat_patches) {
42 // Add methods to .symtab.
43 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
44 // Generate CFI (stack unwinding information).
45 WriteCFISection(builder, method_infos, cfi_format, write_oat_patches);
46 // Write DWARF .debug_* sections.
47 WriteDebugSections(builder, method_infos, write_oat_patches);
48}
49
50template<typename ElfTypes>
51static void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
52 const ArrayRef<const MethodDebugInfo>& method_infos,
53 bool write_oat_patches) {
54 // Group the methods into compilation units based on source file.
55 std::vector<ElfCompilationUnit> compilation_units;
56 const char* last_source_file = nullptr;
57 for (const MethodDebugInfo& mi : method_infos) {
58 auto& dex_class_def = mi.dex_file->GetClassDef(mi.class_def_index);
59 const char* source_file = mi.dex_file->GetSourceFile(dex_class_def);
60 if (compilation_units.empty() || source_file != last_source_file) {
61 compilation_units.push_back(ElfCompilationUnit());
62 }
63 ElfCompilationUnit& cu = compilation_units.back();
64 cu.methods.push_back(&mi);
David Srbecky197160d2016-03-07 17:33:57 +000065 // All methods must have the same addressing mode otherwise the min/max below does not work.
66 DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative);
67 cu.is_code_address_text_relative = mi.is_code_address_text_relative;
68 cu.code_address = std::min(cu.code_address, mi.code_address);
69 cu.code_end = std::max(cu.code_end, mi.code_address + mi.code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +000070 last_source_file = source_file;
71 }
72
73 // Write .debug_line section.
74 if (!compilation_units.empty()) {
75 ElfDebugLineWriter<ElfTypes> line_writer(builder);
76 line_writer.Start();
77 for (auto& compilation_unit : compilation_units) {
78 line_writer.WriteCompilationUnit(compilation_unit);
79 }
80 line_writer.End(write_oat_patches);
81 }
82
83 // Write .debug_info section.
84 if (!compilation_units.empty()) {
85 ElfDebugInfoWriter<ElfTypes> info_writer(builder);
86 info_writer.Start();
87 for (const auto& compilation_unit : compilation_units) {
88 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
89 cu_writer.Write(compilation_unit);
90 }
91 info_writer.End(write_oat_patches);
92 }
93}
94
95std::vector<uint8_t> MakeMiniDebugInfo(
96 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000097 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +000098 size_t rodata_size,
99 size_t text_size,
100 const ArrayRef<const MethodDebugInfo>& method_infos) {
101 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000102 return MakeMiniDebugInfoInternal<ElfTypes64>(isa,
103 features,
104 rodata_size,
105 text_size,
106 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000107 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000108 return MakeMiniDebugInfoInternal<ElfTypes32>(isa,
109 features,
110 rodata_size,
111 text_size,
112 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000113 }
114}
115
116template <typename ElfTypes>
David Srbeckyfe736b72016-03-09 11:44:44 +0000117static ArrayRef<const uint8_t> WriteDebugElfFileForMethodsInternal(
118 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000119 const InstructionSetFeatures* features,
David Srbeckyfe736b72016-03-09 11:44:44 +0000120 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000121 std::vector<uint8_t> buffer;
122 buffer.reserve(KB);
123 VectorOutputStream out("Debug ELF file", &buffer);
David Srbecky5d811202016-03-08 13:21:22 +0000124 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000125 // No program headers since the ELF file is not linked and has no allocated sections.
126 builder->Start(false /* write_program_headers */);
127 WriteDebugInfo(builder.get(),
David Srbeckyfe736b72016-03-09 11:44:44 +0000128 method_infos,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000129 dwarf::DW_DEBUG_FRAME_FORMAT,
130 false /* write_oat_patches */);
131 builder->End();
132 CHECK(builder->Good());
133 // Make a copy of the buffer. We want to shrink it anyway.
134 uint8_t* result = new uint8_t[buffer.size()];
135 CHECK(result != nullptr);
136 memcpy(result, buffer.data(), buffer.size());
137 return ArrayRef<const uint8_t>(result, buffer.size());
138}
139
David Srbeckyfe736b72016-03-09 11:44:44 +0000140ArrayRef<const uint8_t> WriteDebugElfFileForMethods(
141 InstructionSet isa,
142 const InstructionSetFeatures* features,
143 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000144 if (Is64BitInstructionSet(isa)) {
David Srbeckyfe736b72016-03-09 11:44:44 +0000145 return WriteDebugElfFileForMethodsInternal<ElfTypes64>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000146 } else {
David Srbeckyfe736b72016-03-09 11:44:44 +0000147 return WriteDebugElfFileForMethodsInternal<ElfTypes32>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000148 }
149}
150
151template <typename ElfTypes>
152static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
David Srbeckyfe736b72016-03-09 11:44:44 +0000153 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000154 const InstructionSetFeatures* features,
155 const ArrayRef<mirror::Class*>& types)
David Srbeckyc5bfa972016-02-05 15:49:10 +0000156 SHARED_REQUIRES(Locks::mutator_lock_) {
157 std::vector<uint8_t> buffer;
158 buffer.reserve(KB);
159 VectorOutputStream out("Debug ELF file", &buffer);
David Srbecky5d811202016-03-08 13:21:22 +0000160 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000161 // No program headers since the ELF file is not linked and has no allocated sections.
162 builder->Start(false /* write_program_headers */);
163 ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
164 info_writer.Start();
165 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
166 cu_writer.Write(types);
167 info_writer.End(false /* write_oat_patches */);
168
169 builder->End();
170 CHECK(builder->Good());
171 // Make a copy of the buffer. We want to shrink it anyway.
172 uint8_t* result = new uint8_t[buffer.size()];
173 CHECK(result != nullptr);
174 memcpy(result, buffer.data(), buffer.size());
175 return ArrayRef<const uint8_t>(result, buffer.size());
176}
177
David Srbeckyfe736b72016-03-09 11:44:44 +0000178ArrayRef<const uint8_t> WriteDebugElfFileForClasses(InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000179 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000180 const ArrayRef<mirror::Class*>& types) {
181 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000182 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000183 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000184 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000185 }
186}
187
188// Explicit instantiations
189template void WriteDebugInfo<ElfTypes32>(
190 ElfBuilder<ElfTypes32>* builder,
191 const ArrayRef<const MethodDebugInfo>& method_infos,
192 dwarf::CFIFormat cfi_format,
193 bool write_oat_patches);
194template void WriteDebugInfo<ElfTypes64>(
195 ElfBuilder<ElfTypes64>* builder,
196 const ArrayRef<const MethodDebugInfo>& method_infos,
197 dwarf::CFIFormat cfi_format,
198 bool write_oat_patches);
199
200} // namespace debug
201} // namespace art