blob: 759a6d633a8a30b88a3a8d4a89adee7094f6a5e3 [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"
David Srbecky0c4572e2016-01-22 19:19:25 +000036#include "thread-inl.h"
37#include "thread_pool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038#include "utils.h"
39
40namespace art {
41
David Srbeckyad5fa8c2015-05-06 18:27:35 +010042// .eh_frame and .debug_frame are almost identical.
43// Except for some minor formatting differences, the main difference
44// is that .eh_frame is allocated within the running program because
45// it is used by C++ exception handling (which we do not use so we
46// can choose either). C++ compilers generally tend to use .eh_frame
47// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010048// Let's use .debug_frame because it is easier to strip or compress.
49constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010050
David Srbecky0c4572e2016-01-22 19:19:25 +000051class DebugInfoTask : public Task {
52 public:
53 DebugInfoTask(InstructionSet isa,
54 size_t rodata_section_size,
55 size_t text_section_size,
56 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos)
57 : isa_(isa),
58 rodata_section_size_(rodata_section_size),
59 text_section_size_(text_section_size),
60 method_infos_(method_infos) {
61 }
62
63 void Run(Thread*) {
64 result_ = dwarf::MakeMiniDebugInfo(isa_,
65 rodata_section_size_,
66 text_section_size_,
67 method_infos_);
68 }
69
70 std::vector<uint8_t>* GetResult() {
71 return &result_;
72 }
73
74 private:
75 InstructionSet isa_;
76 size_t rodata_section_size_;
77 size_t text_section_size_;
78 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos_;
79 std::vector<uint8_t> result_;
80};
81
David Srbecky533c2072015-04-22 12:20:22 +010082template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000083class ElfWriterQuick FINAL : public ElfWriter {
84 public:
85 ElfWriterQuick(InstructionSet instruction_set,
86 const CompilerOptions* compiler_options,
87 File* elf_file);
88 ~ElfWriterQuick();
89
90 void Start() OVERRIDE;
David Srbecky0c4572e2016-01-22 19:19:25 +000091 void PrepareDebugInfo(size_t rodata_section_size,
92 size_t text_section_size,
93 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +000094 OutputStream* StartRoData() OVERRIDE;
95 void EndRoData(OutputStream* rodata) OVERRIDE;
96 OutputStream* StartText() OVERRIDE;
97 void EndText(OutputStream* text) OVERRIDE;
98 void SetBssSize(size_t bss_size) OVERRIDE;
99 void WriteDynamicSection() OVERRIDE;
100 void WriteDebugInfo(const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE;
101 void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE;
102 bool End() OVERRIDE;
103
Vladimir Marko131980f2015-12-03 18:29:23 +0000104 virtual OutputStream* GetStream() OVERRIDE;
105
Vladimir Marko10c13562015-11-25 14:33:36 +0000106 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
107 std::vector<uint8_t>* buffer);
108
109 private:
110 const CompilerOptions* const compiler_options_;
111 File* const elf_file_;
112 std::unique_ptr<BufferedOutputStream> output_stream_;
113 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000114 std::unique_ptr<DebugInfoTask> debug_info_task_;
115 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000116
117 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
118};
119
120std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
121 const CompilerOptions* compiler_options,
122 File* elf_file) {
123 if (Is64BitInstructionSet(instruction_set)) {
124 return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file);
125 } else {
126 return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file);
127 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700128}
129
David Srbecky533c2072015-04-22 12:20:22 +0100130template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000131ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
132 const CompilerOptions* compiler_options,
133 File* elf_file)
134 : ElfWriter(),
135 compiler_options_(compiler_options),
136 elf_file_(elf_file),
137 output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))),
138 builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700139
Vladimir Marko10c13562015-11-25 14:33:36 +0000140template <typename ElfTypes>
141ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700142
Vladimir Marko10c13562015-11-25 14:33:36 +0000143template <typename ElfTypes>
144void ElfWriterQuick<ElfTypes>::Start() {
145 builder_->Start();
146}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147
Vladimir Marko10c13562015-11-25 14:33:36 +0000148template <typename ElfTypes>
149OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
150 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000152 return rodata;
153}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154
Vladimir Marko10c13562015-11-25 14:33:36 +0000155template <typename ElfTypes>
156void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
157 CHECK_EQ(builder_->GetRoData(), rodata);
158 builder_->GetRoData()->End();
159}
160
161template <typename ElfTypes>
162OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
163 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000164 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000165 return text;
166}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167
Vladimir Marko10c13562015-11-25 14:33:36 +0000168template <typename ElfTypes>
169void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
170 CHECK_EQ(builder_->GetText(), text);
171 builder_->GetText()->End();
172}
173
174template <typename ElfTypes>
175void ElfWriterQuick<ElfTypes>::SetBssSize(size_t bss_size) {
176 auto* bss = builder_->GetBss();
177 if (bss_size != 0u) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000178 bss->WriteNoBitsSection(bss_size);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000179 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700180}
Mark Mendellae9fd932014-02-10 16:14:35 -0800181
David Srbecky533c2072015-04-22 12:20:22 +0100182template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000183void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
184 builder_->WriteDynamicSection(elf_file_->GetPath());
185}
186
187template <typename ElfTypes>
David Srbecky0c4572e2016-01-22 19:19:25 +0000188void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
189 size_t rodata_section_size,
190 size_t text_section_size,
191 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
192 if (compiler_options_->GetGenerateMiniDebugInfo()) {
193 // Prepare the mini-debug-info in background while we do other I/O.
194 Thread* self = Thread::Current();
195 debug_info_task_ = std::unique_ptr<DebugInfoTask>(
196 new DebugInfoTask(builder_->GetIsa(),
197 rodata_section_size,
198 text_section_size,
199 method_infos));
200 debug_info_thread_pool_ = std::unique_ptr<ThreadPool>(
201 new ThreadPool("Mini-debug-info writer", 1));
202 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
203 debug_info_thread_pool_->StartWorkers(self);
204 }
205}
206
207template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000208void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
209 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
210 if (compiler_options_->GetGenerateDebugInfo()) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000211 // Generate all the debug information we can.
David Srbecky579942f2016-01-28 20:01:28 +0000212 dwarf::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */);
Vladimir Marko10c13562015-11-25 14:33:36 +0000213 }
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000214 if (compiler_options_->GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000215 // Wait for the mini-debug-info generation to finish and write it to disk.
216 Thread* self = Thread::Current();
217 DCHECK(debug_info_thread_pool_ != nullptr);
218 debug_info_thread_pool_->Wait(self, true, false);
219 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000220 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000221}
222
223template <typename ElfTypes>
224void ElfWriterQuick<ElfTypes>::WritePatchLocations(
225 const ArrayRef<const uintptr_t>& patch_locations) {
226 // Add relocation section for .text.
227 if (compiler_options_->GetIncludePatchInformation()) {
228 // Note that ElfWriter::Fixup will be called regardless and therefore
229 // we need to include oat_patches for debug sections unconditionally.
230 builder_->WritePatches(".text.oat_patches", patch_locations);
231 }
232}
233
234template <typename ElfTypes>
235bool ElfWriterQuick<ElfTypes>::End() {
236 builder_->End();
237
238 return builder_->Good();
239}
240
241template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000242OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
243 return builder_->GetStream();
244}
245
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000246// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100247template class ElfWriterQuick<ElfTypes32>;
248template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000249
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250} // namespace art