David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 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 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 20 | #include <map> |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 21 | #include <unordered_set> |
David Srbecky | 23c926d | 2020-11-05 12:02:00 +0000 | [diff] [blame] | 22 | #include <unordered_map> |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 23 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/utils.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 25 | #include "debug/debug_info.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 26 | #include "debug/method_debug_info.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 27 | #include "dex/code_item_accessors.h" |
David Srbecky | 23c926d | 2020-11-05 12:02:00 +0000 | [diff] [blame] | 28 | #include "dex/descriptors_names.h" |
| 29 | #include "dex/dex_file-inl.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 30 | #include "elf/elf_builder.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 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. |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 42 | // Note that ARM's Streamline requires it to match function symbol. |
| 43 | constexpr bool kGenerateArmMappingSymbol = true; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 44 | |
David Srbecky | 88e08c0 | 2021-04-29 12:42:22 +0100 | [diff] [blame] | 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 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 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 | |
David Srbecky | 23c926d | 2020-11-05 12:02:00 +0000 | [diff] [blame] | 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.?") |
| 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 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 87 | template <typename ElfTypes> |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 88 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 89 | bool mini_debug_info, |
| 90 | const DebugInfo& debug_info) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 91 | uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max(); |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 92 | const auto* text = builder->GetText(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 93 | auto* strtab = builder->GetStrTab(); |
| 94 | auto* symtab = builder->GetSymTab(); |
| 95 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 96 | if (debug_info.Empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 100 | // Find all addresses which contain deduped methods. |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 101 | // The first instance of method is not marked deduped_, but the rest is. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 102 | std::unordered_set<uint64_t> deduped_addresses; |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 103 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 104 | if (info.deduped) { |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 105 | deduped_addresses.insert(info.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 106 | } |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 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 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 112 | } |
| 113 | |
David Srbecky | 23c926d | 2020-11-05 12:02:00 +0000 | [diff] [blame] | 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 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 123 | strtab->Start(); |
David Srbecky | 88e08c0 | 2021-04-29 12:42:22 +0100 | [diff] [blame] | 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 | } |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 129 | // Generate ARM mapping symbols. ELF local symbols must be added first. |
| 130 | if (mapping_symbol_address != std::numeric_limits<uint64_t>::max()) { |
David Srbecky | 88e08c0 | 2021-04-29 12:42:22 +0100 | [diff] [blame] | 131 | symtab->Add(strtab->Write("$t"), text, mapping_symbol_address, 0, STB_GLOBAL, STT_NOTYPE); |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 132 | } |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 133 | // Add symbols for compiled methods. |
| 134 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 135 | if (info.deduped) { |
| 136 | continue; // Add symbol only for the first instance. |
| 137 | } |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 138 | size_t name_offset; |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 139 | if (!info.custom_name.empty()) { |
| 140 | name_offset = strtab->Write(info.custom_name); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 141 | } else { |
| 142 | DCHECK(info.dex_file != nullptr); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 143 | std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 144 | if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) { |
David Srbecky | 23c926d | 2020-11-05 12:02:00 +0000 | [diff] [blame] | 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); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 149 | } |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 150 | name_offset = strtab->Write(name); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 151 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 152 | |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 153 | uint64_t address = info.code_address; |
| 154 | address += info.is_code_address_text_relative ? text->GetAddress() : 0; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 155 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
Vladimir Marko | 467d570 | 2022-09-30 12:28:49 +0200 | [diff] [blame] | 156 | address += GetInstructionSetEntryPointAdjustment(info.isa); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 157 | symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 158 | } |
David Srbecky | a996953 | 2018-02-05 16:00:55 +0000 | [diff] [blame] | 159 | // Add symbols for dex files. |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 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; |
David Srbecky | 510bb88 | 2018-01-17 23:38:49 +0000 | [diff] [blame] | 165 | typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName); |
David Srbecky | 2593162 | 2018-01-18 22:55:20 +0000 | [diff] [blame] | 166 | symtab->Add(dex_name, dex, dex_address, dex_file->Size(), STB_GLOBAL, STT_FUNC); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 169 | strtab->End(); |
| 170 | |
| 171 | // Symbols are buffered and written after names (because they are smaller). |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 172 | symtab->WriteCachedSection(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace debug |
| 176 | } // namespace art |
| 177 | |
| 178 | #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_ |
| 179 | |