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