blob: 0d6575cffd2de110a2c7dc498de0e74f15c2255b [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
Alexey Alexandrovab40c112016-09-19 09:33:49 -070019#include <openssl/sha.h>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070020#include <unordered_map>
David Srbecky626a1662015-04-12 13:12:26 +010021#include <unordered_set>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070022
David Srbeckyf8980872015-05-22 17:04:47 +010023#include "base/casts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "base/logging.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000025#include "base/stl_util.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "compiled_method.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000027#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000028#include "debug/method_debug_info.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000030#include "elf.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070031#include "elf_builder.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000032#include "elf_utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070034#include "leb128.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000035#include "linker/buffered_output_stream.h"
36#include "linker/file_output_stream.h"
David Srbecky0c4572e2016-01-22 19:19:25 +000037#include "thread-inl.h"
38#include "thread_pool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039#include "utils.h"
40
41namespace art {
42
David Srbeckyad5fa8c2015-05-06 18:27:35 +010043// .eh_frame and .debug_frame are almost identical.
44// Except for some minor formatting differences, the main difference
45// is that .eh_frame is allocated within the running program because
46// it is used by C++ exception handling (which we do not use so we
47// can choose either). C++ compilers generally tend to use .eh_frame
48// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010049// Let's use .debug_frame because it is easier to strip or compress.
50constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010051
David Srbecky0c4572e2016-01-22 19:19:25 +000052class DebugInfoTask : public Task {
53 public:
54 DebugInfoTask(InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000055 const InstructionSetFeatures* features,
David Srbecky0c4572e2016-01-22 19:19:25 +000056 size_t rodata_section_size,
57 size_t text_section_size,
David Srbeckyc5bfa972016-02-05 15:49:10 +000058 const ArrayRef<const debug::MethodDebugInfo>& method_infos)
David Srbecky0c4572e2016-01-22 19:19:25 +000059 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +000060 instruction_set_features_(features),
David Srbecky0c4572e2016-01-22 19:19:25 +000061 rodata_section_size_(rodata_section_size),
62 text_section_size_(text_section_size),
63 method_infos_(method_infos) {
64 }
65
66 void Run(Thread*) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000067 result_ = debug::MakeMiniDebugInfo(isa_,
David Srbecky5d811202016-03-08 13:21:22 +000068 instruction_set_features_,
David Srbecky0c4572e2016-01-22 19:19:25 +000069 rodata_section_size_,
70 text_section_size_,
71 method_infos_);
72 }
73
74 std::vector<uint8_t>* GetResult() {
75 return &result_;
76 }
77
78 private:
79 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +000080 const InstructionSetFeatures* instruction_set_features_;
David Srbecky0c4572e2016-01-22 19:19:25 +000081 size_t rodata_section_size_;
82 size_t text_section_size_;
David Srbeckyc5bfa972016-02-05 15:49:10 +000083 const ArrayRef<const debug::MethodDebugInfo>& method_infos_;
David Srbecky0c4572e2016-01-22 19:19:25 +000084 std::vector<uint8_t> result_;
85};
86
David Srbecky533c2072015-04-22 12:20:22 +010087template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000088class ElfWriterQuick FINAL : public ElfWriter {
89 public:
90 ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +000091 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +000092 const CompilerOptions* compiler_options,
93 File* elf_file);
94 ~ElfWriterQuick();
95
96 void Start() OVERRIDE;
Vladimir Markoaad75c62016-10-03 08:46:48 +000097 void PrepareDynamicSection(size_t rodata_size,
98 size_t text_size,
99 size_t bss_size,
100 size_t bss_roots_offset) OVERRIDE;
Vladimir Marko944da602016-02-19 12:27:55 +0000101 void PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000102 OutputStream* StartRoData() OVERRIDE;
103 void EndRoData(OutputStream* rodata) OVERRIDE;
104 OutputStream* StartText() OVERRIDE;
105 void EndText(OutputStream* text) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000106 void WriteDynamicSection() OVERRIDE;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000107 void WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000108 void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE;
109 bool End() OVERRIDE;
110
Vladimir Marko131980f2015-12-03 18:29:23 +0000111 virtual OutputStream* GetStream() OVERRIDE;
112
Vladimir Marko944da602016-02-19 12:27:55 +0000113 size_t GetLoadedSize() OVERRIDE;
114
Vladimir Marko10c13562015-11-25 14:33:36 +0000115 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
116 std::vector<uint8_t>* buffer);
117
118 private:
David Srbecky5d811202016-03-08 13:21:22 +0000119 const InstructionSetFeatures* instruction_set_features_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000120 const CompilerOptions* const compiler_options_;
121 File* const elf_file_;
Vladimir Marko944da602016-02-19 12:27:55 +0000122 size_t rodata_size_;
123 size_t text_size_;
124 size_t bss_size_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000125 std::unique_ptr<BufferedOutputStream> output_stream_;
126 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000127 std::unique_ptr<DebugInfoTask> debug_info_task_;
128 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000129
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700130 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
131
Vladimir Marko10c13562015-11-25 14:33:36 +0000132 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
133};
134
135std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000136 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000137 const CompilerOptions* compiler_options,
138 File* elf_file) {
139 if (Is64BitInstructionSet(instruction_set)) {
David Srbecky5d811202016-03-08 13:21:22 +0000140 return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set,
141 features,
142 compiler_options,
143 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000144 } else {
David Srbecky5d811202016-03-08 13:21:22 +0000145 return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set,
146 features,
147 compiler_options,
148 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000149 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700150}
151
David Srbecky533c2072015-04-22 12:20:22 +0100152template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000153ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000154 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000155 const CompilerOptions* compiler_options,
156 File* elf_file)
157 : ElfWriter(),
David Srbecky5d811202016-03-08 13:21:22 +0000158 instruction_set_features_(features),
Vladimir Marko10c13562015-11-25 14:33:36 +0000159 compiler_options_(compiler_options),
160 elf_file_(elf_file),
Vladimir Marko944da602016-02-19 12:27:55 +0000161 rodata_size_(0u),
162 text_size_(0u),
163 bss_size_(0u),
Vladimir Marko10c13562015-11-25 14:33:36 +0000164 output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))),
David Srbecky5d811202016-03-08 13:21:22 +0000165 builder_(new ElfBuilder<ElfTypes>(instruction_set, features, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700166
Vladimir Marko10c13562015-11-25 14:33:36 +0000167template <typename ElfTypes>
168ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700169
Vladimir Marko10c13562015-11-25 14:33:36 +0000170template <typename ElfTypes>
171void ElfWriterQuick<ElfTypes>::Start() {
172 builder_->Start();
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700173 if (compiler_options_->GetGenerateBuildId()) {
174 builder_->WriteBuildIdSection();
175 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000176}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000177
Vladimir Marko10c13562015-11-25 14:33:36 +0000178template <typename ElfTypes>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
Vladimir Marko944da602016-02-19 12:27:55 +0000180 size_t text_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000181 size_t bss_size,
182 size_t bss_roots_offset) {
Vladimir Marko944da602016-02-19 12:27:55 +0000183 DCHECK_EQ(rodata_size_, 0u);
184 rodata_size_ = rodata_size;
185 DCHECK_EQ(text_size_, 0u);
186 text_size_ = text_size;
187 DCHECK_EQ(bss_size_, 0u);
188 bss_size_ = bss_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000189 builder_->PrepareDynamicSection(elf_file_->GetPath(),
190 rodata_size_,
191 text_size_,
192 bss_size_,
193 bss_roots_offset);
Vladimir Marko944da602016-02-19 12:27:55 +0000194}
195
196template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000197OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
198 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000199 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000200 return rodata;
201}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000202
Vladimir Marko10c13562015-11-25 14:33:36 +0000203template <typename ElfTypes>
204void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
205 CHECK_EQ(builder_->GetRoData(), rodata);
206 builder_->GetRoData()->End();
207}
208
209template <typename ElfTypes>
210OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
211 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000212 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000213 return text;
214}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000215
Vladimir Marko10c13562015-11-25 14:33:36 +0000216template <typename ElfTypes>
217void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
218 CHECK_EQ(builder_->GetText(), text);
219 builder_->GetText()->End();
220}
221
222template <typename ElfTypes>
Vladimir Marko45724f92016-02-17 17:46:10 +0000223void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
Vladimir Marko944da602016-02-19 12:27:55 +0000224 if (bss_size_ != 0u) {
225 builder_->GetBss()->WriteNoBitsSection(bss_size_);
226 }
Douglas Leung316a2182015-09-17 15:26:25 -0700227 if (builder_->GetIsa() == kMips || builder_->GetIsa() == kMips64) {
228 builder_->WriteMIPSabiflagsSection();
229 }
Vladimir Marko944da602016-02-19 12:27:55 +0000230 builder_->WriteDynamicSection();
Vladimir Marko10c13562015-11-25 14:33:36 +0000231}
232
233template <typename ElfTypes>
David Srbecky0c4572e2016-01-22 19:19:25 +0000234void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
David Srbeckyc5bfa972016-02-05 15:49:10 +0000235 const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
David Srbecky370339c2016-02-05 11:42:23 +0000236 if (!method_infos.empty() && compiler_options_->GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000237 // Prepare the mini-debug-info in background while we do other I/O.
238 Thread* self = Thread::Current();
239 debug_info_task_ = std::unique_ptr<DebugInfoTask>(
David Srbecky5d811202016-03-08 13:21:22 +0000240 new DebugInfoTask(builder_->GetIsa(),
241 instruction_set_features_,
242 rodata_size_,
243 text_size_,
244 method_infos));
David Srbecky0c4572e2016-01-22 19:19:25 +0000245 debug_info_thread_pool_ = std::unique_ptr<ThreadPool>(
246 new ThreadPool("Mini-debug-info writer", 1));
247 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
248 debug_info_thread_pool_->StartWorkers(self);
249 }
250}
251
252template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000253void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
David Srbeckyc5bfa972016-02-05 15:49:10 +0000254 const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
David Srbecky370339c2016-02-05 11:42:23 +0000255 if (!method_infos.empty()) {
256 if (compiler_options_->GetGenerateDebugInfo()) {
257 // Generate all the debug information we can.
David Srbeckyc5bfa972016-02-05 15:49:10 +0000258 debug::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */);
David Srbecky370339c2016-02-05 11:42:23 +0000259 }
260 if (compiler_options_->GetGenerateMiniDebugInfo()) {
261 // Wait for the mini-debug-info generation to finish and write it to disk.
262 Thread* self = Thread::Current();
263 DCHECK(debug_info_thread_pool_ != nullptr);
264 debug_info_thread_pool_->Wait(self, true, false);
265 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
266 }
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000267 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000268}
269
270template <typename ElfTypes>
271void ElfWriterQuick<ElfTypes>::WritePatchLocations(
272 const ArrayRef<const uintptr_t>& patch_locations) {
273 // Add relocation section for .text.
274 if (compiler_options_->GetIncludePatchInformation()) {
275 // Note that ElfWriter::Fixup will be called regardless and therefore
276 // we need to include oat_patches for debug sections unconditionally.
277 builder_->WritePatches(".text.oat_patches", patch_locations);
278 }
279}
280
281template <typename ElfTypes>
282bool ElfWriterQuick<ElfTypes>::End() {
283 builder_->End();
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700284 if (compiler_options_->GetGenerateBuildId()) {
285 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
286 ComputeFileBuildId(&build_id);
287 builder_->WriteBuildId(build_id);
288 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000289 return builder_->Good();
290}
291
292template <typename ElfTypes>
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700293void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
294 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
295 constexpr int kBufSize = 8192;
296 std::vector<char> buffer(kBufSize);
297 int64_t offset = 0;
298 SHA_CTX ctx;
299 SHA1_Init(&ctx);
300 while (true) {
301 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
302 CHECK_GE(bytes_read, 0);
303 if (bytes_read == 0) {
304 // End of file.
305 break;
306 }
307 SHA1_Update(&ctx, buffer.data(), bytes_read);
308 offset += bytes_read;
309 }
310 SHA1_Final(*build_id, &ctx);
311}
312
313template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000314OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
315 return builder_->GetStream();
316}
317
Vladimir Marko944da602016-02-19 12:27:55 +0000318template <typename ElfTypes>
319size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
320 return builder_->GetLoadedSize();
321}
322
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000323// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100324template class ElfWriterQuick<ElfTypes32>;
325template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000326
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327} // namespace art