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> |
| 22 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 23 | #include "base/utils.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 24 | #include "debug/debug_info.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 25 | #include "debug/method_debug_info.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 26 | #include "dex/dex_file-inl.h" |
| 27 | #include "dex/code_item_accessors.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 28 | #include "elf/elf_builder.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace 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 Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 40 | // Note that ARM's Streamline requires it to match function symbol. |
| 41 | constexpr bool kGenerateArmMappingSymbol = true; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 42 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 43 | // Magic name for .symtab symbols which enumerate dex files used |
| 44 | // by this ELF file (currently mmapped inside the .dex section). |
| 45 | constexpr const char* kDexFileSymbolName = "$dexfile"; |
| 46 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 47 | template <typename ElfTypes> |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 48 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 49 | bool mini_debug_info, |
| 50 | const DebugInfo& debug_info) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 51 | uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max(); |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 52 | const auto* text = builder->GetText(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 53 | auto* strtab = builder->GetStrTab(); |
| 54 | auto* symtab = builder->GetSymTab(); |
| 55 | |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 56 | if (debug_info.Empty()) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 60 | // Find all addresses which contain deduped methods. |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 61 | // 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] | 62 | std::unordered_set<uint64_t> deduped_addresses; |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 63 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 64 | if (info.deduped) { |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 65 | deduped_addresses.insert(info.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 66 | } |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 67 | 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 Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | strtab->Start(); |
| 75 | strtab->Write(""); // strtab should start with empty string. |
David Srbecky | d2645a3 | 2018-02-16 16:16:39 +0000 | [diff] [blame] | 76 | // 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 Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 80 | // Add symbols for compiled methods. |
| 81 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 82 | if (info.deduped) { |
| 83 | continue; // Add symbol only for the first instance. |
| 84 | } |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 85 | size_t name_offset; |
David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame] | 86 | if (!info.custom_name.empty()) { |
| 87 | name_offset = strtab->Write(info.custom_name); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 88 | } else { |
| 89 | DCHECK(info.dex_file != nullptr); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 90 | 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] | 91 | if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) { |
Josh Gao | a5ed9a1 | 2020-06-17 19:24:21 -0700 | [diff] [blame] | 92 | name = "[DEDUPED] " + name; |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 93 | } |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 94 | name_offset = strtab->Write(name); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 95 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 96 | |
David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 97 | uint64_t address = info.code_address; |
| 98 | address += info.is_code_address_text_relative ? text->GetAddress() : 0; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 99 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 100 | address += CompiledMethod::CodeDelta(info.isa); |
| 101 | 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] | 102 | } |
David Srbecky | a996953 | 2018-02-05 16:00:55 +0000 | [diff] [blame] | 103 | // Add symbols for dex files. |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 104 | 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 Srbecky | 510bb88 | 2018-01-17 23:38:49 +0000 | [diff] [blame] | 109 | typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName); |
David Srbecky | 2593162 | 2018-01-18 22:55:20 +0000 | [diff] [blame] | 110 | 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] | 111 | } |
| 112 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 113 | strtab->End(); |
| 114 | |
| 115 | // Symbols are buffered and written after names (because they are smaller). |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 116 | symtab->WriteCachedSection(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace debug |
| 120 | } // namespace art |
| 121 | |
| 122 | #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_ |
| 123 | |