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 | |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #include "debug/method_debug_info.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 23 | #include "linker/elf_builder.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 24 | #include "utils.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace debug { |
| 28 | |
| 29 | // The ARM specification defines three special mapping symbols |
| 30 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 31 | // These symbols can be used by tools, for example, to pretty |
| 32 | // print instructions correctly. Objdump will use them if they |
| 33 | // exist, but it will still work well without them. |
| 34 | // However, these extra symbols take space, so let's just generate |
| 35 | // one symbol which marks the whole .text section as code. |
| 36 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 37 | |
| 38 | template <typename ElfTypes> |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 39 | static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 40 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 41 | bool with_signature) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 42 | uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max(); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 43 | auto* strtab = builder->GetStrTab(); |
| 44 | auto* symtab = builder->GetSymTab(); |
| 45 | |
| 46 | if (method_infos.empty()) { |
| 47 | return; |
| 48 | } |
| 49 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 50 | // Find all addresses which contain deduped methods. |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 51 | // 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] | 52 | std::unordered_set<uint64_t> deduped_addresses; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 53 | for (const MethodDebugInfo& info : method_infos) { |
| 54 | if (info.deduped) { |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 55 | deduped_addresses.insert(info.code_address); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | strtab->Start(); |
| 60 | strtab->Write(""); // strtab should start with empty string. |
| 61 | std::string last_name; |
| 62 | size_t last_name_offset = 0; |
| 63 | for (const MethodDebugInfo& info : method_infos) { |
| 64 | if (info.deduped) { |
| 65 | continue; // Add symbol only for the first instance. |
| 66 | } |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 67 | size_t name_offset; |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 68 | if (!info.trampoline_name.empty()) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 69 | name_offset = strtab->Write(info.trampoline_name); |
| 70 | } else { |
| 71 | DCHECK(info.dex_file != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 72 | std::string name = info.dex_file->PrettyMethod(info.dex_method_index, with_signature); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 73 | if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) { |
| 74 | name += " [DEDUPED]"; |
| 75 | } |
| 76 | // If we write method names without signature, we might see the same name multiple times. |
| 77 | name_offset = (name == last_name ? last_name_offset : strtab->Write(name)); |
| 78 | last_name = std::move(name); |
| 79 | last_name_offset = name_offset; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 80 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 81 | |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 82 | const auto* text = info.is_code_address_text_relative ? builder->GetText() : nullptr; |
| 83 | uint64_t address = info.code_address + (text != nullptr ? text->GetAddress() : 0); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 84 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 85 | address += CompiledMethod::CodeDelta(info.isa); |
| 86 | 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] | 87 | |
| 88 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 89 | // instructions, so that disassembler tools can correctly disassemble. |
| 90 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 91 | // requires it to match function symbol. Just address 0 does not work. |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 92 | if (info.isa == kThumb2) { |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 93 | if (address < mapping_symbol_address || !kGenerateSingleArmMappingSymbol) { |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 94 | 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] | 95 | mapping_symbol_address = address; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 98 | } |
| 99 | strtab->End(); |
| 100 | |
| 101 | // Symbols are buffered and written after names (because they are smaller). |
| 102 | // We could also do two passes in this function to avoid the buffering. |
| 103 | symtab->Start(); |
| 104 | symtab->Write(); |
| 105 | symtab->End(); |
| 106 | } |
| 107 | |
| 108 | } // namespace debug |
| 109 | } // namespace art |
| 110 | |
| 111 | #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_ |
| 112 | |