1 /*
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_SYMTAB_WRITER_H_
18 #define ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
19
20 #include <map>
21 #include <unordered_set>
22 #include <unordered_map>
23
24 #include "base/macros.h"
25 #include "base/utils.h"
26 #include "debug/debug_info.h"
27 #include "debug/method_debug_info.h"
28 #include "dex/code_item_accessors.h"
29 #include "dex/descriptors_names.h"
30 #include "dex/dex_file-inl.h"
31 #include "elf/elf_builder.h"
32
33 namespace art HIDDEN {
34 namespace debug {
35
36 // The ARM specification defines three special mapping symbols
37 // $a, $t and $d which mark ARM, Thumb and data ranges respectively.
38 // These symbols can be used by tools, for example, to pretty
39 // print instructions correctly. Objdump will use them if they
40 // exist, but it will still work well without them.
41 // However, these extra symbols take space, so let's just generate
42 // one symbol which marks the whole .text section as code.
43 // Note that ARM's Streamline requires it to match function symbol.
44 constexpr bool kGenerateArmMappingSymbol = true;
45
46 // Create magic symbol to let libunwindstack know that symtab is sorted by address.
47 constexpr bool kGenerateSortedSymbol = true;
48 constexpr const char kSortedSymbolName[] = "$android.symtab.sorted";
49 constexpr size_t kSortedSymbolMinCount = 100; // Don't bother if the table is very small (JIT).
50
51 // Magic name for .symtab symbols which enumerate dex files used
52 // by this ELF file (currently mmapped inside the .dex section).
53 constexpr const char* kDexFileSymbolName = "$dexfile";
54
55 // Return common parts of method names; shared by all methods in the given set.
56 // (e.g. "[DEDUPED] ?.<init>" or "com.android.icu.charset.CharsetEncoderICU.?")
GetDedupedName(const std::vector<const MethodDebugInfo * > & methods,std::string * out)57 static void GetDedupedName(const std::vector<const MethodDebugInfo*>& methods, std::string* out) {
58 DCHECK(!methods.empty());
59 const MethodDebugInfo* first = methods.front();
60 auto is_same_class = [&first](const MethodDebugInfo* mi) {
61 DCHECK(mi->dex_file != nullptr);
62 return mi->dex_file == first->dex_file && mi->class_def_index == first->class_def_index;
63 };
64 auto is_same_method_name = [&first](const MethodDebugInfo* mi) {
65 return strcmp(mi->dex_file->GetMethodName(mi->dex_method_index),
66 first->dex_file->GetMethodName(first->dex_method_index)) == 0;
67 };
68 bool all_same_class = std::all_of(methods.begin(), methods.end(), is_same_class);
69 bool all_same_method_name = std::all_of(methods.begin(), methods.end(), is_same_method_name);
70 *out = "[DEDUPED]";
71 if (all_same_class || all_same_method_name) {
72 *out += ' ';
73 if (all_same_class) {
74 auto& dex_class_def = first->dex_file->GetClassDef(first->class_def_index);
75 AppendPrettyDescriptor(first->dex_file->GetClassDescriptor(dex_class_def), &*out);
76 } else {
77 *out += '?';
78 }
79 *out += '.';
80 if (all_same_method_name) {
81 *out += first->dex_file->GetMethodName(first->dex_method_index);
82 } else {
83 *out += '?';
84 }
85 }
86 }
87
88 template <typename ElfTypes>
WriteDebugSymbols(ElfBuilder<ElfTypes> * builder,bool mini_debug_info,const DebugInfo & debug_info)89 static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
90 bool mini_debug_info,
91 const DebugInfo& debug_info) {
92 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
93 const auto* text = builder->GetText();
94 auto* strtab = builder->GetStrTab();
95 auto* symtab = builder->GetSymTab();
96
97 if (debug_info.Empty()) {
98 return;
99 }
100
101 // Find all addresses which contain deduped methods.
102 // The first instance of method is not marked deduped_, but the rest is.
103 std::unordered_set<uint64_t> deduped_addresses;
104 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
105 if (info.deduped) {
106 deduped_addresses.insert(info.code_address);
107 }
108 if (kGenerateArmMappingSymbol && info.isa == InstructionSet::kThumb2) {
109 uint64_t address = info.code_address;
110 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
111 mapping_symbol_address = std::min(mapping_symbol_address, address);
112 }
113 }
114
115 // Create list of deduped methods per function address.
116 // We have to do it separately since the first method does not have the deduped flag.
117 std::unordered_map<uint64_t, std::vector<const MethodDebugInfo*>> deduped_methods;
118 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
119 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
120 deduped_methods[info.code_address].push_back(&info);
121 }
122 }
123
124 strtab->Start();
125 // Generate marker to annotate the symbol table as sorted (guaranteed by the ElfBuilder).
126 // Note that LOCAL symbols are sorted before GLOBAL ones, so don't mix the two types.
127 if (kGenerateSortedSymbol && debug_info.compiled_methods.size() >= kSortedSymbolMinCount) {
128 symtab->Add(strtab->Write(kSortedSymbolName), nullptr, 0, 0, STB_GLOBAL, STT_NOTYPE);
129 }
130 // Generate ARM mapping symbols. ELF local symbols must be added first.
131 if (mapping_symbol_address != std::numeric_limits<uint64_t>::max()) {
132 symtab->Add(strtab->Write("$t"), text, mapping_symbol_address, 0, STB_GLOBAL, STT_NOTYPE);
133 }
134 // Add symbols for compiled methods.
135 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
136 if (info.deduped) {
137 continue; // Add symbol only for the first instance.
138 }
139 size_t name_offset;
140 if (!info.custom_name.empty()) {
141 name_offset = strtab->Write(info.custom_name);
142 } else {
143 DCHECK(info.dex_file != nullptr);
144 std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info);
145 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
146 // Create method name common to all the deduped methods if possible.
147 // Around half of the time, there is either common class or method name.
148 // NB: We used to return one method at random with tag, but developers found it confusing.
149 GetDedupedName(deduped_methods[info.code_address], &name);
150 }
151 name_offset = strtab->Write(name);
152 }
153
154 uint64_t address = info.code_address;
155 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
156 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
157 address += GetInstructionSetEntryPointAdjustment(info.isa);
158 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
159 }
160 // Add symbols for dex files.
161 if (!debug_info.dex_files.empty() && builder->GetDex()->Exists()) {
162 auto dex = builder->GetDex();
163 for (auto it : debug_info.dex_files) {
164 uint64_t dex_address = dex->GetAddress() + it.first /* offset within the section */;
165 const DexFile* dex_file = it.second;
166 typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName);
167 symtab->Add(dex_name, dex, dex_address, dex_file->Size(), STB_GLOBAL, STT_FUNC);
168 }
169 }
170 strtab->End();
171
172 // Symbols are buffered and written after names (because they are smaller).
173 symtab->WriteCachedSection();
174 }
175
176 } // namespace debug
177 } // namespace art
178
179 #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
180
181