blob: 6463445e7443c1d429ecc5825cfe9d068dff2d3b [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
Yi Kongc57c6802018-10-29 14:28:56 -070019#include <memory>
David Srbecky50928112019-03-22 17:06:28 +000020#include <openssl/sha.h>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070021#include <unordered_map>
David Srbecky626a1662015-04-12 13:12:26 +010022#include <unordered_set>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
25
David Srbeckyf8980872015-05-22 17:04:47 +010026#include "base/casts.h"
David Sehr1979c642018-04-26 14:41:18 -070027#include "base/globals.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/leb128.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/utils.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000030#include "compiled_method.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000031#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000032#include "debug/method_debug_info.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000033#include "driver/compiler_options.h"
David Srbecky2faab002019-02-12 16:35:48 +000034#include "elf/elf_builder.h"
David Srbecky50928112019-03-22 17:06:28 +000035#include "elf/elf_utils.h"
David Srbecky2faab002019-02-12 16:35:48 +000036#include "stream/buffered_output_stream.h"
37#include "stream/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 Srbecky0c4572e2016-01-22 19:19:25 +000044class DebugInfoTask : public Task {
45 public:
46 DebugInfoTask(InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000047 const InstructionSetFeatures* features,
David Srbecky32210b92017-12-04 14:39:21 +000048 uint64_t text_section_address,
David Srbecky0c4572e2016-01-22 19:19:25 +000049 size_t text_section_size,
David Srbecky32210b92017-12-04 14:39:21 +000050 uint64_t dex_section_address,
51 size_t dex_section_size,
52 const debug::DebugInfo& debug_info)
David Srbecky0c4572e2016-01-22 19:19:25 +000053 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +000054 instruction_set_features_(features),
David Srbecky32210b92017-12-04 14:39:21 +000055 text_section_address_(text_section_address),
David Srbecky0c4572e2016-01-22 19:19:25 +000056 text_section_size_(text_section_size),
David Srbecky32210b92017-12-04 14:39:21 +000057 dex_section_address_(dex_section_address),
58 dex_section_size_(dex_section_size),
59 debug_info_(debug_info) {
David Srbecky0c4572e2016-01-22 19:19:25 +000060 }
61
Andreas Gampefa6a1b02018-09-07 08:11:55 -070062 void Run(Thread*) override {
David Srbeckyc5bfa972016-02-05 15:49:10 +000063 result_ = debug::MakeMiniDebugInfo(isa_,
David Srbecky5d811202016-03-08 13:21:22 +000064 instruction_set_features_,
David Srbecky32210b92017-12-04 14:39:21 +000065 text_section_address_,
David Srbecky0c4572e2016-01-22 19:19:25 +000066 text_section_size_,
David Srbecky32210b92017-12-04 14:39:21 +000067 dex_section_address_,
68 dex_section_size_,
69 debug_info_);
David Srbecky0c4572e2016-01-22 19:19:25 +000070 }
71
72 std::vector<uint8_t>* GetResult() {
73 return &result_;
74 }
75
76 private:
77 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +000078 const InstructionSetFeatures* instruction_set_features_;
David Srbecky32210b92017-12-04 14:39:21 +000079 uint64_t text_section_address_;
David Srbecky0c4572e2016-01-22 19:19:25 +000080 size_t text_section_size_;
David Srbecky32210b92017-12-04 14:39:21 +000081 uint64_t dex_section_address_;
82 size_t dex_section_size_;
83 const debug::DebugInfo& debug_info_;
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>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010088class ElfWriterQuick final : public ElfWriter {
Vladimir Marko10c13562015-11-25 14:33:36 +000089 public:
Vladimir Markoa0431112018-06-25 09:32:54 +010090 ElfWriterQuick(const CompilerOptions& compiler_options,
Vladimir Marko10c13562015-11-25 14:33:36 +000091 File* elf_file);
92 ~ElfWriterQuick();
93
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 void Start() override;
Vladimir Markoaad75c62016-10-03 08:46:48 +000095 void PrepareDynamicSection(size_t rodata_size,
96 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +000097 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +000098 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +010099 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000100 size_t bss_roots_offset,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100101 size_t dex_section_size) override;
102 void PrepareDebugInfo(const debug::DebugInfo& debug_info) override;
103 OutputStream* StartRoData() override;
104 void EndRoData(OutputStream* rodata) override;
105 OutputStream* StartText() override;
106 void EndText(OutputStream* text) override;
107 OutputStream* StartDataBimgRelRo() override;
108 void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) override;
109 void WriteDynamicSection() override;
110 void WriteDebugInfo(const debug::DebugInfo& debug_info) override;
111 bool StripDebugInfo() override;
112 bool End() override;
Vladimir Marko10c13562015-11-25 14:33:36 +0000113
Roland Levillainf73caca2018-08-24 17:19:07 +0100114 OutputStream* GetStream() override;
Vladimir Marko131980f2015-12-03 18:29:23 +0000115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 size_t GetLoadedSize() override;
Vladimir Marko944da602016-02-19 12:27:55 +0000117
Vladimir Marko10c13562015-11-25 14:33:36 +0000118 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
119 std::vector<uint8_t>* buffer);
120
121 private:
Vladimir Markoa0431112018-06-25 09:32:54 +0100122 const CompilerOptions& compiler_options_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000123 File* const elf_file_;
Vladimir Marko944da602016-02-19 12:27:55 +0000124 size_t rodata_size_;
125 size_t text_size_;
Vladimir Markob066d432018-01-03 13:14:37 +0000126 size_t data_bimg_rel_ro_size_;
Vladimir Marko944da602016-02-19 12:27:55 +0000127 size_t bss_size_;
David Srbecky32210b92017-12-04 14:39:21 +0000128 size_t dex_section_size_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000129 std::unique_ptr<BufferedOutputStream> output_stream_;
130 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000131 std::unique_ptr<DebugInfoTask> debug_info_task_;
132 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000133
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700134 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
135
Vladimir Marko10c13562015-11-25 14:33:36 +0000136 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
137};
138
Vladimir Markoa0431112018-06-25 09:32:54 +0100139std::unique_ptr<ElfWriter> CreateElfWriterQuick(const CompilerOptions& compiler_options,
Vladimir Marko10c13562015-11-25 14:33:36 +0000140 File* elf_file) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100141 if (Is64BitInstructionSet(compiler_options.GetInstructionSet())) {
142 return std::make_unique<ElfWriterQuick<ElfTypes64>>(compiler_options, elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000143 } else {
Vladimir Markoa0431112018-06-25 09:32:54 +0100144 return std::make_unique<ElfWriterQuick<ElfTypes32>>(compiler_options, elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000145 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700146}
147
David Srbecky533c2072015-04-22 12:20:22 +0100148template <typename ElfTypes>
Vladimir Markoa0431112018-06-25 09:32:54 +0100149ElfWriterQuick<ElfTypes>::ElfWriterQuick(const CompilerOptions& compiler_options, File* elf_file)
Vladimir Marko10c13562015-11-25 14:33:36 +0000150 : ElfWriter(),
151 compiler_options_(compiler_options),
152 elf_file_(elf_file),
Vladimir Marko944da602016-02-19 12:27:55 +0000153 rodata_size_(0u),
154 text_size_(0u),
Vladimir Markob066d432018-01-03 13:14:37 +0000155 data_bimg_rel_ro_size_(0u),
Vladimir Marko944da602016-02-19 12:27:55 +0000156 bss_size_(0u),
David Srbecky32210b92017-12-04 14:39:21 +0000157 dex_section_size_(0u),
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700158 output_stream_(
159 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
Vladimir Markoa0431112018-06-25 09:32:54 +0100160 builder_(new ElfBuilder<ElfTypes>(compiler_options_.GetInstructionSet(),
Vladimir Markoa0431112018-06-25 09:32:54 +0100161 output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700162
Vladimir Marko10c13562015-11-25 14:33:36 +0000163template <typename ElfTypes>
164ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700165
Vladimir Marko10c13562015-11-25 14:33:36 +0000166template <typename ElfTypes>
167void ElfWriterQuick<ElfTypes>::Start() {
168 builder_->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100169 if (compiler_options_.GetGenerateBuildId()) {
David Srbeckye155f4b2017-12-06 15:18:38 +0000170 builder_->GetBuildId()->AllocateVirtualMemory(builder_->GetBuildId()->GetSize());
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700171 builder_->WriteBuildIdSection();
172 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000173}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Marko10c13562015-11-25 14:33:36 +0000175template <typename ElfTypes>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000176void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
Vladimir Marko944da602016-02-19 12:27:55 +0000177 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000178 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100180 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000181 size_t bss_roots_offset,
182 size_t dex_section_size) {
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;
Vladimir Markob066d432018-01-03 13:14:37 +0000187 DCHECK_EQ(data_bimg_rel_ro_size_, 0u);
188 data_bimg_rel_ro_size_ = data_bimg_rel_ro_size;
Vladimir Marko944da602016-02-19 12:27:55 +0000189 DCHECK_EQ(bss_size_, 0u);
190 bss_size_ = bss_size;
David Srbecky32210b92017-12-04 14:39:21 +0000191 DCHECK_EQ(dex_section_size_, 0u);
192 dex_section_size_ = dex_section_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000193 builder_->PrepareDynamicSection(elf_file_->GetPath(),
194 rodata_size_,
195 text_size_,
Vladimir Markob066d432018-01-03 13:14:37 +0000196 data_bimg_rel_ro_size_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000197 bss_size_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100198 bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000199 bss_roots_offset,
200 dex_section_size);
Vladimir Marko944da602016-02-19 12:27:55 +0000201}
202
203template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000204OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
205 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000206 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000207 return rodata;
208}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000209
Vladimir Marko10c13562015-11-25 14:33:36 +0000210template <typename ElfTypes>
211void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
212 CHECK_EQ(builder_->GetRoData(), rodata);
213 builder_->GetRoData()->End();
214}
215
216template <typename ElfTypes>
217OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
218 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000219 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000220 return text;
221}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000222
Vladimir Marko10c13562015-11-25 14:33:36 +0000223template <typename ElfTypes>
224void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
225 CHECK_EQ(builder_->GetText(), text);
226 builder_->GetText()->End();
227}
228
229template <typename ElfTypes>
Vladimir Markob066d432018-01-03 13:14:37 +0000230OutputStream* ElfWriterQuick<ElfTypes>::StartDataBimgRelRo() {
231 auto* data_bimg_rel_ro = builder_->GetDataBimgRelRo();
232 data_bimg_rel_ro->Start();
233 return data_bimg_rel_ro;
234}
235
236template <typename ElfTypes>
237void ElfWriterQuick<ElfTypes>::EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) {
238 CHECK_EQ(builder_->GetDataBimgRelRo(), data_bimg_rel_ro);
239 builder_->GetDataBimgRelRo()->End();
240}
241
242template <typename ElfTypes>
Vladimir Marko45724f92016-02-17 17:46:10 +0000243void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
Vladimir Marko944da602016-02-19 12:27:55 +0000244 builder_->WriteDynamicSection();
Vladimir Marko10c13562015-11-25 14:33:36 +0000245}
246
247template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000248void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(const debug::DebugInfo& debug_info) {
David Srbecky49b2b202019-02-01 13:35:48 +0000249 if (compiler_options_.GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000250 // Prepare the mini-debug-info in background while we do other I/O.
251 Thread* self = Thread::Current();
Yi Kongc57c6802018-10-29 14:28:56 -0700252 debug_info_task_ = std::make_unique<DebugInfoTask>(
253 builder_->GetIsa(),
254 compiler_options_.GetInstructionSetFeatures(),
255 builder_->GetText()->GetAddress(),
256 text_size_,
257 builder_->GetDex()->Exists() ? builder_->GetDex()->GetAddress() : 0,
258 dex_section_size_,
259 debug_info);
260 debug_info_thread_pool_ = std::make_unique<ThreadPool>("Mini-debug-info writer", 1);
David Srbecky0c4572e2016-01-22 19:19:25 +0000261 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
262 debug_info_thread_pool_->StartWorkers(self);
263 }
264}
265
266template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000267void ElfWriterQuick<ElfTypes>::WriteDebugInfo(const debug::DebugInfo& debug_info) {
David Srbecky49b2b202019-02-01 13:35:48 +0000268 if (compiler_options_.GetGenerateMiniDebugInfo()) {
269 // Wait for the mini-debug-info generation to finish and write it to disk.
270 Thread* self = Thread::Current();
271 DCHECK(debug_info_thread_pool_ != nullptr);
272 debug_info_thread_pool_->Wait(self, true, false);
273 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
274 }
275 // The Strip method expects debug info to be last (mini-debug-info is not stripped).
276 if (!debug_info.Empty() && compiler_options_.GetGenerateDebugInfo()) {
277 // Generate all the debug information we can.
David Srbecky7370d922019-02-12 14:00:30 +0000278 debug::WriteDebugInfo(builder_.get(), debug_info);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000279 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000280}
281
282template <typename ElfTypes>
David Srbeckyde91fd42018-07-05 22:27:08 +0100283bool ElfWriterQuick<ElfTypes>::StripDebugInfo() {
284 off_t file_size = builder_->Strip();
285 return elf_file_->SetLength(file_size) == 0;
286}
287
288template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000289bool ElfWriterQuick<ElfTypes>::End() {
290 builder_->End();
Vladimir Markoa0431112018-06-25 09:32:54 +0100291 if (compiler_options_.GetGenerateBuildId()) {
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700292 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
293 ComputeFileBuildId(&build_id);
294 builder_->WriteBuildId(build_id);
295 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000296 return builder_->Good();
297}
298
299template <typename ElfTypes>
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700300void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
301 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
302 constexpr int kBufSize = 8192;
303 std::vector<char> buffer(kBufSize);
304 int64_t offset = 0;
305 SHA_CTX ctx;
306 SHA1_Init(&ctx);
307 while (true) {
308 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
309 CHECK_GE(bytes_read, 0);
310 if (bytes_read == 0) {
311 // End of file.
312 break;
313 }
314 SHA1_Update(&ctx, buffer.data(), bytes_read);
315 offset += bytes_read;
316 }
317 SHA1_Final(*build_id, &ctx);
318}
319
320template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000321OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
322 return builder_->GetStream();
323}
324
Vladimir Marko944da602016-02-19 12:27:55 +0000325template <typename ElfTypes>
326size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
327 return builder_->GetLoadedSize();
328}
329
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000330// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100331template class ElfWriterQuick<ElfTypes32>;
332template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000333
Vladimir Marko74527972016-11-29 15:57:32 +0000334} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335} // namespace art