blob: 57e010f2323c63879d8685471e9b0d01aa07321f [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
20#include <unordered_set>
21
22#include "debug/method_debug_info.h"
Vladimir Marko74527972016-11-29 15:57:32 +000023#include "linker/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000024#include "utils.h"
25
26namespace art {
27namespace 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.
36constexpr bool kGenerateSingleArmMappingSymbol = true;
37
38template <typename ElfTypes>
Vladimir Marko74527972016-11-29 15:57:32 +000039static void WriteDebugSymbols(linker::ElfBuilder<ElfTypes>* builder,
David Srbeckyc5bfa972016-02-05 15:49:10 +000040 const ArrayRef<const MethodDebugInfo>& method_infos,
41 bool with_signature) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000042 uint64_t mapping_symbol_address = std::numeric_limits<uint64_t>::max();
David Srbeckyc5bfa972016-02-05 15:49:10 +000043 auto* strtab = builder->GetStrTab();
44 auto* symtab = builder->GetSymTab();
45
46 if (method_infos.empty()) {
47 return;
48 }
49
David Srbecky197160d2016-03-07 17:33:57 +000050 // Find all addresses which contain deduped methods.
David Srbeckyc5bfa972016-02-05 15:49:10 +000051 // The first instance of method is not marked deduped_, but the rest is.
David Srbecky197160d2016-03-07 17:33:57 +000052 std::unordered_set<uint64_t> deduped_addresses;
David Srbeckyc5bfa972016-02-05 15:49:10 +000053 for (const MethodDebugInfo& info : method_infos) {
54 if (info.deduped) {
David Srbecky197160d2016-03-07 17:33:57 +000055 deduped_addresses.insert(info.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +000056 }
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 Srbecky09c2a6b2016-03-11 17:11:44 +000067 size_t name_offset;
Vladimir Marko1b404a82017-09-01 13:35:26 +010068 if (!info.trampoline_name.empty()) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000069 name_offset = strtab->Write(info.trampoline_name);
70 } else {
71 DCHECK(info.dex_file != nullptr);
David Sehr709b0702016-10-13 09:12:37 -070072 std::string name = info.dex_file->PrettyMethod(info.dex_method_index, with_signature);
David Srbecky09c2a6b2016-03-11 17:11:44 +000073 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 Srbeckyc5bfa972016-02-05 15:49:10 +000080 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000081
David Srbeckyf4886df2017-12-11 16:06:29 +000082 const auto* text = builder->GetText();
83 uint64_t address = info.code_address;
84 address += info.is_code_address_text_relative ? text->GetAddress() : 0;
David Srbeckyc5bfa972016-02-05 15:49:10 +000085 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
David Srbecky197160d2016-03-07 17:33:57 +000086 address += CompiledMethod::CodeDelta(info.isa);
87 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
David Srbeckyc5bfa972016-02-05 15:49:10 +000088
89 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
90 // instructions, so that disassembler tools can correctly disassemble.
91 // Note that even if we generate just a single mapping symbol, ARM's Streamline
92 // requires it to match function symbol. Just address 0 does not work.
Vladimir Marko33bff252017-11-01 14:35:42 +000093 if (info.isa == InstructionSet::kThumb2) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000094 if (address < mapping_symbol_address || !kGenerateSingleArmMappingSymbol) {
David Srbecky197160d2016-03-07 17:33:57 +000095 symtab->Add(strtab->Write("$t"), text, address & ~1, 0, STB_LOCAL, STT_NOTYPE);
David Srbecky09c2a6b2016-03-11 17:11:44 +000096 mapping_symbol_address = address;
David Srbeckyc5bfa972016-02-05 15:49:10 +000097 }
98 }
David Srbeckyc5bfa972016-02-05 15:49:10 +000099 }
100 strtab->End();
101
102 // Symbols are buffered and written after names (because they are smaller).
103 // We could also do two passes in this function to avoid the buffering.
104 symtab->Start();
105 symtab->Write();
106 symtab->End();
107}
108
109} // namespace debug
110} // namespace art
111
112#endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
113