• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   builder_->ReserveSpaceForDynamicSection(elf_file_->GetPath());
174 }
175 
176 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)177 void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
178                                                      size_t text_size,
179                                                      size_t data_img_rel_ro_size,
180                                                      size_t data_img_rel_ro_app_image_offset,
181                                                      size_t bss_size,
182                                                      size_t bss_methods_offset,
183                                                      size_t bss_roots_offset,
184                                                      size_t dex_section_size) {
185   DCHECK_EQ(rodata_size_, 0u);
186   rodata_size_ = rodata_size;
187   DCHECK_EQ(text_size_, 0u);
188   text_size_ = text_size;
189   DCHECK_EQ(data_img_rel_ro_size_, 0u);
190   data_img_rel_ro_size_ = data_img_rel_ro_size;
191   DCHECK_EQ(bss_size_, 0u);
192   bss_size_ = bss_size;
193   DCHECK_EQ(dex_section_size_, 0u);
194   dex_section_size_ = dex_section_size;
195   builder_->PrepareDynamicSection(elf_file_->GetPath(),
196                                   rodata_size_,
197                                   text_size_,
198                                   data_img_rel_ro_size_,
199                                   data_img_rel_ro_app_image_offset,
200                                   bss_size_,
201                                   bss_methods_offset,
202                                   bss_roots_offset,
203                                   dex_section_size);
204 }
205 
206 template <typename ElfTypes>
StartRoData()207 OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
208   auto* rodata = builder_->GetRoData();
209   rodata->Start();
210   return rodata;
211 }
212 
213 template <typename ElfTypes>
EndRoData(OutputStream * rodata)214 void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
215   CHECK_EQ(builder_->GetRoData(), rodata);
216   builder_->GetRoData()->End();
217 }
218 
219 template <typename ElfTypes>
StartText()220 OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
221   auto* text = builder_->GetText();
222   text->Start();
223   return text;
224 }
225 
226 template <typename ElfTypes>
EndText(OutputStream * text)227 void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
228   CHECK_EQ(builder_->GetText(), text);
229   builder_->GetText()->End();
230 }
231 
232 template <typename ElfTypes>
StartDataImgRelRo()233 OutputStream* ElfWriterQuick<ElfTypes>::StartDataImgRelRo() {
234   auto* data_img_rel_ro = builder_->GetDataImgRelRo();
235   data_img_rel_ro->Start();
236   return data_img_rel_ro;
237 }
238 
239 template <typename ElfTypes>
EndDataImgRelRo(OutputStream * data_img_rel_ro)240 void ElfWriterQuick<ElfTypes>::EndDataImgRelRo(OutputStream* data_img_rel_ro) {
241   CHECK_EQ(builder_->GetDataImgRelRo(), data_img_rel_ro);
242   builder_->GetDataImgRelRo()->End();
243 }
244 
245 template <typename ElfTypes>
WriteDynamicSection()246 void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
247   builder_->WriteDynamicSection();
248 }
249 
250 template <typename ElfTypes>
PrepareDebugInfo(const debug::DebugInfo & debug_info)251 std::unique_ptr<ThreadPool> ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
252     const debug::DebugInfo& debug_info) {
253   std::unique_ptr<ThreadPool> thread_pool;
254   if (compiler_options_.GetGenerateMiniDebugInfo()) {
255     thread_pool.reset(ThreadPool::Create("Mini-debug-info writer", 1));
256     // Prepare the mini-debug-info in background while we do other I/O.
257     Thread* self = Thread::Current();
258     debug_info_task_ = std::make_unique<DebugInfoTask>(
259         thread_pool.get(),
260         builder_->GetIsa(),
261         compiler_options_.GetInstructionSetFeatures(),
262         builder_->GetText()->GetAddress(),
263         text_size_,
264         builder_->GetDex()->Exists() ? builder_->GetDex()->GetAddress() : 0,
265         dex_section_size_,
266         debug_info);
267     thread_pool->AddTask(self, debug_info_task_.get());
268     thread_pool->StartWorkers(self);
269   }
270   return thread_pool;
271 }
272 
273 template <typename ElfTypes>
WriteDebugInfo(const debug::DebugInfo & debug_info)274 void ElfWriterQuick<ElfTypes>::WriteDebugInfo(const debug::DebugInfo& debug_info) {
275   std::unique_ptr<ThreadPool> thread_pool;
276   if (compiler_options_.GetGenerateMiniDebugInfo()) {
277     // If mini-debug-info wasn't explicitly created so far, create it now (happens in tests).
278     if (debug_info_task_ == nullptr) {
279       thread_pool = PrepareDebugInfo(debug_info);
280     }
281     builder_->WriteSection(".gnu_debugdata", debug_info_task_->WaitAndGetMiniDebugInfo());
282   }
283   // The Strip method expects debug info to be last (mini-debug-info is not stripped).
284   if (!debug_info.Empty() && compiler_options_.GetGenerateDebugInfo()) {
285     // Generate all the debug information we can.
286     debug::WriteDebugInfo(builder_.get(), debug_info);
287   }
288 }
289 
290 template <typename ElfTypes>
StripDebugInfo()291 bool ElfWriterQuick<ElfTypes>::StripDebugInfo() {
292   off_t file_size = builder_->Strip();
293   return elf_file_->SetLength(file_size) == 0;
294 }
295 
296 template <typename ElfTypes>
End()297 bool ElfWriterQuick<ElfTypes>::End() {
298   builder_->End();
299   if (compiler_options_.GetGenerateBuildId()) {
300     uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
301     ComputeFileBuildId(&build_id);
302     builder_->WriteBuildId(build_id);
303   }
304   return builder_->Good();
305 }
306 
307 template <typename ElfTypes>
ComputeFileBuildId(uint8_t (* build_id)[ElfBuilder<ElfTypes>::kBuildIdLen])308 void ElfWriterQuick<ElfTypes>::ComputeFileBuildId(
309     uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) {
310   constexpr int kBufSize = 8192;
311   std::vector<char> buffer(kBufSize);
312   int64_t offset = 0;
313   SHA_CTX ctx;
314   SHA1_Init(&ctx);
315   while (true) {
316     int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset);
317     CHECK_GE(bytes_read, 0);
318     if (bytes_read == 0) {
319       // End of file.
320       break;
321     }
322     SHA1_Update(&ctx, buffer.data(), bytes_read);
323     offset += bytes_read;
324   }
325   SHA1_Final(*build_id, &ctx);
326 }
327 
328 template <typename ElfTypes>
GetStream()329 OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
330   return builder_->GetStream();
331 }
332 
333 template <typename ElfTypes>
GetLoadedSize()334 size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
335   return builder_->GetLoadedSize();
336 }
337 
338 // Explicit instantiations
339 template class ElfWriterQuick<ElfTypes32>;
340 template class ElfWriterQuick<ElfTypes64>;
341 
342 }  // namespace linker
343 }  // namespace art
344