| 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 Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 23 | #include "debug/debug_info.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 24 | #include "debug/method_debug_info.h" |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 25 | #include "dex/dex_file-inl.h" |
| 26 | #include "dex/code_item_accessors.h" |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 27 | #include "linker/elf_builder.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 28 | #include "utils.h" |
| 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. |
| 40 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 41 | |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 42 | // Magic name for .symtab symbols which enumerate dex files used |
| 43 | // by this ELF file (currently mmapped inside the .dex section). |
| 44 | constexpr const char* kDexFileSymbolName = "$dexfile"; |
| 45 | |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 46 | template <typename ElfTypes> |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 47 | static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 48 | bool mini_debug_info, |
| 49 | const DebugInfo& debug_info) { |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 50 | uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max(); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 51 | auto* strtab = builder->GetStrTab(); |
| 52 | auto* symtab = builder->GetSymTab(); |
| 53 | |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 54 | if (debug_info.Empty()) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
| David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 58 | // Find all addresses which contain deduped methods. |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 59 | // 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] | 60 | std::unordered_set<uint64_t> deduped_addresses; |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 61 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 62 | if (info.deduped) { |
| David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 63 | deduped_addresses.insert(info.code_address); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | strtab->Start(); |
| 68 | strtab->Write(""); // strtab should start with empty string. |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 69 | // Add symbols for compiled methods. |
| 70 | for (const MethodDebugInfo& info : debug_info.compiled_methods) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 71 | if (info.deduped) { |
| 72 | continue; // Add symbol only for the first instance. |
| 73 | } |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 74 | size_t name_offset; |
| David Srbecky | c684f33 | 2018-01-19 17:38:06 +0000 | [diff] [blame^] | 75 | if (!info.custom_name.empty()) { |
| 76 | name_offset = strtab->Write(info.custom_name); |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 77 | } else { |
| 78 | DCHECK(info.dex_file != nullptr); |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 79 | 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] | 80 | if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) { |
| 81 | name += " [DEDUPED]"; |
| 82 | } |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 83 | name_offset = strtab->Write(name); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 84 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 85 | |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 86 | const auto* text = builder->GetText(); |
| 87 | uint64_t address = info.code_address; |
| 88 | address += info.is_code_address_text_relative ? text->GetAddress() : 0; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 89 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 90 | address += CompiledMethod::CodeDelta(info.isa); |
| 91 | 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] | 92 | |
| 93 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 94 | // instructions, so that disassembler tools can correctly disassemble. |
| 95 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 96 | // requires it to match function symbol. Just address 0 does not work. |
| Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 97 | if (info.isa == InstructionSet::kThumb2) { |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 98 | if (address < mapping_symbol_address || !kGenerateSingleArmMappingSymbol) { |
| David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 99 | symtab->Add(strtab->Write("$t"), text, address & ~1, 0, STB_LOCAL, STT_NOTYPE); |
| David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 100 | mapping_symbol_address = address; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 103 | } |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 104 | // Add symbols for interpreted methods (with address range of the method's bytecode). |
| 105 | if (!debug_info.dex_files.empty() && builder->GetDex()->Exists()) { |
| 106 | auto dex = builder->GetDex(); |
| 107 | for (auto it : debug_info.dex_files) { |
| 108 | uint64_t dex_address = dex->GetAddress() + it.first /* offset within the section */; |
| 109 | const DexFile* dex_file = it.second; |
| David Srbecky | 510bb88 | 2018-01-17 23:38:49 +0000 | [diff] [blame] | 110 | typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName); |
| David Srbecky | 2593162 | 2018-01-18 22:55:20 +0000 | [diff] [blame] | 111 | 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] | 112 | if (mini_debug_info) { |
| 113 | continue; // Don't add interpreter method names to mini-debug-info for now. |
| 114 | } |
| 115 | for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { |
| 116 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); |
| 117 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 118 | if (class_data == nullptr) { |
| 119 | continue; |
| 120 | } |
| 121 | for (ClassDataItemIterator item(*dex_file, class_data); item.HasNext(); item.Next()) { |
| 122 | if (!item.IsAtMethod()) { |
| 123 | continue; |
| 124 | } |
| 125 | const DexFile::CodeItem* code_item = item.GetMethodCodeItem(); |
| 126 | if (code_item == nullptr) { |
| 127 | continue; |
| 128 | } |
| 129 | CodeItemInstructionAccessor code(*dex_file, code_item); |
| 130 | DCHECK(code.HasCodeItem()); |
| 131 | std::string name = dex_file->PrettyMethod(item.GetMemberIndex(), !mini_debug_info); |
| 132 | size_t name_offset = strtab->Write(name); |
| 133 | uint64_t offset = reinterpret_cast<const uint8_t*>(code.Insns()) - dex_file->Begin(); |
| 134 | uint64_t address = dex_address + offset; |
| 135 | size_t size = code.InsnsSizeInCodeUnits() * sizeof(uint16_t); |
| 136 | symtab->Add(name_offset, dex, address, size, STB_GLOBAL, STT_FUNC); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 141 | strtab->End(); |
| 142 | |
| 143 | // Symbols are buffered and written after names (because they are smaller). |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 144 | symtab->WriteCachedSection(); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace debug |
| 148 | } // namespace art |
| 149 | |
| 150 | #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_ |
| 151 | |