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 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max(); 43 auto* strtab = builder->GetStrTab(); 44 auto* symtab = builder->GetSymTab(); 45 46 if (method_infos.empty()) { 47 return; 48 } 49 50 // Find all addresses which contain deduped methods. 51 // The first instance of method is not marked deduped_, but the rest is. 52 std::unordered_set<uint64_t> deduped_addresses; 53 for (const MethodDebugInfo& info : method_infos) { 54 if (info.deduped) { 55 deduped_addresses.insert(info.code_address); 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 size_t name_offset; 68 if (info.trampoline_name != nullptr) { 69 name_offset = strtab->Write(info.trampoline_name); 70 } else { 71 DCHECK(info.dex_file != nullptr); 72 std::string name = PrettyMethod(info.dex_method_index, *info.dex_file, with_signature); 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; 80 } 81 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); 84 // Add in code delta, e.g., thumb bit 0 for Thumb2 code. 85 address += CompiledMethod::CodeDelta(info.isa); 86 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC); 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. 92 if (info.isa == kThumb2) { 93 if (address < mapping_symbol_address || !kGenerateSingleArmMappingSymbol) { 94 symtab->Add(strtab->Write("$t"), text, address & ~1, 0, STB_LOCAL, STT_NOTYPE); 95 mapping_symbol_address = address; 96 } 97 } 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 113