blob: c09639ef74f7638bb3789e94778ca90759af14b7 [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>
22
David Sehrc431b9d2018-03-02 12:01:51 -080023#include "base/utils.h"
David Srbecky32210b92017-12-04 14:39:21 +000024#include "debug/debug_info.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000025#include "debug/method_debug_info.h"
David Srbecky32210b92017-12-04 14:39:21 +000026#include "dex/dex_file-inl.h"
27#include "dex/code_item_accessors.h"
David Srbecky2faab002019-02-12 16:35:48 +000028#include "elf/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000029
30namespace art {
31namespace debug {
32
33// The ARM specification defines three special mapping symbols
34// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
35// These symbols can be used by tools, for example, to pretty
36// print instructions correctly. Objdump will use them if they
37// exist, but it will still work well without them.
38// However, these extra symbols take space, so let's just generate
39// one symbol which marks the whole .text section as code.
David Srbeckyd2645a32018-02-16 16:16:39 +000040// Note that ARM's Streamline requires it to match function symbol.
41constexpr bool kGenerateArmMappingSymbol = true;
David Srbeckyc5bfa972016-02-05 15:49:10 +000042
David Srbecky32210b92017-12-04 14:39:21 +000043// Magic name for .symtab symbols which enumerate dex files used
44// by this ELF file (currently mmapped inside the .dex section).
45constexpr const char* kDexFileSymbolName = "$dexfile";
46
David Srbeckyc5bfa972016-02-05 15:49:10 +000047template <typename ElfTypes>
David Srbecky2faab002019-02-12 16:35:48 +000048static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
David Srbecky32210b92017-12-04 14:39:21 +000049 bool mini_debug_info,
50 const DebugInfo& debug_info) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000051 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
David Srbeckyd2645a32018-02-16 16:16:39 +000052 const auto* text = builder->GetText();
David Srbeckyc5bfa972016-02-05 15:49:10 +000053 auto* strtab = builder->GetStrTab();
54 auto* symtab = builder->GetSymTab();
55
David Srbecky32210b92017-12-04 14:39:21 +000056 if (debug_info.Empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000057 return;
58 }
59
David Srbecky197160d2016-03-07 17:33:57 +000060 // Find all addresses which contain deduped methods.
David Srbeckyc5bfa972016-02-05 15:49:10 +000061 // The first instance of method is not marked deduped_, but the rest is.
David Srbecky197160d2016-03-07 17:33:57 +000062 std::unordered_set<uint64_t> deduped_addresses;
David Srbecky32210b92017-12-04 14:39:21 +000063 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000064 if (info.deduped) {
David Srbecky197160d2016-03-07 17:33:57 +000065 deduped_addresses.insert(info.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +000066 }
David Srbeckyd2645a32018-02-16 16:16:39 +000067 if (kGenerateArmMappingSymbol && info.isa == InstructionSet::kThumb2) {
68 uint64_t address = info.code_address;
69 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
70 mapping_symbol_address = std::min(mapping_symbol_address, address);
71 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000072 }
73
74 strtab->Start();
75 strtab->Write(""); // strtab should start with empty string.
David Srbeckyd2645a32018-02-16 16:16:39 +000076 // Generate ARM mapping symbols. ELF local symbols must be added first.
77 if (mapping_symbol_address != std::numeric_limits<uint64_t>::max()) {
78 symtab->Add(strtab->Write("$t"), text, mapping_symbol_address, 0, STB_LOCAL, STT_NOTYPE);
79 }
David Srbecky32210b92017-12-04 14:39:21 +000080 // Add symbols for compiled methods.
81 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000082 if (info.deduped) {
83 continue; // Add symbol only for the first instance.
84 }
David Srbecky09c2a6b2016-03-11 17:11:44 +000085 size_t name_offset;
David Srbeckyc684f332018-01-19 17:38:06 +000086 if (!info.custom_name.empty()) {
87 name_offset = strtab->Write(info.custom_name);
David Srbecky09c2a6b2016-03-11 17:11:44 +000088 } else {
89 DCHECK(info.dex_file != nullptr);
David Srbecky32210b92017-12-04 14:39:21 +000090 std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info);
David Srbecky09c2a6b2016-03-11 17:11:44 +000091 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
Josh Gaoa5ed9a12020-06-17 19:24:21 -070092 name = "[DEDUPED] " + name;
David Srbecky09c2a6b2016-03-11 17:11:44 +000093 }
David Srbecky32210b92017-12-04 14:39:21 +000094 name_offset = strtab->Write(name);
David Srbeckyc5bfa972016-02-05 15:49:10 +000095 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000096
David Srbeckyf4886df2017-12-11 16:06:29 +000097 uint64_t address = info.code_address;
98 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
David Srbeckyc5bfa972016-02-05 15:49:10 +000099 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
David Srbecky197160d2016-03-07 17:33:57 +0000100 address += CompiledMethod::CodeDelta(info.isa);
101 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000102 }
David Srbeckya9969532018-02-05 16:00:55 +0000103 // Add symbols for dex files.
David Srbecky32210b92017-12-04 14:39:21 +0000104 if (!debug_info.dex_files.empty() && builder->GetDex()->Exists()) {
105 auto dex = builder->GetDex();
106 for (auto it : debug_info.dex_files) {
107 uint64_t dex_address = dex->GetAddress() + it.first /* offset within the section */;
108 const DexFile* dex_file = it.second;
David Srbecky510bb882018-01-17 23:38:49 +0000109 typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName);
David Srbecky25931622018-01-18 22:55:20 +0000110 symtab->Add(dex_name, dex, dex_address, dex_file->Size(), STB_GLOBAL, STT_FUNC);
David Srbecky32210b92017-12-04 14:39:21 +0000111 }
112 }
David Srbeckyc5bfa972016-02-05 15:49:10 +0000113 strtab->End();
114
115 // Symbols are buffered and written after names (because they are smaller).
David Srbecky32210b92017-12-04 14:39:21 +0000116 symtab->WriteCachedSection();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000117}
118
119} // namespace debug
120} // namespace art
121
122#endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
123