blob: 9c9e8b35b8275b521711c85a916c8d630653a2c7 [file] [log] [blame]
David Srbeckyc5bfa972016-02-05 15:49:10 +00001/*
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 Srbecky32210b92017-12-04 14:39:21 +000020#include <map>
David Srbeckyc5bfa972016-02-05 15:49:10 +000021#include <unordered_set>
22
David Srbecky32210b92017-12-04 14:39:21 +000023#include "debug/debug_info.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000024#include "debug/method_debug_info.h"
David Srbecky32210b92017-12-04 14:39:21 +000025#include "dex/dex_file-inl.h"
26#include "dex/code_item_accessors.h"
Vladimir Marko74527972016-11-29 15:57:32 +000027#include "linker/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000028#include "utils.h"
29
30namespace art {
31namespace 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.
40constexpr bool kGenerateSingleArmMappingSymbol = true;
41
David Srbecky32210b92017-12-04 14:39:21 +000042// Magic name for .symtab symbols which enumerate dex files used
43// by this ELF file (currently mmapped inside the .dex section).
44constexpr const char* kDexFileSymbolName = "$dexfile";
45
David Srbeckyc5bfa972016-02-05 15:49:10 +000046template <typename ElfTypes>
Vladimir Marko74527972016-11-29 15:57:32 +000047static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder,
David Srbecky32210b92017-12-04 14:39:21 +000048 bool mini_debug_info,
49 const DebugInfo& debug_info) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000050 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
David Srbeckyc5bfa972016-02-05 15:49:10 +000051 auto* strtab = builder->GetStrTab();
52 auto* symtab = builder->GetSymTab();
53
David Srbecky32210b92017-12-04 14:39:21 +000054 if (debug_info.Empty()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000055 return;
56 }
57
David Srbecky197160d2016-03-07 17:33:57 +000058 // Find all addresses which contain deduped methods.
David Srbeckyc5bfa972016-02-05 15:49:10 +000059 // The first instance of method is not marked deduped_, but the rest is.
David Srbecky197160d2016-03-07 17:33:57 +000060 std::unordered_set<uint64_t> deduped_addresses;
David Srbecky32210b92017-12-04 14:39:21 +000061 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000062 if (info.deduped) {
David Srbecky197160d2016-03-07 17:33:57 +000063 deduped_addresses.insert(info.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +000064 }
65 }
66
67 strtab->Start();
68 strtab->Write(""); // strtab should start with empty string.
David Srbecky32210b92017-12-04 14:39:21 +000069 // Add symbols for compiled methods.
70 for (const MethodDebugInfo& info : debug_info.compiled_methods) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000071 if (info.deduped) {
72 continue; // Add symbol only for the first instance.
73 }
David Srbecky09c2a6b2016-03-11 17:11:44 +000074 size_t name_offset;
David Srbeckyc684f332018-01-19 17:38:06 +000075 if (!info.custom_name.empty()) {
76 name_offset = strtab->Write(info.custom_name);
David Srbecky09c2a6b2016-03-11 17:11:44 +000077 } else {
78 DCHECK(info.dex_file != nullptr);
David Srbecky32210b92017-12-04 14:39:21 +000079 std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info);
David Srbecky09c2a6b2016-03-11 17:11:44 +000080 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
81 name += " [DEDUPED]";
82 }
David Srbecky32210b92017-12-04 14:39:21 +000083 name_offset = strtab->Write(name);
David Srbeckyc5bfa972016-02-05 15:49:10 +000084 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000085
David Srbeckyf4886df2017-12-11 16:06:29 +000086 const auto* text = builder->GetText();
87 uint64_t address = info.code_address;
88 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
David Srbeckyc5bfa972016-02-05 15:49:10 +000089 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
David Srbecky197160d2016-03-07 17:33:57 +000090 address += CompiledMethod::CodeDelta(info.isa);
91 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
David Srbeckyc5bfa972016-02-05 15:49:10 +000092
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 Marko33bff252017-11-01 14:35:42 +000097 if (info.isa == InstructionSet::kThumb2) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000098 if (address < mapping_symbol_address || !kGenerateSingleArmMappingSymbol) {
David Srbecky197160d2016-03-07 17:33:57 +000099 symtab->Add(strtab->Write("$t"), text, address & ~1, 0, STB_LOCAL, STT_NOTYPE);
David Srbecky09c2a6b2016-03-11 17:11:44 +0000100 mapping_symbol_address = address;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000101 }
102 }
David Srbeckyc5bfa972016-02-05 15:49:10 +0000103 }
David Srbecky32210b92017-12-04 14:39:21 +0000104 // 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 Srbecky510bb882018-01-17 23:38:49 +0000110 typename ElfTypes::Word dex_name = strtab->Write(kDexFileSymbolName);
David Srbecky25931622018-01-18 22:55:20 +0000111 symtab->Add(dex_name, dex, dex_address, dex_file->Size(), STB_GLOBAL, STT_FUNC);
David Srbecky32210b92017-12-04 14:39:21 +0000112 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 Srbeckyc5bfa972016-02-05 15:49:10 +0000141 strtab->End();
142
143 // Symbols are buffered and written after names (because they are smaller).
David Srbecky32210b92017-12-04 14:39:21 +0000144 symtab->WriteCachedSection();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000145}
146
147} // namespace debug
148} // namespace art
149
150#endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
151