blob: 5d6dd2e1d776d2bae50b72344ec805c49faffe86 [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 Marko20f85592015-03-19 10:07:02 +000025#include "compiled_method.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000026#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000027#include "debug/method_debug_info.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000029#include "elf.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070030#include "elf_builder.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000031#include "elf_utils.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"
Andreas Gampeb486a982017-06-01 13:45:54 -070036#include "thread-current-inl.h"
David Srbecky0c4572e2016-01-22 19:19:25 +000037#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,
David Srbecky5d811202016-03-08 13:21:22 +000054 const InstructionSetFeatures* features,
David Srbecky0c4572e2016-01-22 19:19:25 +000055 size_t rodata_section_size,
56 size_t text_section_size,
David Srbeckyc5bfa972016-02-05 15:49:10 +000057 const ArrayRef<const debug::MethodDebugInfo>& method_infos)
David Srbecky0c4572e2016-01-22 19:19:25 +000058 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +000059 instruction_set_features_(features),
David Srbecky0c4572e2016-01-22 19:19:25 +000060 rodata_section_size_(rodata_section_size),
61 text_section_size_(text_section_size),
62 method_infos_(method_infos) {
63 }
64
65 void Run(Thread*) {
David Srbeckyc5bfa972016-02-05 15:49:10 +000066 result_ = debug::MakeMiniDebugInfo(isa_,
David Srbecky5d811202016-03-08 13:21:22 +000067 instruction_set_features_,
David Srbecky0c4572e2016-01-22 19:19:25 +000068 rodata_section_size_,
69 text_section_size_,
70 method_infos_);
71 }
72
73 std::vector<uint8_t>* GetResult() {
74 return &result_;
75 }
76
77 private:
78 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +000079 const InstructionSetFeatures* instruction_set_features_;
David Srbecky0c4572e2016-01-22 19:19:25 +000080 size_t rodata_section_size_;
81 size_t text_section_size_;
Pirama Arumuga Nainarf96a2db2017-06-07 15:57:30 -070082 const ArrayRef<const debug::MethodDebugInfo> method_infos_;
David Srbecky0c4572e2016-01-22 19:19:25 +000083 std::vector<uint8_t> result_;
84};
85
David Srbecky533c2072015-04-22 12:20:22 +010086template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000087class ElfWriterQuick FINAL : public ElfWriter {
88 public:
89 ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +000090 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +000091 const CompilerOptions* compiler_options,
92 File* elf_file);
93 ~ElfWriterQuick();
94
95 void Start() OVERRIDE;
Vladimir Markoaad75c62016-10-03 08:46:48 +000096 void PrepareDynamicSection(size_t rodata_size,
97 size_t text_size,
98 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +010099 size_t bss_methods_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000100 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 bool End() OVERRIDE;
109
Vladimir Marko131980f2015-12-03 18:29:23 +0000110 virtual OutputStream* GetStream() OVERRIDE;
111
Vladimir Marko944da602016-02-19 12:27:55 +0000112 size_t GetLoadedSize() OVERRIDE;
113
Vladimir Marko10c13562015-11-25 14:33:36 +0000114 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
115 std::vector<uint8_t>* buffer);
116
117 private:
David Srbecky5d811202016-03-08 13:21:22 +0000118 const InstructionSetFeatures* instruction_set_features_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000119 const CompilerOptions* const compiler_options_;
120 File* const elf_file_;
Vladimir Marko944da602016-02-19 12:27:55 +0000121 size_t rodata_size_;
122 size_t text_size_;
123 size_t bss_size_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000124 std::unique_ptr<BufferedOutputStream> output_stream_;
125 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky0c4572e2016-01-22 19:19:25 +0000126 std::unique_ptr<DebugInfoTask> debug_info_task_;
127 std::unique_ptr<ThreadPool> debug_info_thread_pool_;
Vladimir Marko10c13562015-11-25 14:33:36 +0000128
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700129 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
130
Vladimir Marko10c13562015-11-25 14:33:36 +0000131 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
132};
133
134std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000135 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000136 const CompilerOptions* compiler_options,
137 File* elf_file) {
138 if (Is64BitInstructionSet(instruction_set)) {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700139 return std::make_unique<ElfWriterQuick<ElfTypes64>>(instruction_set,
140 features,
141 compiler_options,
142 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000143 } else {
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700144 return std::make_unique<ElfWriterQuick<ElfTypes32>>(instruction_set,
145 features,
146 compiler_options,
147 elf_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000148 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700149}
150
David Srbecky533c2072015-04-22 12:20:22 +0100151template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000152ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
David Srbecky5d811202016-03-08 13:21:22 +0000153 const InstructionSetFeatures* features,
Vladimir Marko10c13562015-11-25 14:33:36 +0000154 const CompilerOptions* compiler_options,
155 File* elf_file)
156 : ElfWriter(),
David Srbecky5d811202016-03-08 13:21:22 +0000157 instruction_set_features_(features),
Vladimir Marko10c13562015-11-25 14:33:36 +0000158 compiler_options_(compiler_options),
159 elf_file_(elf_file),
Vladimir Marko944da602016-02-19 12:27:55 +0000160 rodata_size_(0u),
161 text_size_(0u),
162 bss_size_(0u),
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700163 output_stream_(
164 std::make_unique<BufferedOutputStream>(std::make_unique<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,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100182 size_t bss_methods_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000183 size_t bss_roots_offset) {
Vladimir Marko944da602016-02-19 12:27:55 +0000184 DCHECK_EQ(rodata_size_, 0u);
185 rodata_size_ = rodata_size;
186 DCHECK_EQ(text_size_, 0u);
187 text_size_ = text_size;
188 DCHECK_EQ(bss_size_, 0u);
189 bss_size_ = bss_size;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000190 builder_->PrepareDynamicSection(elf_file_->GetPath(),
191 rodata_size_,
192 text_size_,
193 bss_size_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100194 bss_methods_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000195 bss_roots_offset);
Vladimir Marko944da602016-02-19 12:27:55 +0000196}
197
198template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000199OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
200 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000201 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000202 return rodata;
203}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000204
Vladimir Marko10c13562015-11-25 14:33:36 +0000205template <typename ElfTypes>
206void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
207 CHECK_EQ(builder_->GetRoData(), rodata);
208 builder_->GetRoData()->End();
209}
210
211template <typename ElfTypes>
212OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
213 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000214 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000215 return text;
216}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000217
Vladimir Marko10c13562015-11-25 14:33:36 +0000218template <typename ElfTypes>
219void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
220 CHECK_EQ(builder_->GetText(), text);
221 builder_->GetText()->End();
222}
223
224template <typename ElfTypes>
Vladimir Marko45724f92016-02-17 17:46:10 +0000225void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
Vladimir Marko944da602016-02-19 12:27:55 +0000226 if (bss_size_ != 0u) {
227 builder_->GetBss()->WriteNoBitsSection(bss_size_);
228 }
Douglas Leung316a2182015-09-17 15:26:25 -0700229 if (builder_->GetIsa() == kMips || builder_->GetIsa() == kMips64) {
230 builder_->WriteMIPSabiflagsSection();
231 }
Vladimir Marko944da602016-02-19 12:27:55 +0000232 builder_->WriteDynamicSection();
Vladimir Marko10c13562015-11-25 14:33:36 +0000233}
234
235template <typename ElfTypes>
David Srbecky0c4572e2016-01-22 19:19:25 +0000236void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
David Srbeckyc5bfa972016-02-05 15:49:10 +0000237 const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
David Srbecky370339c2016-02-05 11:42:23 +0000238 if (!method_infos.empty() && compiler_options_->GetGenerateMiniDebugInfo()) {
David Srbecky0c4572e2016-01-22 19:19:25 +0000239 // Prepare the mini-debug-info in background while we do other I/O.
240 Thread* self = Thread::Current();
241 debug_info_task_ = std::unique_ptr<DebugInfoTask>(
David Srbecky5d811202016-03-08 13:21:22 +0000242 new DebugInfoTask(builder_->GetIsa(),
243 instruction_set_features_,
244 rodata_size_,
245 text_size_,
246 method_infos));
David Srbecky0c4572e2016-01-22 19:19:25 +0000247 debug_info_thread_pool_ = std::unique_ptr<ThreadPool>(
248 new ThreadPool("Mini-debug-info writer", 1));
249 debug_info_thread_pool_->AddTask(self, debug_info_task_.get());
250 debug_info_thread_pool_->StartWorkers(self);
251 }
252}
253
254template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000255void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
David Srbeckyc5bfa972016-02-05 15:49:10 +0000256 const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
David Srbecky370339c2016-02-05 11:42:23 +0000257 if (!method_infos.empty()) {
258 if (compiler_options_->GetGenerateDebugInfo()) {
259 // Generate all the debug information we can.
David Srbeckyc5bfa972016-02-05 15:49:10 +0000260 debug::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */);
David Srbecky370339c2016-02-05 11:42:23 +0000261 }
262 if (compiler_options_->GetGenerateMiniDebugInfo()) {
263 // Wait for the mini-debug-info generation to finish and write it to disk.
264 Thread* self = Thread::Current();
265 DCHECK(debug_info_thread_pool_ != nullptr);
266 debug_info_thread_pool_->Wait(self, true, false);
267 builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult());
268 }
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000269 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000270}
271
272template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000273bool ElfWriterQuick<ElfTypes>::End() {
274 builder_->End();
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700275 if (compiler_options_->GetGenerateBuildId()) {
276 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
277 ComputeFileBuildId(&build_id);
278 builder_->WriteBuildId(build_id);
279 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000280 return builder_->Good();
281}
282
283template <typename ElfTypes>
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700284void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
285 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
286 constexpr int kBufSize = 8192;
287 std::vector<char> buffer(kBufSize);
288 int64_t offset = 0;
289 SHA_CTX ctx;
290 SHA1_Init(&ctx);
291 while (true) {
292 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
293 CHECK_GE(bytes_read, 0);
294 if (bytes_read == 0) {
295 // End of file.
296 break;
297 }
298 SHA1_Update(&ctx, buffer.data(), bytes_read);
299 offset += bytes_read;
300 }
301 SHA1_Final(*build_id, &ctx);
302}
303
304template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000305OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
306 return builder_->GetStream();
307}
308
Vladimir Marko944da602016-02-19 12:27:55 +0000309template <typename ElfTypes>
310size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
311 return builder_->GetLoadedSize();
312}
313
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000314// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100315template class ElfWriterQuick<ElfTypes32>;
316template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000317
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318} // namespace art