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