blob: 58bd1b0f1f5d728a6243f1c46a6c7177e0f71dea [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
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
David Srbeckyf8980872015-05-22 17:04:47 +010025#include "base/casts.h"
David Sehr1979c642018-04-26 14:41:18 -070026#include "base/globals.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/utils.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "compiled_method.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000030#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000031#include "debug/method_debug_info.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000032#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000033#include "elf.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000034#include "elf_utils.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000035#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000036#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000037#include "linker/file_output_stream.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070038#include "thread-current-inl.h"
David Srbecky0c4572e2016-01-22 19:19:25 +000039#include "thread_pool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070040
41namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000042namespace linker {
Brian Carlstrom7940e442013-07-12 13:46:57 -070043
David Srbeckyad5fa8c2015-05-06 18:27:35 +010044// .eh_frame and .debug_frame are almost identical.
45// Except for some minor formatting differences, the main difference
46// is that .eh_frame is allocated within the running program because
47// it is used by C++ exception handling (which we do not use so we
48// can choose either). C++ compilers generally tend to use .eh_frame
49// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010050// Let's use .debug_frame because it is easier to strip or compress.
51constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010052
David Srbecky0c4572e2016-01-22 19:19:25 +000053class DebugInfoTask : public Task {
54 public:
55 DebugInfoTask(InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000056 const InstructionSetFeatures* features,
David Srbecky32210b92017-12-04 14:39:21 +000057 uint64_t text_section_address,
David Srbecky0c4572e2016-01-22 19:19:25 +000058 size_t text_section_size,
David Srbecky32210b92017-12-04 14:39:21 +000059 uint64_t dex_section_address,
60 size_t dex_section_size,
61 const debug::DebugInfo& debug_info)
David Srbecky0c4572e2016-01-22 19:19:25 +000062 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +000063 instruction_set_features_(features),
David Srbecky32210b92017-12-04 14:39:21 +000064 text_section_address_(text_section_address),
David Srbecky0c4572e2016-01-22 19:19:25 +000065 text_section_size_(text_section_size),
David Srbecky32210b92017-12-04 14:39:21 +000066 dex_section_address_(dex_section_address),
67 dex_section_size_(dex_section_size),
68 debug_info_(debug_info) {
David Srbecky0c4572e2016-01-22 19:19:25 +000069 }
70
71 void Run(Thread*) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000072 result_ = debug::MakeMiniDebugInfo(isa_,
David Srbecky5d811202016-03-08 13:21:22 +000073 instruction_set_features_,
David Srbecky32210b92017-12-04 14:39:21 +000074 text_section_address_,
David Srbecky0c4572e2016-01-22 19:19:25 +000075 text_section_size_,
David Srbecky32210b92017-12-04 14:39:21 +000076 dex_section_address_,
77 dex_section_size_,
78 debug_info_);
David Srbecky0c4572e2016-01-22 19:19:25 +000079 }
80
81 std::vector<uint8_t>* GetResult() {
82 return &result_;
83 }
84
85 private:
86 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +000087 const InstructionSetFeatures* instruction_set_features_;
David Srbecky32210b92017-12-04 14:39:21 +000088 uint64_t text_section_address_;
David Srbecky0c4572e2016-01-22 19:19:25 +000089 size_t text_section_size_;
David Srbecky32210b92017-12-04 14:39:21 +000090 uint64_t dex_section_address_;
91 size_t dex_section_size_;
92 const debug::DebugInfo& debug_info_;
David Srbecky0c4572e2016-01-22 19:19:25 +000093 std::vector<uint8_t> result_;
94};
95
David Srbecky533c2072015-04-22 12:20:22 +010096template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000097class ElfWriterQuick FINAL : public ElfWriter {
98 public:
99 ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000100 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000101 const CompilerOptions* compiler_options,
102 File* elf_file);
103 ~ElfWriterQuick();
104
105 void Start() OVERRIDE;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000106 void PrepareDynamicSection(size_t rodata_size,
107 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000108 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000109 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100110 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000111 size_t bss_roots_offset,
112 size_t dex_section_size) OVERRIDE;
David Srbecky32210b92017-12-04 14:39:21 +0000113 void PrepareDebugInfo(const debug::DebugInfo& debug_info) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000114 OutputStream* StartRoData() OVERRIDE;
115 void EndRoData(OutputStream* rodata) OVERRIDE;
116 OutputStream* StartText() OVERRIDE;
117 void EndText(OutputStream* text) OVERRIDE;
Vladimir Markob066d432018-01-03 13:14:37 +0000118 OutputStream* StartDataBimgRelRo() OVERRIDE;
119 void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000120 void WriteDynamicSection() OVERRIDE;
David Srbecky32210b92017-12-04 14:39:21 +0000121 void WriteDebugInfo(const debug::DebugInfo& debug_info) OVERRIDE;
Vladimir Marko10c13562015-11-25 14:33:36 +0000122 bool End() OVERRIDE;
123
Vladimir Marko131980f2015-12-03 18:29:23 +0000124 virtual OutputStream* GetStream() OVERRIDE;
125
Vladimir Marko944da602016-02-19 12:27:55 +0000126 size_t GetLoadedSize() OVERRIDE;
127
Vladimir Marko10c13562015-11-25 14:33:36 +0000128 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
129 std::vector<uint8_t>* buffer);
130
131 private:
David Srbecky5d811202016-03-08 13:21:22 +0000132 const InstructionSetFeatures* instruction_set_features_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000133 const CompilerOptions* const compiler_options_;
134 File* const elf_file_;
Vladimir Marko944da602016-02-19 12:27:55 +0000135 size_t rodata_size_;
136 size_t text_size_;
Vladimir Markob066d432018-01-03 13:14:37 +0000137 size_t data_bimg_rel_ro_size_;
Vladimir Marko944da602016-02-19 12:27:55 +0000138 size_t bss_size_;
David Srbecky32210b92017-12-04 14:39:21 +0000139 size_t dex_section_size_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000140 std::unique_ptr<BufferedOutputStream> output_stream_;
141 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000142 std::unique_ptr<DebugInfoTask> debug_info_task_;
143 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000144
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700145 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
146
Vladimir Marko10c13562015-11-25 14:33:36 +0000147 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
148};
149
150std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000151 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000152 const CompilerOptions* compiler_options,
153 File* elf_file) {
154 if (Is64BitInstructionSet(instruction_set)) {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700155 return std::make_unique<ElfWriterQuick<ElfTypes64>>(instruction_set,
156 features,
157 compiler_options,
158 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000159 } else {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700160 return std::make_unique<ElfWriterQuick<ElfTypes32>>(instruction_set,
161 features,
162 compiler_options,
163 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000164 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700165}
166
David Srbecky533c2072015-04-22 12:20:22 +0100167template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000168ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000169 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000170 const CompilerOptions* compiler_options,
171 File* elf_file)
172 : ElfWriter(),
David Srbecky5d811202016-03-08 13:21:22 +0000173 instruction_set_features_(features),
Vladimir Marko10c13562015-11-25 14:33:36 +0000174 compiler_options_(compiler_options),
175 elf_file_(elf_file),
Vladimir Marko944da602016-02-19 12:27:55 +0000176 rodata_size_(0u),
177 text_size_(0u),
Vladimir Markob066d432018-01-03 13:14:37 +0000178 data_bimg_rel_ro_size_(0u),
Vladimir Marko944da602016-02-19 12:27:55 +0000179 bss_size_(0u),
David Srbecky32210b92017-12-04 14:39:21 +0000180 dex_section_size_(0u),
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700181 output_stream_(
182 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
David Srbecky5d811202016-03-08 13:21:22 +0000183 builder_(new ElfBuilder<ElfTypes>(instruction_set, features, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700184
Vladimir Marko10c13562015-11-25 14:33:36 +0000185template <typename ElfTypes>
186ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700187
Vladimir Marko10c13562015-11-25 14:33:36 +0000188template <typename ElfTypes>
189void ElfWriterQuick<ElfTypes>::Start() {
190 builder_->Start();
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700191 if (compiler_options_->GetGenerateBuildId()) {
David Srbeckye155f4b2017-12-06 15:18:38 +0000192 builder_->GetBuildId()->AllocateVirtualMemory(builder_->GetBuildId()->GetSize());
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700193 builder_->WriteBuildIdSection();
194 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000195}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000196
Vladimir Marko10c13562015-11-25 14:33:36 +0000197template <typename ElfTypes>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000198void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
Vladimir Marko944da602016-02-19 12:27:55 +0000199 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000200 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000201 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100202 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000203 size_t bss_roots_offset,
204 size_t dex_section_size) {
Vladimir Marko944da602016-02-19 12:27:55 +0000205 DCHECK_EQ(rodata_size_, 0u);
206 rodata_size_ = rodata_size;
207 DCHECK_EQ(text_size_, 0u);
208 text_size_ = text_size;
Vladimir Markob066d432018-01-03 13:14:37 +0000209 DCHECK_EQ(data_bimg_rel_ro_size_, 0u);
210 data_bimg_rel_ro_size_ = data_bimg_rel_ro_size;
Vladimir Marko944da602016-02-19 12:27:55 +0000211 DCHECK_EQ(bss_size_, 0u);
212 bss_size_ = bss_size;
David Srbecky32210b92017-12-04 14:39:21 +0000213 DCHECK_EQ(dex_section_size_, 0u);
214 dex_section_size_ = dex_section_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000215 builder_->PrepareDynamicSection(elf_file_->GetPath(),
216 rodata_size_,
217 text_size_,
Vladimir Markob066d432018-01-03 13:14:37 +0000218 data_bimg_rel_ro_size_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000219 bss_size_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100220 bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000221 bss_roots_offset,
222 dex_section_size);
Vladimir Marko944da602016-02-19 12:27:55 +0000223}
224
225template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000226OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
227 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000228 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000229 return rodata;
230}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000231
Vladimir Marko10c13562015-11-25 14:33:36 +0000232template <typename ElfTypes>
233void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
234 CHECK_EQ(builder_->GetRoData(), rodata);
235 builder_->GetRoData()->End();
236}
237
238template <typename ElfTypes>
239OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
240 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000241 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000242 return text;
243}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000244
Vladimir Marko10c13562015-11-25 14:33:36 +0000245template <typename ElfTypes>
246void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
247 CHECK_EQ(builder_->GetText(), text);
248 builder_->GetText()->End();
249}
250
251template <typename ElfTypes>
Vladimir Markob066d432018-01-03 13:14:37 +0000252OutputStream* ElfWriterQuick<ElfTypes>::StartDataBimgRelRo() {
253 auto* data_bimg_rel_ro = builder_->GetDataBimgRelRo();
254 data_bimg_rel_ro->Start();
255 return data_bimg_rel_ro;
256}
257
258template <typename ElfTypes>
259void ElfWriterQuick<ElfTypes>::EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) {
260 CHECK_EQ(builder_->GetDataBimgRelRo(), data_bimg_rel_ro);
261 builder_->GetDataBimgRelRo()->End();
262}
263
264template <typename ElfTypes>
Vladimir Marko45724f92016-02-17 17:46:10 +0000265void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
Vladimir Marko33bff252017-11-01 14:35:42 +0000266 if (builder_->GetIsa() == InstructionSet::kMips ||
267 builder_->GetIsa() == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700268 builder_->WriteMIPSabiflagsSection();
269 }
Vladimir Marko944da602016-02-19 12:27:55 +0000270 builder_->WriteDynamicSection();
Vladimir Marko10c13562015-11-25 14:33:36 +0000271}
272
273template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000274void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(const debug::DebugInfo& debug_info) {
275 if (!debug_info.Empty() && compiler_options_->GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000276 // Prepare the mini-debug-info in background while we do other I/O.
277 Thread* self = Thread::Current();
278 debug_info_task_ = std::unique_ptr<DebugInfoTask>(
David Srbecky5d811202016-03-08 13:21:22 +0000279 new DebugInfoTask(builder_->GetIsa(),
280 instruction_set_features_,
David Srbecky32210b92017-12-04 14:39:21 +0000281 builder_->GetText()->GetAddress(),
David Srbecky5d811202016-03-08 13:21:22 +0000282 text_size_,
David Srbecky32210b92017-12-04 14:39:21 +0000283 builder_->GetDex()->Exists() ? builder_->GetDex()->GetAddress() : 0,
284 dex_section_size_,
285 debug_info));
David Srbecky0c4572e2016-01-22 19:19:25 +0000286 debug_info_thread_pool_ = std::unique_ptr<ThreadPool>(
287 new ThreadPool("Mini-debug-info writer", 1));
288 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
289 debug_info_thread_pool_->StartWorkers(self);
290 }
291}
292
293template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000294void ElfWriterQuick<ElfTypes>::WriteDebugInfo(const debug::DebugInfo& debug_info) {
295 if (!debug_info.Empty()) {
David Srbecky370339c2016-02-05 11:42:23 +0000296 if (compiler_options_->GetGenerateDebugInfo()) {
297 // Generate all the debug information we can.
David Srbecky32210b92017-12-04 14:39:21 +0000298 debug::WriteDebugInfo(builder_.get(), debug_info, kCFIFormat, true /* write_oat_patches */);
David Srbecky370339c2016-02-05 11:42:23 +0000299 }
300 if (compiler_options_->GetGenerateMiniDebugInfo()) {
301 // Wait for the mini-debug-info generation to finish and write it to disk.
302 Thread* self = Thread::Current();
303 DCHECK(debug_info_thread_pool_ != nullptr);
304 debug_info_thread_pool_->Wait(self, true, false);
305 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
306 }
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000307 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000308}
309
310template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000311bool ElfWriterQuick<ElfTypes>::End() {
312 builder_->End();
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700313 if (compiler_options_->GetGenerateBuildId()) {
314 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
315 ComputeFileBuildId(&build_id);
316 builder_->WriteBuildId(build_id);
317 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000318 return builder_->Good();
319}
320
321template <typename ElfTypes>
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700322void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
323 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
324 constexpr int kBufSize = 8192;
325 std::vector<char> buffer(kBufSize);
326 int64_t offset = 0;
327 SHA_CTX ctx;
328 SHA1_Init(&ctx);
329 while (true) {
330 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
331 CHECK_GE(bytes_read, 0);
332 if (bytes_read == 0) {
333 // End of file.
334 break;
335 }
336 SHA1_Update(&ctx, buffer.data(), bytes_read);
337 offset += bytes_read;
338 }
339 SHA1_Final(*build_id, &ctx);
340}
341
342template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000343OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
344 return builder_->GetStream();
345}
346
Vladimir Marko944da602016-02-19 12:27:55 +0000347template <typename ElfTypes>
348size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
349 return builder_->GetLoadedSize();
350}
351
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000352// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100353template class ElfWriterQuick<ElfTypes32>;
354template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000355
Vladimir Marko74527972016-11-29 15:57:32 +0000356} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357} // namespace art