blob: 0a199dac35f2764fca7b71242db6a986aa968b02 [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"
23#include "elf_builder.h"
24#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>
39static 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
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 }
67 std::string name = PrettyMethod(info.dex_method_index, *info.dex_file, with_signature);
David Srbecky197160d2016-03-07 17:33:57 +000068 if (deduped_addresses.find(info.code_address) != deduped_addresses.end()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000069 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
David Srbecky197160d2016-03-07 17:33:57 +000074 const auto* text = info.is_code_address_text_relative ? builder->GetText() : nullptr;
75 uint64_t address = info.code_address + (text != nullptr ? text->GetAddress() : 0);
David Srbeckyc5bfa972016-02-05 15:49:10 +000076 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
David Srbecky197160d2016-03-07 17:33:57 +000077 address += CompiledMethod::CodeDelta(info.isa);
78 symtab->Add(name_offset, text, address, info.code_size, STB_GLOBAL, STT_FUNC);
David Srbeckyc5bfa972016-02-05 15:49:10 +000079
80 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
81 // instructions, so that disassembler tools can correctly disassemble.
82 // Note that even if we generate just a single mapping symbol, ARM's Streamline
83 // requires it to match function symbol. Just address 0 does not work.
David Srbecky197160d2016-03-07 17:33:57 +000084 if (info.isa == kThumb2) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000085 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky197160d2016-03-07 17:33:57 +000086 symtab->Add(strtab->Write("$t"), text, address & ~1, 0, STB_LOCAL, STT_NOTYPE);
David Srbeckyc5bfa972016-02-05 15:49:10 +000087 generated_mapping_symbol = true;
88 }
89 }
90
91 last_name = std::move(name);
92 last_name_offset = name_offset;
93 }
94 strtab->End();
95
96 // Symbols are buffered and written after names (because they are smaller).
97 // We could also do two passes in this function to avoid the buffering.
98 symtab->Start();
99 symtab->Write();
100 symtab->End();
101}
102
103} // namespace debug
104} // namespace art
105
106#endif // ART_COMPILER_DEBUG_ELF_SYMTAB_WRITER_H_
107