blob: 79069d1aa205849827904984d0834ca5439baf10 [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);
65 cu.low_pc = std::min(cu.low_pc, mi.low_pc);
66 cu.high_pc = std::max(cu.high_pc, mi.high_pc);
67 last_source_file = source_file;
68 }
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>
114static ArrayRef<const uint8_t> WriteDebugElfFileForMethodInternal(
David Srbecky5d811202016-03-08 13:21:22 +0000115 const InstructionSet isa,
116 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000117 const MethodDebugInfo& method_info) {
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(),
125 ArrayRef<const MethodDebugInfo>(&method_info, 1),
126 dwarf::DW_DEBUG_FRAME_FORMAT,
127 false /* write_oat_patches */);
128 builder->End();
129 CHECK(builder->Good());
130 // Make a copy of the buffer. We want to shrink it anyway.
131 uint8_t* result = new uint8_t[buffer.size()];
132 CHECK(result != nullptr);
133 memcpy(result, buffer.data(), buffer.size());
134 return ArrayRef<const uint8_t>(result, buffer.size());
135}
136
David Srbecky5d811202016-03-08 13:21:22 +0000137ArrayRef<const uint8_t> WriteDebugElfFileForMethod(const InstructionSet isa,
138 const InstructionSetFeatures* features,
139 const MethodDebugInfo& method_info) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000140 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000141 return WriteDebugElfFileForMethodInternal<ElfTypes64>(isa, features, method_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000142 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000143 return WriteDebugElfFileForMethodInternal<ElfTypes32>(isa, features, method_info);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000144 }
145}
146
147template <typename ElfTypes>
148static ArrayRef<const uint8_t> WriteDebugElfFileForClassesInternal(
David Srbecky5d811202016-03-08 13:21:22 +0000149 const InstructionSet isa,
150 const InstructionSetFeatures* features,
151 const ArrayRef<mirror::Class*>& types)
David Srbeckyc5bfa972016-02-05 15:49:10 +0000152 SHARED_REQUIRES(Locks::mutator_lock_) {
153 std::vector<uint8_t> buffer;
154 buffer.reserve(KB);
155 VectorOutputStream out("Debug ELF file", &buffer);
David Srbecky5d811202016-03-08 13:21:22 +0000156 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000157 // No program headers since the ELF file is not linked and has no allocated sections.
158 builder->Start(false /* write_program_headers */);
159 ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
160 info_writer.Start();
161 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
162 cu_writer.Write(types);
163 info_writer.End(false /* write_oat_patches */);
164
165 builder->End();
166 CHECK(builder->Good());
167 // Make a copy of the buffer. We want to shrink it anyway.
168 uint8_t* result = new uint8_t[buffer.size()];
169 CHECK(result != nullptr);
170 memcpy(result, buffer.data(), buffer.size());
171 return ArrayRef<const uint8_t>(result, buffer.size());
172}
173
174ArrayRef<const uint8_t> WriteDebugElfFileForClasses(const InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000175 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000176 const ArrayRef<mirror::Class*>& types) {
177 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000178 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000179 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000180 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000181 }
182}
183
184// Explicit instantiations
185template void WriteDebugInfo<ElfTypes32>(
186 ElfBuilder<ElfTypes32>* builder,
187 const ArrayRef<const MethodDebugInfo>& method_infos,
188 dwarf::CFIFormat cfi_format,
189 bool write_oat_patches);
190template void WriteDebugInfo<ElfTypes64>(
191 ElfBuilder<ElfTypes64>* builder,
192 const ArrayRef<const MethodDebugInfo>& method_infos,
193 dwarf::CFIFormat cfi_format,
194 bool write_oat_patches);
195
196} // namespace debug
197} // namespace art