blob: e411496980a3196f35b18f89410dfca85f80e259 [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"
Vladimir Marko131980f2015-12-03 18:29:23 +000034#include "linker/buffered_output_stream.h"
35#include "linker/file_output_stream.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "utils.h"
37
38namespace art {
39
David Srbeckyad5fa8c2015-05-06 18:27:35 +010040// .eh_frame and .debug_frame are almost identical.
41// Except for some minor formatting differences, the main difference
42// is that .eh_frame is allocated within the running program because
43// it is used by C++ exception handling (which we do not use so we
44// can choose either). C++ compilers generally tend to use .eh_frame
45// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010046// Let's use .debug_frame because it is easier to strip or compress.
47constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010048
David Srbecky388d2862015-05-21 19:11:18 +010049// The ARM specification defines three special mapping symbols
50// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
51// These symbols can be used by tools, for example, to pretty
52// print instructions correctly. Objdump will use them if they
53// exist, but it will still work well without them.
54// However, these extra symbols take space, so let's just generate
55// one symbol which marks the whole .text section as code.
56constexpr bool kGenerateSingleArmMappingSymbol = true;
57
David Srbecky533c2072015-04-22 12:20:22 +010058template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000059class ElfWriterQuick FINAL : public ElfWriter {
60 public:
61 ElfWriterQuick(InstructionSet instruction_set,
62 const CompilerOptions* compiler_options,
63 File* elf_file);
64 ~ElfWriterQuick();
65
66 void Start() OVERRIDE;
67 OutputStream* StartRoData() OVERRIDE;
68 void EndRoData(OutputStream* rodata) OVERRIDE;
69 OutputStream* StartText() OVERRIDE;
70 void EndText(OutputStream* text) OVERRIDE;
71 void SetBssSize(size_t bss_size) OVERRIDE;
72 void WriteDynamicSection() OVERRIDE;
73 void WriteDebugInfo(const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE;
74 void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE;
75 bool End() OVERRIDE;
76
Vladimir Marko131980f2015-12-03 18:29:23 +000077 virtual OutputStream* GetStream() OVERRIDE;
78
Vladimir Marko10c13562015-11-25 14:33:36 +000079 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
80 std::vector<uint8_t>* buffer);
81
82 private:
83 const CompilerOptions* const compiler_options_;
84 File* const elf_file_;
85 std::unique_ptr<BufferedOutputStream> output_stream_;
86 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
87
88 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
89};
90
91std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
92 const CompilerOptions* compiler_options,
93 File* elf_file) {
94 if (Is64BitInstructionSet(instruction_set)) {
95 return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file);
96 } else {
97 return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file);
98 }
Brian Carlstromb12f3472014-06-11 14:54:46 -070099}
100
David Srbecky533c2072015-04-22 12:20:22 +0100101template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000102static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
103 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700104
David Srbecky533c2072015-04-22 12:20:22 +0100105template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000106ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
107 const CompilerOptions* compiler_options,
108 File* elf_file)
109 : ElfWriter(),
110 compiler_options_(compiler_options),
111 elf_file_(elf_file),
112 output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))),
113 builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700114
Vladimir Marko10c13562015-11-25 14:33:36 +0000115template <typename ElfTypes>
116ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700117
Vladimir Marko10c13562015-11-25 14:33:36 +0000118template <typename ElfTypes>
119void ElfWriterQuick<ElfTypes>::Start() {
120 builder_->Start();
121}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122
Vladimir Marko10c13562015-11-25 14:33:36 +0000123template <typename ElfTypes>
124OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
125 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000127 return rodata;
128}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129
Vladimir Marko10c13562015-11-25 14:33:36 +0000130template <typename ElfTypes>
131void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
132 CHECK_EQ(builder_->GetRoData(), rodata);
133 builder_->GetRoData()->End();
134}
135
136template <typename ElfTypes>
137OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
138 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000140 return text;
141}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000142
Vladimir Marko10c13562015-11-25 14:33:36 +0000143template <typename ElfTypes>
144void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
145 CHECK_EQ(builder_->GetText(), text);
146 builder_->GetText()->End();
147}
148
149template <typename ElfTypes>
150void ElfWriterQuick<ElfTypes>::SetBssSize(size_t bss_size) {
151 auto* bss = builder_->GetBss();
152 if (bss_size != 0u) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153 bss->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000154 bss->SetSize(bss_size);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155 bss->End();
156 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700157}
Mark Mendellae9fd932014-02-10 16:14:35 -0800158
David Srbecky533c2072015-04-22 12:20:22 +0100159template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000160void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
161 builder_->WriteDynamicSection(elf_file_->GetPath());
162}
163
164template <typename ElfTypes>
165void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
166 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
167 if (compiler_options_->GetGenerateDebugInfo()) {
168 if (!method_infos.empty()) {
169 // Add methods to .symtab.
170 WriteDebugSymbols(builder_.get(), method_infos);
171 // Generate CFI (stack unwinding information).
172 dwarf::WriteCFISection(builder_.get(), method_infos, kCFIFormat);
173 // Write DWARF .debug_* sections.
174 dwarf::WriteDebugSections(builder_.get(), method_infos);
175 }
176 }
177}
178
179template <typename ElfTypes>
180void ElfWriterQuick<ElfTypes>::WritePatchLocations(
181 const ArrayRef<const uintptr_t>& patch_locations) {
182 // Add relocation section for .text.
183 if (compiler_options_->GetIncludePatchInformation()) {
184 // Note that ElfWriter::Fixup will be called regardless and therefore
185 // we need to include oat_patches for debug sections unconditionally.
186 builder_->WritePatches(".text.oat_patches", patch_locations);
187 }
188}
189
190template <typename ElfTypes>
191bool ElfWriterQuick<ElfTypes>::End() {
192 builder_->End();
193
194 return builder_->Good();
195}
196
197template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000198OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
199 return builder_->GetStream();
200}
201
202template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000203static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder,
204 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
David Srbecky388d2862015-05-21 19:11:18 +0100205 bool generated_mapping_symbol = false;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000206 auto* strtab = builder->GetStrTab();
207 auto* symtab = builder->GetSymTab();
208
Vladimir Marko10c13562015-11-25 14:33:36 +0000209 if (method_infos.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000210 return;
211 }
David Srbecky626a1662015-04-12 13:12:26 +0100212
213 // Find all addresses (low_pc) which contain deduped methods.
214 // The first instance of method is not marked deduped_, but the rest is.
215 std::unordered_set<uint32_t> deduped_addresses;
Vladimir Marko10c13562015-11-25 14:33:36 +0000216 for (const dwarf::MethodDebugInfo& info : method_infos) {
217 if (info.deduped_) {
218 deduped_addresses.insert(info.low_pc_);
David Srbecky626a1662015-04-12 13:12:26 +0100219 }
220 }
221
David Srbecky6d8c8f02015-10-26 10:57:09 +0000222 strtab->Start();
223 strtab->Write(""); // strtab should start with empty string.
Vladimir Marko10c13562015-11-25 14:33:36 +0000224 for (const dwarf::MethodDebugInfo& info : method_infos) {
225 if (info.deduped_) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100226 continue; // Add symbol only for the first instance.
227 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000228 std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, true);
229 if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) {
David Srbecky626a1662015-04-12 13:12:26 +0100230 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100231 }
232
Vladimir Marko10c13562015-11-25 14:33:36 +0000233 uint32_t low_pc = info.low_pc_;
David Srbecky6f715892015-03-30 14:21:42 +0100234 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
Vladimir Marko10c13562015-11-25 14:33:36 +0000235 low_pc += info.compiled_method_->CodeDelta();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000236 symtab->Add(strtab->Write(name), builder->GetText(), low_pc,
Vladimir Marko10c13562015-11-25 14:33:36 +0000237 true, info.high_pc_ - info.low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238
Ningsheng Jianf9734552014-10-27 14:56:34 +0800239 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
240 // instructions, so that disassembler tools can correctly disassemble.
David Srbecky388d2862015-05-21 19:11:18 +0100241 // Note that even if we generate just a single mapping symbol, ARM's Streamline
242 // requires it to match function symbol. Just address 0 does not work.
Vladimir Marko10c13562015-11-25 14:33:36 +0000243 if (info.compiled_method_->GetInstructionSet() == kThumb2) {
David Srbecky388d2862015-05-21 19:11:18 +0100244 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000245 symtab->Add(strtab->Write("$t"), builder->GetText(), info.low_pc_ & ~1,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000246 true, 0, STB_LOCAL, STT_NOTYPE);
David Srbecky388d2862015-05-21 19:11:18 +0100247 generated_mapping_symbol = true;
248 }
Ningsheng Jianf9734552014-10-27 14:56:34 +0800249 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000251 strtab->End();
252
253 // Symbols are buffered and written after names (because they are smaller).
254 // We could also do two passes in this function to avoid the buffering.
255 symtab->Start();
256 symtab->Write();
257 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700258}
259
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000260// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100261template class ElfWriterQuick<ElfTypes32>;
262template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000263
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264} // namespace art