1 /*
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
19 #include <memory>
20 #include <openssl/sha.h>
21
22 #include <android-base/logging.h>
23
24 #include "base/casts.h"
25 #include "base/globals.h"
26 #include "base/leb128.h"
27 #include "base/utils.h"
28 #include "debug/elf_debug_writer.h"
29 #include "debug/method_debug_info.h"
30 #include "driver/compiler_options.h"
31 #include "elf/elf_builder.h"
32 #include "elf/elf_utils.h"
33 #include "stream/buffered_output_stream.h"
34 #include "stream/file_output_stream.h"
35 #include "thread-current-inl.h"
36 #include "thread_pool.h"
37
38 namespace art {
39 namespace linker {
40
41 class DebugInfoTask : public Task {
42 public:
DebugInfoTask(ThreadPool * owner,InstructionSet isa,const InstructionSetFeatures * features,uint64_t text_section_address,size_t text_section_size,uint64_t dex_section_address,size_t dex_section_size,const debug::DebugInfo & debug_info)43 DebugInfoTask(ThreadPool* owner,
44 InstructionSet isa,
45 const InstructionSetFeatures* features,
46 uint64_t text_section_address,
47 size_t text_section_size,
48 uint64_t dex_section_address,
49 size_t dex_section_size,
50 const debug::DebugInfo& debug_info)
51 : owner_(owner),
52 isa_(isa),
53 instruction_set_features_(features),
54 text_section_address_(text_section_address),
55 text_section_size_(text_section_size),
56 dex_section_address_(dex_section_address),
57 dex_section_size_(dex_section_size),
58 debug_info_(debug_info) {}
59
Run(Thread *)60 void Run(Thread*) override {
61 result_ = debug::MakeMiniDebugInfo(isa_,
62 instruction_set_features_,
63 text_section_address_,
64 text_section_size_,
65 dex_section_address_,
66 dex_section_size_,
67 debug_info_);
68 }
69
WaitAndGetMiniDebugInfo()70 std::vector<uint8_t>* WaitAndGetMiniDebugInfo() {
71 owner_->Wait(Thread::Current(), true, false);
72 return &result_;
73 }
74
75 private:
76 ThreadPool* owner_;
77 InstructionSet isa_;
78 const InstructionSetFeatures* instruction_set_features_;
79 uint64_t text_section_address_;
80 size_t text_section_size_;
81 uint64_t dex_section_address_;
82 size_t dex_section_size_;
83 const debug::DebugInfo& debug_info_;
84 std::vector<uint8_t> result_;
85 };
86
87 template <typename ElfTypes>
88 class ElfWriterQuick final : public ElfWriter {
89 public:
90 ElfWriterQuick(const CompilerOptions& compiler_options,
91 File* elf_file);
92 ~ElfWriterQuick();
93
94 void Start() override;
95 void PrepareDynamicSection(size_t rodata_size,
96 size_t text_size,
97 size_t data_img_rel_ro_size,
98 size_t data_img_rel_ro_app_image_offset,
99 size_t bss_size,
100 size_t bss_methods_offset,
101 size_t bss_roots_offset,
102 size_t dex_section_size) override;
103 std::unique_ptr<ThreadPool> PrepareDebugInfo(const debug::DebugInfo& debug_info) override;
104 OutputStream* StartRoData() override;
105 void EndRoData(OutputStream* rodata) override;
106 OutputStream* StartText() override;
107 void EndText(OutputStream* text) override;
108 OutputStream* StartDataImgRelRo() override;
109 void EndDataImgRelRo(OutputStream* data_img_rel_ro) override;
110 void WriteDynamicSection() override;
111 void WriteDebugInfo(const debug::DebugInfo& debug_info) override;
112 bool StripDebugInfo() override;
113 bool End() override;
114
115 OutputStream* GetStream() override;
116
117 size_t GetLoadedSize() override;
118
119 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
120 std::vector<uint8_t>* buffer);
121
122 private:
123 const CompilerOptions& compiler_options_;
124 File* const elf_file_;
125 size_t rodata_size_;
126 size_t text_size_;
127 size_t data_img_rel_ro_size_;
128 size_t bss_size_;
129 size_t dex_section_size_;
130 std::unique_ptr<BufferedOutputStream> output_stream_;
131 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
132 std::unique_ptr<DebugInfoTask> debug_info_task_;
133
134 void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
135
136 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
137 };
138
CreateElfWriterQuick(const CompilerOptions & compiler_options,File * elf_file)139 std::unique_ptr<ElfWriter> CreateElfWriterQuick(const CompilerOptions& compiler_options,
140 File* elf_file) {
141 if (Is64BitInstructionSet(compiler_options.GetInstructionSet())) {
142 return std::make_unique<ElfWriterQuick<ElfTypes64>>(compiler_options, elf_file);
143 } else {
144 return std::make_unique<ElfWriterQuick<ElfTypes32>>(compiler_options, elf_file);
145 }
146 }
147
148 template <typename ElfTypes>
ElfWriterQuick(const CompilerOptions & compiler_options,File * elf_file)149 ElfWriterQuick<ElfTypes>::ElfWriterQuick(const CompilerOptions& compiler_options, File* elf_file)
150 : ElfWriter(),
151 compiler_options_(compiler_options),
152 elf_file_(elf_file),
153 rodata_size_(0u),
154 text_size_(0u),
155 data_img_rel_ro_size_(0u),
156 bss_size_(0u),
157 dex_section_size_(0u),
158 output_stream_(
159 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
160 builder_(new ElfBuilder<ElfTypes>(compiler_options_.GetInstructionSet(),
161 output_stream_.get())) {}
162
163 template <typename ElfTypes>
~ElfWriterQuick()164 ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
165
166 template <typename ElfTypes>
Start()167 void ElfWriterQuick<ElfTypes>::Start() {
168 builder_->Start();
169 if (compiler_options_.GetGenerateBuildId()) {
170 builder_->GetBuildId()->AllocateVirtualMemory(builder_->GetBuildId()->GetSize());
171 builder_->WriteBuildIdSection();
172 }
173 }
174
175 template <typename ElfTypes>
PrepareDynamicSection(size_t rodata_size,size_t text_size,size_t data_img_rel_ro_size,size_t data_img_rel_ro_app_image_offset,size_t bss_size,size_t bss_methods_offset,size_t bss_roots_offset,size_t dex_section_size)176 void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
177 size_t text_size,
178 size_t data_img_rel_ro_size,
179 size_t data_img_rel_ro_app_image_offset,
180 size_t bss_size,
181 size_t bss_methods_offset,
182 size_t bss_roots_offset,
183 size_t dex_section_size) {
184 DCHECK_EQ(rodata_size_, 0u);
185 rodata_size_ = rodata_size;
186 DCHECK_EQ(text_size_, 0u);
187 text_size_ = text_size;
188 DCHECK_EQ(data_img_rel_ro_size_, 0u);
189 data_img_rel_ro_size_ = data_img_rel_ro_size;
190 DCHECK_EQ(bss_size_, 0u);
191 bss_size_ = bss_size;
192 DCHECK_EQ(dex_section_size_, 0u);
193 dex_section_size_ = dex_section_size;
194 builder_->PrepareDynamicSection(elf_file_->GetPath(),
195 rodata_size_,
196 text_size_,
197 data_img_rel_ro_size_,
198 data_img_rel_ro_app_image_offset,
199 bss_size_,
200 bss_methods_offset,
201 bss_roots_offset,
202 dex_section_size);
203 }
204
205 template <typename ElfTypes>
StartRoData()206 OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
207 auto* rodata = builder_->GetRoData();
208 rodata->Start();
209 return rodata;
210 }
211
212 template <typename ElfTypes>
EndRoData(OutputStream * rodata)213 void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
214 CHECK_EQ(builder_->GetRoData(), rodata);
215 builder_->GetRoData()->End();
216 }
217
218 template <typename ElfTypes>
StartText()219 OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
220 auto* text = builder_->GetText();
221 text->Start();
222 return text;
223 }
224
225 template <typename ElfTypes>
EndText(OutputStream * text)226 void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
227 CHECK_EQ(builder_->GetText(), text);
228 builder_->GetText()->End();
229 }
230
231 template <typename ElfTypes>
StartDataImgRelRo()232 OutputStream* ElfWriterQuick<ElfTypes>::StartDataImgRelRo() {
233 auto* data_img_rel_ro = builder_->GetDataImgRelRo();
234 data_img_rel_ro->Start();
235 return data_img_rel_ro;
236 }
237
238 template <typename ElfTypes>
EndDataImgRelRo(OutputStream * data_img_rel_ro)239 void ElfWriterQuick<ElfTypes>::EndDataImgRelRo(OutputStream* data_img_rel_ro) {
240 CHECK_EQ(builder_->GetDataImgRelRo(), data_img_rel_ro);
241 builder_->GetDataImgRelRo()->End();
242 }
243
244 template <typename ElfTypes>
WriteDynamicSection()245 void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
246 builder_->WriteDynamicSection();
247 }
248
249 template <typename ElfTypes>
PrepareDebugInfo(const debug::DebugInfo & debug_info)250 std::unique_ptr<ThreadPool> ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
251 const debug::DebugInfo& debug_info) {
252 std::unique_ptr<ThreadPool> thread_pool;
253 if (compiler_options_.GetGenerateMiniDebugInfo()) {
254 thread_pool.reset(ThreadPool::Create("Mini-debug-info writer", 1));
255 // Prepare the mini-debug-info in background while we do other I/O.
256 Thread* self = Thread::Current();
257 debug_info_task_ = std::make_unique<DebugInfoTask>(
258 thread_pool.get(),
259 builder_->GetIsa(),
260 compiler_options_.GetInstructionSetFeatures(),
261 builder_->GetText()->GetAddress(),
262 text_size_,
263 builder_->GetDex()->Exists() ? builder_->GetDex()->GetAddress() : 0,
264 dex_section_size_,
265 debug_info);
266 thread_pool->AddTask(self, debug_info_task_.get());
267 thread_pool->StartWorkers(self);
268 }
269 return thread_pool;
270 }
271
272 template <typename ElfTypes>
WriteDebugInfo(const debug::DebugInfo & debug_info)273 void ElfWriterQuick<ElfTypes>::WriteDebugInfo(const debug::DebugInfo& debug_info) {
274 std::unique_ptr<ThreadPool> thread_pool;
275 if (compiler_options_.GetGenerateMiniDebugInfo()) {
276 // If mini-debug-info wasn't explicitly created so far, create it now (happens in tests).
277 if (debug_info_task_ == nullptr) {
278 thread_pool = PrepareDebugInfo(debug_info);
279 }
280 builder_->WriteSection(".gnu_debugdata", debug_info_task_->WaitAndGetMiniDebugInfo());
281 }
282 // The Strip method expects debug info to be last (mini-debug-info is not stripped).
283 if (!debug_info.Empty() && compiler_options_.GetGenerateDebugInfo()) {
284 // Generate all the debug information we can.
285 debug::WriteDebugInfo(builder_.get(), debug_info);
286 }
287 }
288
289 template <typename ElfTypes>
StripDebugInfo()290 bool ElfWriterQuick<ElfTypes>::StripDebugInfo() {
291 off_t file_size = builder_->Strip();
292 return elf_file_->SetLength(file_size) == 0;
293 }
294
295 template <typename ElfTypes>
End()296 bool ElfWriterQuick<ElfTypes>::End() {
297 builder_->End();
298 if (compiler_options_.GetGenerateBuildId()) {
299 uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
300 ComputeFileBuildId(&build_id);
301 builder_->WriteBuildId(build_id);
302 }
303 return builder_->Good();
304 }
305
306 template <typename ElfTypes>
ComputeFileBuildId(uint8_t (* build_id)[ElfBuilder<ElfTypes>::kBuildIdLen])307 void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
308 uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
309 constexpr int kBufSize = 8192;
310 std::vector<char> buffer(kBufSize);
311 int64_t offset = 0;
312 SHA_CTX ctx;
313 SHA1_Init(&ctx);
314 while (true) {
315 int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
316 CHECK_GE(bytes_read, 0);
317 if (bytes_read == 0) {
318 // End of file.
319 break;
320 }
321 SHA1_Update(&ctx, buffer.data(), bytes_read);
322 offset += bytes_read;
323 }
324 SHA1_Final(*build_id, &ctx);
325 }
326
327 template <typename ElfTypes>
GetStream()328 OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
329 return builder_->GetStream();
330 }
331
332 template <typename ElfTypes>
GetLoadedSize()333 size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
334 return builder_->GetLoadedSize();
335 }
336
337 // Explicit instantiations
338 template class ElfWriterQuick<ElfTypes32>;
339 template class ElfWriterQuick<ElfTypes64>;
340
341 } // namespace linker
342 } // namespace art
343