blob: 33c46d7e1fd5fdcb2130930dbcbc3b3bc229ab51 [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>
David Srbecky56da23c2017-09-08 19:59:15 +010020#include <unordered_map>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021
David Brazdild9c90372016-09-14 16:53:55 +010022#include "base/array_ref.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000023#include "debug/dwarf/dwarf_constants.h"
24#include "debug/elf_compilation_unit.h"
25#include "debug/elf_debug_frame_writer.h"
26#include "debug/elf_debug_info_writer.h"
27#include "debug/elf_debug_line_writer.h"
28#include "debug/elf_debug_loc_writer.h"
29#include "debug/elf_gnu_debugdata_writer.h"
30#include "debug/elf_symtab_writer.h"
31#include "debug/method_debug_info.h"
Vladimir Marko74527972016-11-29 15:57:32 +000032#include "linker/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000033#include "linker/vector_output_stream.h"
Andreas Gamped4901292017-05-30 18:41:34 -070034#include "oat.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000035
36namespace art {
37namespace debug {
38
39template <typename ElfTypes>
Vladimir Marko74527972016-11-29 15:57:32 +000040void WriteDebugInfo(linker::ElfBuilder<ElfTypes>* builder,
David Srbeckyc5bfa972016-02-05 15:49:10 +000041 const ArrayRef<const MethodDebugInfo>& method_infos,
42 dwarf::CFIFormat cfi_format,
43 bool write_oat_patches) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000044 // Write .strtab and .symtab.
David Srbeckyc5bfa972016-02-05 15:49:10 +000045 WriteDebugSymbols(builder, method_infos, true /* with_signature */);
David Srbeckyc5bfa972016-02-05 15:49:10 +000046
David Srbecky09c2a6b2016-03-11 17:11:44 +000047 // Write .debug_frame.
48 WriteCFISection(builder, method_infos, cfi_format, write_oat_patches);
49
David Srbecky56da23c2017-09-08 19:59:15 +010050 // Group the methods into compilation units based on class.
51 std::unordered_map<const DexFile::ClassDef*, ElfCompilationUnit> class_to_compilation_unit;
David Srbeckyc5bfa972016-02-05 15:49:10 +000052 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);
David Srbecky56da23c2017-09-08 19:59:15 +010055 ElfCompilationUnit& cu = class_to_compilation_unit[&dex_class_def];
David Srbecky09c2a6b2016-03-11 17:11:44 +000056 cu.methods.push_back(&mi);
57 // All methods must have the same addressing mode otherwise the min/max below does not work.
58 DCHECK_EQ(cu.methods.front()->is_code_address_text_relative, mi.is_code_address_text_relative);
59 cu.is_code_address_text_relative = mi.is_code_address_text_relative;
60 cu.code_address = std::min(cu.code_address, mi.code_address);
61 cu.code_end = std::max(cu.code_end, mi.code_address + mi.code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +000062 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000063 }
64
David Srbecky56da23c2017-09-08 19:59:15 +010065 // Sort compilation units to make the compiler output deterministic.
66 std::vector<ElfCompilationUnit> compilation_units;
67 compilation_units.reserve(class_to_compilation_unit.size());
68 for (auto& it : class_to_compilation_unit) {
69 // The .debug_line section requires the methods to be sorted by code address.
70 std::stable_sort(it.second.methods.begin(),
71 it.second.methods.end(),
72 [](const MethodDebugInfo* a, const MethodDebugInfo* b) {
73 return a->code_address < b->code_address;
74 });
75 compilation_units.push_back(std::move(it.second));
76 }
77 std::sort(compilation_units.begin(),
78 compilation_units.end(),
79 [](ElfCompilationUnit& a, ElfCompilationUnit& b) {
80 // Sort by index of the first method within the method_infos array.
81 // This assumes that the order of method_infos is deterministic.
82 // Code address is not good for sorting due to possible duplicates.
83 return a.methods.front() < b.methods.front();
84 });
85
David Srbeckyc5bfa972016-02-05 15:49:10 +000086 // Write .debug_line section.
87 if (!compilation_units.empty()) {
88 ElfDebugLineWriter<ElfTypes> line_writer(builder);
89 line_writer.Start();
90 for (auto& compilation_unit : compilation_units) {
91 line_writer.WriteCompilationUnit(compilation_unit);
92 }
93 line_writer.End(write_oat_patches);
94 }
95
96 // Write .debug_info section.
97 if (!compilation_units.empty()) {
98 ElfDebugInfoWriter<ElfTypes> info_writer(builder);
99 info_writer.Start();
100 for (const auto& compilation_unit : compilation_units) {
101 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
102 cu_writer.Write(compilation_unit);
103 }
104 info_writer.End(write_oat_patches);
105 }
106}
107
108std::vector<uint8_t> MakeMiniDebugInfo(
109 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000110 const InstructionSetFeatures* features,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000111 size_t rodata_size,
112 size_t text_size,
113 const ArrayRef<const MethodDebugInfo>& method_infos) {
114 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000115 return MakeMiniDebugInfoInternal<ElfTypes64>(isa,
116 features,
117 rodata_size,
118 text_size,
119 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000120 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000121 return MakeMiniDebugInfoInternal<ElfTypes32>(isa,
122 features,
123 rodata_size,
124 text_size,
125 method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000126 }
127}
128
129template <typename ElfTypes>
Vladimir Marko93205e32016-04-13 11:59:46 +0100130static std::vector<uint8_t> WriteDebugElfFileForMethodsInternal(
David Srbeckyfe736b72016-03-09 11:44:44 +0000131 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000132 const InstructionSetFeatures* features,
David Srbeckyfe736b72016-03-09 11:44:44 +0000133 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000134 std::vector<uint8_t> buffer;
135 buffer.reserve(KB);
Vladimir Marko74527972016-11-29 15:57:32 +0000136 linker::VectorOutputStream out("Debug ELF file", &buffer);
137 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
138 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000139 // No program headers since the ELF file is not linked and has no allocated sections.
140 builder->Start(false /* write_program_headers */);
141 WriteDebugInfo(builder.get(),
David Srbeckyfe736b72016-03-09 11:44:44 +0000142 method_infos,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000143 dwarf::DW_DEBUG_FRAME_FORMAT,
144 false /* write_oat_patches */);
145 builder->End();
146 CHECK(builder->Good());
Vladimir Marko93205e32016-04-13 11:59:46 +0100147 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000148}
149
Vladimir Marko93205e32016-04-13 11:59:46 +0100150std::vector<uint8_t> WriteDebugElfFileForMethods(
David Srbeckyfe736b72016-03-09 11:44:44 +0000151 InstructionSet isa,
152 const InstructionSetFeatures* features,
153 const ArrayRef<const MethodDebugInfo>& method_infos) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000154 if (Is64BitInstructionSet(isa)) {
David Srbeckyfe736b72016-03-09 11:44:44 +0000155 return WriteDebugElfFileForMethodsInternal<ElfTypes64>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000156 } else {
David Srbeckyfe736b72016-03-09 11:44:44 +0000157 return WriteDebugElfFileForMethodsInternal<ElfTypes32>(isa, features, method_infos);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000158 }
159}
160
161template <typename ElfTypes>
Vladimir Marko93205e32016-04-13 11:59:46 +0100162static std::vector<uint8_t> WriteDebugElfFileForClassesInternal(
David Srbeckyfe736b72016-03-09 11:44:44 +0000163 InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +0000164 const InstructionSetFeatures* features,
165 const ArrayRef<mirror::Class*>& types)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700166 REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000167 std::vector<uint8_t> buffer;
168 buffer.reserve(KB);
Vladimir Marko74527972016-11-29 15:57:32 +0000169 linker::VectorOutputStream out("Debug ELF file", &buffer);
170 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder(
171 new linker::ElfBuilder<ElfTypes>(isa, features, &out));
David Srbeckyc5bfa972016-02-05 15:49:10 +0000172 // No program headers since the ELF file is not linked and has no allocated sections.
173 builder->Start(false /* write_program_headers */);
174 ElfDebugInfoWriter<ElfTypes> info_writer(builder.get());
175 info_writer.Start();
176 ElfCompilationUnitWriter<ElfTypes> cu_writer(&info_writer);
177 cu_writer.Write(types);
178 info_writer.End(false /* write_oat_patches */);
179
180 builder->End();
181 CHECK(builder->Good());
Vladimir Marko93205e32016-04-13 11:59:46 +0100182 return buffer;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000183}
184
Vladimir Marko93205e32016-04-13 11:59:46 +0100185std::vector<uint8_t> WriteDebugElfFileForClasses(InstructionSet isa,
186 const InstructionSetFeatures* features,
187 const ArrayRef<mirror::Class*>& types) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000188 if (Is64BitInstructionSet(isa)) {
David Srbecky5d811202016-03-08 13:21:22 +0000189 return WriteDebugElfFileForClassesInternal<ElfTypes64>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000190 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000191 return WriteDebugElfFileForClassesInternal<ElfTypes32>(isa, features, types);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000192 }
193}
194
David Srbeckyc5bfa972016-02-05 15:49:10 +0000195// Explicit instantiations
196template void WriteDebugInfo<ElfTypes32>(
Vladimir Marko74527972016-11-29 15:57:32 +0000197 linker::ElfBuilder<ElfTypes32>* builder,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000198 const ArrayRef<const MethodDebugInfo>& method_infos,
199 dwarf::CFIFormat cfi_format,
200 bool write_oat_patches);
201template void WriteDebugInfo<ElfTypes64>(
Vladimir Marko74527972016-11-29 15:57:32 +0000202 linker::ElfBuilder<ElfTypes64>* builder,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000203 const ArrayRef<const MethodDebugInfo>& method_infos,
204 dwarf::CFIFormat cfi_format,
205 bool write_oat_patches);
206
207} // namespace debug
208} // namespace art