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" |
| 23 | #include "elf_builder.h" |
| 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> |
| 39 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 40 | const ArrayRef<const MethodDebugInfo>& method_infos, |
| 41 | bool with_signature) { |
| 42 | bool generated_mapping_symbol = false; |
| 43 | auto* strtab = builder->GetStrTab(); |
| 44 | auto* symtab = builder->GetSymTab(); |
| 45 | |
| 46 | if (method_infos.empty()) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Find all addresses (low_pc) which contain deduped methods. |
| 51 | // The first instance of method is not marked deduped_, but the rest is. |
| 52 | std::unordered_set<uint32_t> deduped_addresses; |
| 53 | for (const MethodDebugInfo& info : method_infos) { |
| 54 | if (info.deduped) { |
| 55 | deduped_addresses.insert(info.low_pc); |
| 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 | } |
| 67 | std::string name = PrettyMethod(info.dex_method_index, *info.dex_file, with_signature); |
| 68 | if (deduped_addresses.find(info.low_pc) != deduped_addresses.end()) { |
| 69 | name += " [DEDUPED]"; |
| 70 | } |
| 71 | // If we write method names without signature, we might see the same name multiple times. |
| 72 | size_t name_offset = (name == last_name ? last_name_offset : strtab->Write(name)); |
| 73 | |
| 74 | const auto* text = builder->GetText()->Exists() ? builder->GetText() : nullptr; |
| 75 | const bool is_relative = (text != nullptr); |
| 76 | uint32_t low_pc = info.low_pc; |
| 77 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 78 | low_pc += info.compiled_method->CodeDelta(); |
| 79 | symtab->Add(name_offset, |
| 80 | text, |
| 81 | low_pc, |
| 82 | is_relative, |
| 83 | info.high_pc - info.low_pc, |
| 84 | STB_GLOBAL, |
| 85 | STT_FUNC); |
| 86 | |
| 87 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 88 | // instructions, so that disassembler tools can correctly disassemble. |
| 89 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 90 | // requires it to match function symbol. Just address 0 does not work. |
| 91 | if (info.compiled_method->GetInstructionSet() == kThumb2) { |
| 92 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
| 93 | symtab->Add(strtab->Write("$t"), text, info.low_pc & ~1, |
| 94 | is_relative, 0, STB_LOCAL, STT_NOTYPE); |
| 95 | generated_mapping_symbol = true; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | last_name = std::move(name); |
| 100 | last_name_offset = name_offset; |
| 101 | } |
| 102 | strtab->End(); |
| 103 | |
| 104 | // Symbols are buffered and written after names (because they are smaller). |
| 105 | // We could also do two passes in this function to avoid the buffering. |
| 106 | symtab->Start(); |
| 107 | symtab->Write(); |
| 108 | symtab->End(); |
| 109 | } |
| 110 | |
| 111 | } // namespace debug |
| 112 | } // namespace art |
| 113 | |
| 114 | #endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_ |
| 115 | |