blob: 10dc23e2e705310806db44715f7d7f8bd4c83db3 [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
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
David Srbeckyf8980872015-05-22 17:04:47 +010024#include "base/casts.h"
David Sehr1979c642018-04-26 14:41:18 -070025#include "base/globals.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "base/leb128.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/utils.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000028#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000029#include "debug/method_debug_info.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000030#include "driver/compiler_options.h"
David Srbecky2faab002019-02-12 16:35:48 +000031#include "elf/elf_builder.h"
David Srbecky50928112019-03-22 17:06:28 +000032#include "elf/elf_utils.h"
David Srbecky2faab002019-02-12 16:35:48 +000033#include "stream/buffered_output_stream.h"
34#include "stream/file_output_stream.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
David Srbecky0c4572e2016-01-22 19:19:25 +000036#include "thread_pool.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037
38namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000039namespace linker {
Brian Carlstrom7940e442013-07-12 13:46:57 -070040
David Srbecky0c4572e2016-01-22 19:19:25 +000041class DebugInfoTask : public Task {
42 public:
43 DebugInfoTask(InstructionSet isa,
David Srbecky5d811202016-03-08 13:21:22 +000044 const InstructionSetFeatures* features,
David Srbecky32210b92017-12-04 14:39:21 +000045 uint64_t text_section_address,
David Srbecky0c4572e2016-01-22 19:19:25 +000046 size_t text_section_size,
David Srbecky32210b92017-12-04 14:39:21 +000047 uint64_t dex_section_address,
48 size_t dex_section_size,
49 const debug::DebugInfo& debug_info)
David Srbecky0c4572e2016-01-22 19:19:25 +000050 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +000051 instruction_set_features_(features),
David Srbecky32210b92017-12-04 14:39:21 +000052 text_section_address_(text_section_address),
David Srbecky0c4572e2016-01-22 19:19:25 +000053 text_section_size_(text_section_size),
David Srbecky32210b92017-12-04 14:39:21 +000054 dex_section_address_(dex_section_address),
55 dex_section_size_(dex_section_size),
56 debug_info_(debug_info) {
David Srbecky0c4572e2016-01-22 19:19:25 +000057 }
58
Andreas Gampefa6a1b02018-09-07 08:11:55 -070059 void Run(Thread*) override {
David Srbeckyc5bfa972016-02-05 15:49:10 +000060 result_ = debug::MakeMiniDebugInfo(isa_,
David Srbecky5d811202016-03-08 13:21:22 +000061 instruction_set_features_,
David Srbecky32210b92017-12-04 14:39:21 +000062 text_section_address_,
David Srbecky0c4572e2016-01-22 19:19:25 +000063 text_section_size_,
David Srbecky32210b92017-12-04 14:39:21 +000064 dex_section_address_,
65 dex_section_size_,
66 debug_info_);
David Srbecky0c4572e2016-01-22 19:19:25 +000067 }
68
69 std::vector<uint8_t>* GetResult() {
70 return &result_;
71 }
72
73 private:
74 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +000075 const InstructionSetFeatures* instruction_set_features_;
David Srbecky32210b92017-12-04 14:39:21 +000076 uint64_t text_section_address_;
David Srbecky0c4572e2016-01-22 19:19:25 +000077 size_t text_section_size_;
David Srbecky32210b92017-12-04 14:39:21 +000078 uint64_t dex_section_address_;
79 size_t dex_section_size_;
80 const debug::DebugInfo& debug_info_;
David Srbecky0c4572e2016-01-22 19:19:25 +000081 std::vector<uint8_t> result_;
82};
83
David Srbecky533c2072015-04-22 12:20:22 +010084template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010085class ElfWriterQuick final : public ElfWriter {
Vladimir Marko10c13562015-11-25 14:33:36 +000086 public:
Vladimir Markoa0431112018-06-25 09:32:54 +010087 ElfWriterQuick(const CompilerOptions& compiler_options,
Vladimir Marko10c13562015-11-25 14:33:36 +000088 File* elf_file);
89 ~ElfWriterQuick();
90
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 void Start() override;
Vladimir Markoaad75c62016-10-03 08:46:48 +000092 void PrepareDynamicSection(size_t rodata_size,
93 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +000094 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +000095 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +010096 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +000097 size_t bss_roots_offset,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010098 size_t dex_section_size) override;
99 void PrepareDebugInfo(const debug::DebugInfo& debug_info) override;
100 OutputStream* StartRoData() override;
101 void EndRoData(OutputStream* rodata) override;
102 OutputStream* StartText() override;
103 void EndText(OutputStream* text) override;
104 OutputStream* StartDataBimgRelRo() override;
105 void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) override;
106 void WriteDynamicSection() override;
107 void WriteDebugInfo(const debug::DebugInfo& debug_info) override;
108 bool StripDebugInfo() override;
109 bool End() override;
Vladimir Marko10c13562015-11-25 14:33:36 +0000110
Roland Levillainf73caca2018-08-24 17:19:07 +0100111 OutputStream* GetStream() override;
Vladimir Marko131980f2015-12-03 18:29:23 +0000112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 size_t GetLoadedSize() override;
Vladimir Marko944da602016-02-19 12:27:55 +0000114
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:
Vladimir Markoa0431112018-06-25 09:32:54 +0100119 const CompilerOptions& compiler_options_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000120 File* const elf_file_;
Vladimir Marko944da602016-02-19 12:27:55 +0000121 size_t rodata_size_;
122 size_t text_size_;
Vladimir Markob066d432018-01-03 13:14:37 +0000123 size_t data_bimg_rel_ro_size_;
Vladimir Marko944da602016-02-19 12:27:55 +0000124 size_t bss_size_;
David Srbecky32210b92017-12-04 14:39:21 +0000125 size_t dex_section_size_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000126 std::unique_ptr<BufferedOutputStream> output_stream_;
127 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000128 std::unique_ptr<DebugInfoTask> debug_info_task_;
129 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000130
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700131 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
132
Vladimir Marko10c13562015-11-25 14:33:36 +0000133 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
134};
135
Vladimir Markoa0431112018-06-25 09:32:54 +0100136std::unique_ptr<ElfWriter> CreateElfWriterQuick(const CompilerOptions& compiler_options,
Vladimir Marko10c13562015-11-25 14:33:36 +0000137 File* elf_file) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100138 if (Is64BitInstructionSet(compiler_options.GetInstructionSet())) {
139 return std::make_unique<ElfWriterQuick<ElfTypes64>>(compiler_options, elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000140 } else {
Vladimir Markoa0431112018-06-25 09:32:54 +0100141 return std::make_unique<ElfWriterQuick<ElfTypes32>>(compiler_options, elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000142 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700143}
144
David Srbecky533c2072015-04-22 12:20:22 +0100145template <typename ElfTypes>
Vladimir Markoa0431112018-06-25 09:32:54 +0100146ElfWriterQuick<ElfTypes>::ElfWriterQuick(const CompilerOptions& compiler_options, File* elf_file)
Vladimir Marko10c13562015-11-25 14:33:36 +0000147 : ElfWriter(),
148 compiler_options_(compiler_options),
149 elf_file_(elf_file),
Vladimir Marko944da602016-02-19 12:27:55 +0000150 rodata_size_(0u),
151 text_size_(0u),
Vladimir Markob066d432018-01-03 13:14:37 +0000152 data_bimg_rel_ro_size_(0u),
Vladimir Marko944da602016-02-19 12:27:55 +0000153 bss_size_(0u),
David Srbecky32210b92017-12-04 14:39:21 +0000154 dex_section_size_(0u),
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700155 output_stream_(
156 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
Vladimir Markoa0431112018-06-25 09:32:54 +0100157 builder_(new ElfBuilder<ElfTypes>(compiler_options_.GetInstructionSet(),
Vladimir Markoa0431112018-06-25 09:32:54 +0100158 output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700159
Vladimir Marko10c13562015-11-25 14:33:36 +0000160template <typename ElfTypes>
161ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700162
Vladimir Marko10c13562015-11-25 14:33:36 +0000163template <typename ElfTypes>
164void ElfWriterQuick<ElfTypes>::Start() {
165 builder_->Start();
Vladimir Markoa0431112018-06-25 09:32:54 +0100166 if (compiler_options_.GetGenerateBuildId()) {
David Srbeckye155f4b2017-12-06 15:18:38 +0000167 builder_->GetBuildId()->AllocateVirtualMemory(builder_->GetBuildId()->GetSize());
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700168 builder_->WriteBuildIdSection();
169 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000170}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171
Vladimir Marko10c13562015-11-25 14:33:36 +0000172template <typename ElfTypes>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000173void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
Vladimir Marko944da602016-02-19 12:27:55 +0000174 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000175 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000176 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100177 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000178 size_t bss_roots_offset,
179 size_t dex_section_size) {
Vladimir Marko944da602016-02-19 12:27:55 +0000180 DCHECK_EQ(rodata_size_, 0u);
181 rodata_size_ = rodata_size;
182 DCHECK_EQ(text_size_, 0u);
183 text_size_ = text_size;
Vladimir Markob066d432018-01-03 13:14:37 +0000184 DCHECK_EQ(data_bimg_rel_ro_size_, 0u);
185 data_bimg_rel_ro_size_ = data_bimg_rel_ro_size;
Vladimir Marko944da602016-02-19 12:27:55 +0000186 DCHECK_EQ(bss_size_, 0u);
187 bss_size_ = bss_size;
David Srbecky32210b92017-12-04 14:39:21 +0000188 DCHECK_EQ(dex_section_size_, 0u);
189 dex_section_size_ = dex_section_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000190 builder_->PrepareDynamicSection(elf_file_->GetPath(),
191 rodata_size_,
192 text_size_,
Vladimir Markob066d432018-01-03 13:14:37 +0000193 data_bimg_rel_ro_size_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000194 bss_size_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100195 bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000196 bss_roots_offset,
197 dex_section_size);
Vladimir Marko944da602016-02-19 12:27:55 +0000198}
199
200template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000201OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
202 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000203 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000204 return rodata;
205}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000206
Vladimir Marko10c13562015-11-25 14:33:36 +0000207template <typename ElfTypes>
208void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
209 CHECK_EQ(builder_->GetRoData(), rodata);
210 builder_->GetRoData()->End();
211}
212
213template <typename ElfTypes>
214OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
215 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000216 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000217 return text;
218}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000219
Vladimir Marko10c13562015-11-25 14:33:36 +0000220template <typename ElfTypes>
221void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
222 CHECK_EQ(builder_->GetText(), text);
223 builder_->GetText()->End();
224}
225
226template <typename ElfTypes>
Vladimir Markob066d432018-01-03 13:14:37 +0000227OutputStream* ElfWriterQuick<ElfTypes>::StartDataBimgRelRo() {
228 auto* data_bimg_rel_ro = builder_->GetDataBimgRelRo();
229 data_bimg_rel_ro->Start();
230 return data_bimg_rel_ro;
231}
232
233template <typename ElfTypes>
234void ElfWriterQuick<ElfTypes>::EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) {
235 CHECK_EQ(builder_->GetDataBimgRelRo(), data_bimg_rel_ro);
236 builder_->GetDataBimgRelRo()->End();
237}
238
239template <typename ElfTypes>
Vladimir Marko45724f92016-02-17 17:46:10 +0000240void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
Vladimir Marko944da602016-02-19 12:27:55 +0000241 builder_->WriteDynamicSection();
Vladimir Marko10c13562015-11-25 14:33:36 +0000242}
243
244template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000245void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(const debug::DebugInfo& debug_info) {
David Srbecky49b2b202019-02-01 13:35:48 +0000246 if (compiler_options_.GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000247 // Prepare the mini-debug-info in background while we do other I/O.
248 Thread* self = Thread::Current();
Yi Kongc57c6802018-10-29 14:28:56 -0700249 debug_info_task_ = std::make_unique<DebugInfoTask>(
250 builder_->GetIsa(),
251 compiler_options_.GetInstructionSetFeatures(),
252 builder_->GetText()->GetAddress(),
253 text_size_,
254 builder_->GetDex()->Exists() ? builder_->GetDex()->GetAddress() : 0,
255 dex_section_size_,
256 debug_info);
Nicolas Geoffraydfc85002023-12-20 10:55:23 +0000257 debug_info_thread_pool_.reset(ThreadPool::Create("Mini-debug-info writer", 1));
David Srbecky0c4572e2016-01-22 19:19:25 +0000258 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
259 debug_info_thread_pool_->StartWorkers(self);
260 }
261}
262
263template <typename ElfTypes>
David Srbecky32210b92017-12-04 14:39:21 +0000264void ElfWriterQuick<ElfTypes>::WriteDebugInfo(const debug::DebugInfo& debug_info) {
David Srbecky49b2b202019-02-01 13:35:48 +0000265 if (compiler_options_.GetGenerateMiniDebugInfo()) {
David Srbecky889da942021-04-30 13:03:14 +0100266 // If mini-debug-info wasn't explicitly created so far, create it now (happens in tests).
267 if (debug_info_task_ == nullptr) {
268 PrepareDebugInfo(debug_info);
269 }
David Srbecky49b2b202019-02-01 13:35:48 +0000270 // Wait for the mini-debug-info generation to finish and write it to disk.
271 Thread* self = Thread::Current();
272 DCHECK(debug_info_thread_pool_ != nullptr);
273 debug_info_thread_pool_->Wait(self, true, false);
274 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
275 }
276 // The Strip method expects debug info to be last (mini-debug-info is not stripped).
277 if (!debug_info.Empty() && compiler_options_.GetGenerateDebugInfo()) {
278 // Generate all the debug information we can.
David Srbecky7370d922019-02-12 14:00:30 +0000279 debug::WriteDebugInfo(builder_.get(), debug_info);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000280 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000281}
282
283template <typename ElfTypes>
David Srbeckyde91fd42018-07-05 22:27:08 +0100284bool ElfWriterQuick<ElfTypes>::StripDebugInfo() {
285 off_t file_size = builder_->Strip();
286 return elf_file_->SetLength(file_size) == 0;
287}
288
289template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000290bool ElfWriterQuick<ElfTypes>::End() {
291 builder_->End();
Vladimir Markoa0431112018-06-25 09:32:54 +0100292 if (compiler_options_.GetGenerateBuildId()) {
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700293 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
294 ComputeFileBuildId(&build_id);
295 builder_->WriteBuildId(build_id);
296 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000297 return builder_->Good();
298}
299
300template <typename ElfTypes>
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700301void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
302 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
303 constexpr int kBufSize = 8192;
304 std::vector<char> buffer(kBufSize);
305 int64_t offset = 0;
306 SHA_CTX ctx;
307 SHA1_Init(&ctx);
308 while (true) {
309 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
310 CHECK_GE(bytes_read, 0);
311 if (bytes_read == 0) {
312 // End of file.
313 break;
314 }
315 SHA1_Update(&ctx, buffer.data(), bytes_read);
316 offset += bytes_read;
317 }
318 SHA1_Final(*build_id, &ctx);
319}
320
321template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000322OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
323 return builder_->GetStream();
324}
325
Vladimir Marko944da602016-02-19 12:27:55 +0000326template <typename ElfTypes>
327size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
328 return builder_->GetLoadedSize();
329}
330
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000331// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100332template class ElfWriterQuick<ElfTypes32>;
333template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000334
Vladimir Marko74527972016-11-29 15:57:32 +0000335} // namespace linker
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336} // namespace art