blob: d2acd64c2a9b0b6c6d6afbab8deb10b0892276f1 [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#ifndef ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
18#define ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
19
David Srbecky32210b92017-12-04 14:39:21 +000020#include <map>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021#include <unordered_set>
David Srbecky23c926d2020-11-05 12:02:00 +000022#include <unordered_map>
David Srbeckyc5bfa972016-02-05 15:49:10 +000023
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
David Srbecky32210b92017-12-04 14:39:21 +000025#include "debug/debug_info.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000026#include "debug/method_debug_info.h"
David Srbecky32210b92017-12-04 14:39:21 +000027#include "dex/code_item_accessors.h"
David Srbecky23c926d2020-11-05 12:02:00 +000028#include "dex/descriptors_names.h"
29#include "dex/dex_file-inl.h"
David Srbecky2faab002019-02-12 16:35:48 +000030#include "elf/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000031
32namespace art {
33namespace 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 Srbeckyd2645a32018-02-16 16:16:39 +000042// Note that ARM's Streamline requires it to match function symbol.
43constexpr bool kGenerateArmMappingSymbol = true;
David Srbeckyc5bfa972016-02-05 15:49:10 +000044
David Srbecky88e08c02021-04-29 12:42:22 +010045// Create magic symbol to let libunwindstack know that symtab is sorted by address.
46constexpr bool kGenerateSortedSymbol = true;
47constexpr const char kSortedSymbolName[] = "$android.symtab.sorted";
48constexpr size_t kSortedSymbolMinCount = 100; // Don't bother if the table is very small (JIT).
49
David Srbecky32210b92017-12-04 14:39:21 +000050// Magic name for .symtab symbols which enumerate dex files used
51// by this ELF file (currently mmapped inside the .dex section).
52constexpr const char* kDexFileSymbolName = "$dexfile";
53
David Srbecky23c926d2020-11-05 12:02:00 +000054// 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.?")
56static 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 Srbeckyc5bfa972016-02-05 15:49:10 +000087template <typename ElfTypes>
David Srbecky2faab002019-02-12 16:35:48 +000088static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
David Srbecky32210b92017-12-04 14:39:21 +000089 bool mini_debug_info,
90 const DebugInfo& debug_info) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000091 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
David Srbeckyd2645a32018-02-16 16:16:39 +000092 const auto* text = builder->GetText();
David Srbeckyc5bfa972016-02-05 15:49:10 +000093 auto* strtab = builder->GetStrTab();
94 auto* symtab = builder->GetSymTab();
95
David Srbecky32210b92017-12-04 14:39:21 +000096 if (debug_info.Empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000097 return;
98 }
99
David Srbecky197160d2016-03-07 17:33:57 +0000100 // Find all addresses which contain deduped methods.
David Srbeckyc5bfa972016-02-05 15:49:10 +0000101 // The first instance of method is not marked deduped_, but the rest is.
David Srbecky197160d2016-03-07 17:33:57 +0000102 std::unordered_set<uint64_t> deduped_addresses;
David Srbecky32210b92017-12-04 14:39:21 +0000103 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000104 if (info.deduped) {
David Srbecky197160d2016-03-07 17:33:57 +0000105 deduped_addresses.insert(info.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000106 }
David Srbeckyd2645a32018-02-16 16:16:39 +0000107 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 Srbeckyc5bfa972016-02-05 15:49:10 +0000112 }
113
David Srbecky23c926d2020-11-05 12:02:00 +0000114 // 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 Srbeckyc5bfa972016-02-05 15:49:10 +0000123 strtab->Start();
David Srbecky88e08c02021-04-29 12:42:22 +0100124 // 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 Srbeckyd2645a32018-02-16 16:16:39 +0000129 // Generate ARM mapping symbols. ELF local symbols must be added first.
130 if (mapping_symbol_address != std::numeric_limits<uint64_t>::max()) {
David Srbecky88e08c02021-04-29 12:42:22 +0100131 symtab->Add(strtab->Write("$t"), text, mapping_symbol_address, 0, STB_GLOBAL, STT_NOTYPE);
David Srbeckyd2645a32018-02-16 16:16:39 +0000132 }
David Srbecky32210b92017-12-04 14:39:21 +0000133 // Add symbols for compiled methods.
134 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000135 if (info.deduped) {
136 continue; // Add symbol only for the first instance.
137 }
David Srbecky09c2a6b2016-03-11 17:11:44 +0000138 size_t name_offset;
David Srbeckyc684f332018-01-19 17:38:06 +0000139 if (!info.custom_name.empty()) {
140 name_offset = strtab->Write(info.custom_name);
David Srbecky09c2a6b2016-03-11 17:11:44 +0000141 } else {
142 DCHECK(info.dex_file != nullptr);
David Srbecky32210b92017-12-04 14:39:21 +0000143 std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info);
David Srbecky09c2a6b2016-03-11 17:11:44 +0000144 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
David Srbecky23c926d2020-11-05 12:02:00 +0000145 // 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 Srbecky09c2a6b2016-03-11 17:11:44 +0000149 }
David Srbecky32210b92017-12-04 14:39:21 +0000150 name_offset = strtab->Write(name);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000151 }
David Srbeckyc5bfa972016-02-05 15:49:10 +0000152
David Srbeckyf4886df2017-12-11 16:06:29 +0000153 uint64_t address = info.code_address;
154 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000155 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
Vladimir Marko467d5702022-09-30 12:28:49 +0200156 address += GetInstructionSetEntryPointAdjustment(info.isa);
David Srbecky197160d2016-03-07 17:33:57 +0000157 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000158 }
David Srbeckya9969532018-02-05 16:00:55 +0000159 // Add symbols for dex files.
David Srbecky32210b92017-12-04 14:39:21 +0000160 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 Srbecky510bb882018-01-17 23:38:49 +0000165 typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName);
David Srbecky25931622018-01-18 22:55:20 +0000166 symtab->Add(dex_name, dex, dex_address, dex_file->Size(), STB_GLOBAL, STT_FUNC);
David Srbecky32210b92017-12-04 14:39:21 +0000167 }
168 }
David Srbeckyc5bfa972016-02-05 15:49:10 +0000169 strtab->End();
170
171 // Symbols are buffered and written after names (because they are smaller).
David Srbecky32210b92017-12-04 14:39:21 +0000172 symtab->WriteCachedSection();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000173}
174
175} // namespace debug
176} // namespace art
177
178#endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
179