• 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 <openssl/sha.h>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 #include "base/casts.h"
24 #include "base/logging.h"
25 #include "compiled_method.h"
26 #include "debug/elf_debug_writer.h"
27 #include "debug/method_debug_info.h"
28 #include "driver/compiler_options.h"
29 #include "elf.h"
30 #include "elf_builder.h"
31 #include "elf_utils.h"
32 #include "globals.h"
33 #include "leb128.h"
34 #include "linker/buffered_output_stream.h"
35 #include "linker/file_output_stream.h"
36 #include "thread-current-inl.h"
37 #include "thread_pool.h"
38 #include "utils.h"
39 
40 namespace art {
41 
42 // .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.
48 // Let's use .debug_frame because it is easier to strip or compress.
49 constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
50 
51 class DebugInfoTask : public Task {
52  public:
DebugInfoTask(InstructionSet isa,const InstructionSetFeatures * features,size_t rodata_section_size,size_t text_section_size,const ArrayRef<const debug::MethodDebugInfo> & method_infos)53   DebugInfoTask(InstructionSet isa,
54                 const InstructionSetFeatures* features,
55                 size_t rodata_section_size,
56                 size_t text_section_size,
57                 const ArrayRef<const debug::MethodDebugInfo>& method_infos)
58       : isa_(isa),
59         instruction_set_features_(features),
60         rodata_section_size_(rodata_section_size),
61         text_section_size_(text_section_size),
62         method_infos_(method_infos) {
63   }
64 
Run(Thread *)65   void Run(Thread*) {
66     result_ = debug::MakeMiniDebugInfo(isa_,
67                                        instruction_set_features_,
68                                        rodata_section_size_,
69                                        text_section_size_,
70                                        method_infos_);
71   }
72 
GetResult()73   std::vector<uint8_t>* GetResult() {
74     return &result_;
75   }
76 
77  private:
78   InstructionSet isa_;
79   const InstructionSetFeatures* instruction_set_features_;
80   size_t rodata_section_size_;
81   size_t text_section_size_;
82   const ArrayRef<const debug::MethodDebugInfo> method_infos_;
83   std::vector<uint8_t> result_;
84 };
85 
86 template <typename ElfTypes>
87 class ElfWriterQuick FINAL : public ElfWriter {
88  public:
89   ElfWriterQuick(InstructionSet instruction_set,
90                  const InstructionSetFeatures* features,
91                  const CompilerOptions* compiler_options,
92                  File* elf_file);
93   ~ElfWriterQuick();
94 
95   void Start() OVERRIDE;
96   void PrepareDynamicSection(size_t rodata_size,
97                              size_t text_size,
98                              size_t bss_size,
99                              size_t bss_methods_offset,
100                              size_t bss_roots_offset) OVERRIDE;
101   void PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE;
102   OutputStream* StartRoData() OVERRIDE;
103   void EndRoData(OutputStream* rodata) OVERRIDE;
104   OutputStream* StartText() OVERRIDE;
105   void EndText(OutputStream* text) OVERRIDE;
106   void WriteDynamicSection() OVERRIDE;
107   void WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE;
108   bool End() OVERRIDE;
109 
110   virtual OutputStream* GetStream() OVERRIDE;
111 
112   size_t GetLoadedSize() OVERRIDE;
113 
114   static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
115                                std::vector<uint8_t>* buffer);
116 
117  private:
118   const InstructionSetFeatures* instruction_set_features_;
119   const CompilerOptions* const compiler_options_;
120   File* const elf_file_;
121   size_t rodata_size_;
122   size_t text_size_;
123   size_t bss_size_;
124   std::unique_ptr<BufferedOutputStream> output_stream_;
125   std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
126   std::unique_ptr<DebugInfoTask> debug_info_task_;
127   std::unique_ptr<ThreadPool> debug_info_thread_pool_;
128 
129   void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]);
130 
131   DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
132 };
133 
CreateElfWriterQuick(InstructionSet instruction_set,const InstructionSetFeatures * features,const CompilerOptions * compiler_options,File * elf_file)134 std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
135                                                 const InstructionSetFeatures* features,
136                                                 const CompilerOptions* compiler_options,
137                                                 File* elf_file) {
138   if (Is64BitInstructionSet(instruction_set)) {
139     return std::make_unique<ElfWriterQuick<ElfTypes64>>(instruction_set,
140                                                         features,
141                                                         compiler_options,
142                                                         elf_file);
143   } else {
144     return std::make_unique<ElfWriterQuick<ElfTypes32>>(instruction_set,
145                                                         features,
146                                                         compiler_options,
147                                                         elf_file);
148   }
149 }
150 
151 template <typename ElfTypes>
ElfWriterQuick(InstructionSet instruction_set,const InstructionSetFeatures * features,const CompilerOptions * compiler_options,File * elf_file)152 ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
153                                          const InstructionSetFeatures* features,
154                                          const CompilerOptions* compiler_options,
155                                          File* elf_file)
156     : ElfWriter(),
157       instruction_set_features_(features),
158       compiler_options_(compiler_options),
159       elf_file_(elf_file),
160       rodata_size_(0u),
161       text_size_(0u),
162       bss_size_(0u),
163       output_stream_(
164           std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file))),
165       builder_(new ElfBuilder<ElfTypes>(instruction_set, features, output_stream_.get())) {}
166 
167 template <typename ElfTypes>
~ElfWriterQuick()168 ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
169 
170 template <typename ElfTypes>
Start()171 void ElfWriterQuick<ElfTypes>::Start() {
172   builder_->Start();
173   if (compiler_options_->GetGenerateBuildId()) {
174     builder_->WriteBuildIdSection();
175   }
176 }
177 
178 template <typename ElfTypes>
PrepareDynamicSection(size_t rodata_size,size_t text_size,size_t bss_size,size_t bss_methods_offset,size_t bss_roots_offset)179 void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size,
180                                                      size_t text_size,
181                                                      size_t bss_size,
182                                                      size_t bss_methods_offset,
183                                                      size_t bss_roots_offset) {
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(bss_size_, 0u);
189   bss_size_ = bss_size;
190   builder_->PrepareDynamicSection(elf_file_->GetPath(),
191                                   rodata_size_,
192                                   text_size_,
193                                   bss_size_,
194                                   bss_methods_offset,
195                                   bss_roots_offset);
196 }
197 
198 template <typename ElfTypes>
StartRoData()199 OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
200   auto* rodata = builder_->GetRoData();
201   rodata->Start();
202   return rodata;
203 }
204 
205 template <typename ElfTypes>
EndRoData(OutputStream * rodata)206 void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
207   CHECK_EQ(builder_->GetRoData(), rodata);
208   builder_->GetRoData()->End();
209 }
210 
211 template <typename ElfTypes>
StartText()212 OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
213   auto* text = builder_->GetText();
214   text->Start();
215   return text;
216 }
217 
218 template <typename ElfTypes>
EndText(OutputStream * text)219 void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
220   CHECK_EQ(builder_->GetText(), text);
221   builder_->GetText()->End();
222 }
223 
224 template <typename ElfTypes>
WriteDynamicSection()225 void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
226   if (bss_size_ != 0u) {
227     builder_->GetBss()->WriteNoBitsSection(bss_size_);
228   }
229   if (builder_->GetIsa() == kMips || builder_->GetIsa() == kMips64) {
230     builder_->WriteMIPSabiflagsSection();
231   }
232   builder_->WriteDynamicSection();
233 }
234 
235 template <typename ElfTypes>
PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo> & method_infos)236 void ElfWriterQuick<ElfTypes>::PrepareDebugInfo(
237     const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
238   if (!method_infos.empty() && compiler_options_->GetGenerateMiniDebugInfo()) {
239     // 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>(
242         new DebugInfoTask(builder_->GetIsa(),
243                           instruction_set_features_,
244                           rodata_size_,
245                           text_size_,
246                           method_infos));
247     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 
254 template <typename ElfTypes>
WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo> & method_infos)255 void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
256     const ArrayRef<const debug::MethodDebugInfo>& method_infos) {
257   if (!method_infos.empty()) {
258     if (compiler_options_->GetGenerateDebugInfo()) {
259       // Generate all the debug information we can.
260       debug::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */);
261     }
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     }
269   }
270 }
271 
272 template <typename ElfTypes>
End()273 bool ElfWriterQuick<ElfTypes>::End() {
274   builder_->End();
275   if (compiler_options_->GetGenerateBuildId()) {
276     uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen];
277     ComputeFileBuildId(&build_id);
278     builder_->WriteBuildId(build_id);
279   }
280   return builder_->Good();
281 }
282 
283 template <typename ElfTypes>
ComputeFileBuildId(uint8_t (* build_id)[ElfBuilder<ElfTypes>::kBuildIdLen])284 void 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 
304 template <typename ElfTypes>
GetStream()305 OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
306   return builder_->GetStream();
307 }
308 
309 template <typename ElfTypes>
GetLoadedSize()310 size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() {
311   return builder_->GetLoadedSize();
312 }
313 
314 // Explicit instantiations
315 template class ElfWriterQuick<ElfTypes32>;
316 template class ElfWriterQuick<ElfTypes64>;
317 
318 }  // namespace art
319