blob: 9da2af8d3eda01b47860f6227e61e690441ef4a8 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 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#include "elf_writer_quick.h"
18
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070019#include <unordered_map>
David Srbecky626a1662015-04-12 13:12:26 +010020#include <unordered_set>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070021
David Srbeckyf8980872015-05-22 17:04:47 +010022#include "base/casts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "base/logging.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000024#include "base/stl_util.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000025#include "compiled_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000027#include "dwarf/method_debug_info.h"
28#include "elf.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070029#include "elf_builder.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000030#include "elf_utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010031#include "elf_writer_debug.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070033#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "utils.h"
35
36namespace art {
37
David Srbeckyad5fa8c2015-05-06 18:27:35 +010038// .eh_frame and .debug_frame are almost identical.
39// Except for some minor formatting differences, the main difference
40// is that .eh_frame is allocated within the running program because
41// it is used by C++ exception handling (which we do not use so we
42// can choose either). C++ compilers generally tend to use .eh_frame
43// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010044// Let's use .debug_frame because it is easier to strip or compress.
45constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010046
David Srbecky388d2862015-05-21 19:11:18 +010047// The ARM specification defines three special mapping symbols
48// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
49// These symbols can be used by tools, for example, to pretty
50// print instructions correctly. Objdump will use them if they
51// exist, but it will still work well without them.
52// However, these extra symbols take space, so let's just generate
53// one symbol which marks the whole .text section as code.
54constexpr bool kGenerateSingleArmMappingSymbol = true;
55
David Srbecky533c2072015-04-22 12:20:22 +010056template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000057class ElfWriterQuick FINAL : public ElfWriter {
58 public:
59 ElfWriterQuick(InstructionSet instruction_set,
60 const CompilerOptions* compiler_options,
61 File* elf_file);
62 ~ElfWriterQuick();
63
64 void Start() OVERRIDE;
65 OutputStream* StartRoData() OVERRIDE;
66 void EndRoData(OutputStream* rodata) OVERRIDE;
67 OutputStream* StartText() OVERRIDE;
68 void EndText(OutputStream* text) OVERRIDE;
69 void SetBssSize(size_t bss_size) OVERRIDE;
70 void WriteDynamicSection() OVERRIDE;
71 void WriteDebugInfo(const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE;
72 void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE;
73 bool End() OVERRIDE;
74
75 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
76 std::vector<uint8_t>* buffer);
77
78 private:
79 const CompilerOptions* const compiler_options_;
80 File* const elf_file_;
81 std::unique_ptr<BufferedOutputStream> output_stream_;
82 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
83
84 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
85};
86
87std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
88 const CompilerOptions* compiler_options,
89 File* elf_file) {
90 if (Is64BitInstructionSet(instruction_set)) {
91 return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file);
92 } else {
93 return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file);
94 }
Brian Carlstromb12f3472014-06-11 14:54:46 -070095}
96
David Srbecky533c2072015-04-22 12:20:22 +010097template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000098static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
99 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700100
David Srbecky533c2072015-04-22 12:20:22 +0100101template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000102ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
103 const CompilerOptions* compiler_options,
104 File* elf_file)
105 : ElfWriter(),
106 compiler_options_(compiler_options),
107 elf_file_(elf_file),
108 output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))),
109 builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700110
Vladimir Marko10c13562015-11-25 14:33:36 +0000111template <typename ElfTypes>
112ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700113
Vladimir Marko10c13562015-11-25 14:33:36 +0000114template <typename ElfTypes>
115void ElfWriterQuick<ElfTypes>::Start() {
116 builder_->Start();
117}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118
Vladimir Marko10c13562015-11-25 14:33:36 +0000119template <typename ElfTypes>
120OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
121 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000123 return rodata;
124}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125
Vladimir Marko10c13562015-11-25 14:33:36 +0000126template <typename ElfTypes>
127void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
128 CHECK_EQ(builder_->GetRoData(), rodata);
129 builder_->GetRoData()->End();
130}
131
132template <typename ElfTypes>
133OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
134 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000136 return text;
137}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
Vladimir Marko10c13562015-11-25 14:33:36 +0000139template <typename ElfTypes>
140void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
141 CHECK_EQ(builder_->GetText(), text);
142 builder_->GetText()->End();
143}
144
145template <typename ElfTypes>
146void ElfWriterQuick<ElfTypes>::SetBssSize(size_t bss_size) {
147 auto* bss = builder_->GetBss();
148 if (bss_size != 0u) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 bss->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000150 bss->SetSize(bss_size);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151 bss->End();
152 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700153}
Mark Mendellae9fd932014-02-10 16:14:35 -0800154
David Srbecky533c2072015-04-22 12:20:22 +0100155template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000156void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
157 builder_->WriteDynamicSection(elf_file_->GetPath());
158}
159
160template <typename ElfTypes>
161void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
162 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
163 if (compiler_options_->GetGenerateDebugInfo()) {
164 if (!method_infos.empty()) {
165 // Add methods to .symtab.
166 WriteDebugSymbols(builder_.get(), method_infos);
167 // Generate CFI (stack unwinding information).
168 dwarf::WriteCFISection(builder_.get(), method_infos, kCFIFormat);
169 // Write DWARF .debug_* sections.
170 dwarf::WriteDebugSections(builder_.get(), method_infos);
171 }
172 }
173}
174
175template <typename ElfTypes>
176void ElfWriterQuick<ElfTypes>::WritePatchLocations(
177 const ArrayRef<const uintptr_t>& patch_locations) {
178 // Add relocation section for .text.
179 if (compiler_options_->GetIncludePatchInformation()) {
180 // Note that ElfWriter::Fixup will be called regardless and therefore
181 // we need to include oat_patches for debug sections unconditionally.
182 builder_->WritePatches(".text.oat_patches", patch_locations);
183 }
184}
185
186template <typename ElfTypes>
187bool ElfWriterQuick<ElfTypes>::End() {
188 builder_->End();
189
190 return builder_->Good();
191}
192
193template <typename ElfTypes>
194static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
195 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
David Srbecky388d2862015-05-21 19:11:18 +0100196 bool generated_mapping_symbol = false;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000197 auto* strtab = builder->GetStrTab();
198 auto* symtab = builder->GetSymTab();
199
Vladimir Marko10c13562015-11-25 14:33:36 +0000200 if (method_infos.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000201 return;
202 }
David Srbecky626a1662015-04-12 13:12:26 +0100203
204 // Find all addresses (low_pc) which contain deduped methods.
205 // The first instance of method is not marked deduped_, but the rest is.
206 std::unordered_set<uint32_t> deduped_addresses;
Vladimir Marko10c13562015-11-25 14:33:36 +0000207 for (const dwarf::MethodDebugInfo& info : method_infos) {
208 if (info.deduped_) {
209 deduped_addresses.insert(info.low_pc_);
David Srbecky626a1662015-04-12 13:12:26 +0100210 }
211 }
212
David Srbecky6d8c8f02015-10-26 10:57:09 +0000213 strtab->Start();
214 strtab->Write(""); // strtab should start with empty string.
Vladimir Marko10c13562015-11-25 14:33:36 +0000215 for (const dwarf::MethodDebugInfo& info : method_infos) {
216 if (info.deduped_) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100217 continue; // Add symbol only for the first instance.
218 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000219 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, true);
220 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
David Srbecky626a1662015-04-12 13:12:26 +0100221 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100222 }
223
Vladimir Marko10c13562015-11-25 14:33:36 +0000224 uint32_t low_pc = info.low_pc_;
David Srbecky6f715892015-03-30 14:21:42 +0100225 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
Vladimir Marko10c13562015-11-25 14:33:36 +0000226 low_pc += info.compiled_method_->CodeDelta();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000227 symtab->Add(strtab->Write(name), builder->GetText(), low_pc,
Vladimir Marko10c13562015-11-25 14:33:36 +0000228 true, info.high_pc_ - info.low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229
Ningsheng Jianf9734552014-10-27 14:56:34 +0800230 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
231 // instructions, so that disassembler tools can correctly disassemble.
David Srbecky388d2862015-05-21 19:11:18 +0100232 // Note that even if we generate just a single mapping symbol, ARM's Streamline
233 // requires it to match function symbol. Just address 0 does not work.
Vladimir Marko10c13562015-11-25 14:33:36 +0000234 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
David Srbecky388d2862015-05-21 19:11:18 +0100235 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000236 symtab->Add(strtab->Write("$t"), builder->GetText(), info.low_pc_ & ~1,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000237 true, 0, STB_LOCAL, STT_NOTYPE);
David Srbecky388d2862015-05-21 19:11:18 +0100238 generated_mapping_symbol = true;
239 }
Ningsheng Jianf9734552014-10-27 14:56:34 +0800240 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000242 strtab->End();
243
244 // Symbols are buffered and written after names (because they are smaller).
245 // We could also do two passes in this function to avoid the buffering.
246 symtab->Start();
247 symtab->Write();
248 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249}
250
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000251// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100252template class ElfWriterQuick<ElfTypes32>;
253template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000254
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255} // namespace art