Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 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 | |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 19 | #include <openssl/sha.h> |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 20 | #include <unordered_map> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 21 | #include <unordered_set> |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 22 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 23 | #include "base/casts.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include "base/logging.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 26 | #include "compiled_method.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 27 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 28 | #include "debug/method_debug_info.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 29 | #include "driver/compiler_options.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 30 | #include "elf.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 31 | #include "elf_builder.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 32 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "globals.h" |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 34 | #include "leb128.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 35 | #include "linker/buffered_output_stream.h" |
| 36 | #include "linker/file_output_stream.h" |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 37 | #include "thread-inl.h" |
| 38 | #include "thread_pool.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 39 | #include "utils.h" |
| 40 | |
| 41 | namespace art { |
| 42 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 43 | // .eh_frame and .debug_frame are almost identical. |
| 44 | // Except for some minor formatting differences, the main difference |
| 45 | // is that .eh_frame is allocated within the running program because |
| 46 | // it is used by C++ exception handling (which we do not use so we |
| 47 | // can choose either). C++ compilers generally tend to use .eh_frame |
| 48 | // because if they need it sometimes, they might as well always use it. |
David Srbecky | aaf143d | 2015-05-21 14:03:48 +0100 | [diff] [blame] | 49 | // Let's use .debug_frame because it is easier to strip or compress. |
| 50 | constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 51 | |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 52 | class DebugInfoTask : public Task { |
| 53 | public: |
| 54 | DebugInfoTask(InstructionSet isa, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 55 | const InstructionSetFeatures* features, |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 56 | size_t rodata_section_size, |
| 57 | size_t text_section_size, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 58 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 59 | : isa_(isa), |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 60 | instruction_set_features_(features), |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 61 | rodata_section_size_(rodata_section_size), |
| 62 | text_section_size_(text_section_size), |
| 63 | method_infos_(method_infos) { |
| 64 | } |
| 65 | |
| 66 | void Run(Thread*) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 67 | result_ = debug::MakeMiniDebugInfo(isa_, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 68 | instruction_set_features_, |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 69 | rodata_section_size_, |
| 70 | text_section_size_, |
| 71 | method_infos_); |
| 72 | } |
| 73 | |
| 74 | std::vector<uint8_t>* GetResult() { |
| 75 | return &result_; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | InstructionSet isa_; |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 80 | const InstructionSetFeatures* instruction_set_features_; |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 81 | size_t rodata_section_size_; |
| 82 | size_t text_section_size_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 83 | const ArrayRef<const debug::MethodDebugInfo>& method_infos_; |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 84 | std::vector<uint8_t> result_; |
| 85 | }; |
| 86 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 87 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 88 | class ElfWriterQuick FINAL : public ElfWriter { |
| 89 | public: |
| 90 | ElfWriterQuick(InstructionSet instruction_set, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 91 | const InstructionSetFeatures* features, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 92 | const CompilerOptions* compiler_options, |
| 93 | File* elf_file); |
| 94 | ~ElfWriterQuick(); |
| 95 | |
| 96 | void Start() OVERRIDE; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 97 | void PrepareDynamicSection(size_t rodata_size, |
| 98 | size_t text_size, |
| 99 | size_t bss_size, |
| 100 | size_t bss_roots_offset) OVERRIDE; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 101 | void PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 102 | OutputStream* StartRoData() OVERRIDE; |
| 103 | void EndRoData(OutputStream* rodata) OVERRIDE; |
| 104 | OutputStream* StartText() OVERRIDE; |
| 105 | void EndText(OutputStream* text) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 106 | void WriteDynamicSection() OVERRIDE; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 107 | void WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 108 | bool End() OVERRIDE; |
| 109 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 110 | virtual OutputStream* GetStream() OVERRIDE; |
| 111 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 112 | size_t GetLoadedSize() OVERRIDE; |
| 113 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 114 | static void EncodeOatPatches(const std::vector<uintptr_t>& locations, |
| 115 | std::vector<uint8_t>* buffer); |
| 116 | |
| 117 | private: |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 118 | const InstructionSetFeatures* instruction_set_features_; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 119 | const CompilerOptions* const compiler_options_; |
| 120 | File* const elf_file_; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 121 | size_t rodata_size_; |
| 122 | size_t text_size_; |
| 123 | size_t bss_size_; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 124 | std::unique_ptr<BufferedOutputStream> output_stream_; |
| 125 | std::unique_ptr<ElfBuilder<ElfTypes>> builder_; |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 126 | std::unique_ptr<DebugInfoTask> debug_info_task_; |
| 127 | std::unique_ptr<ThreadPool> debug_info_thread_pool_; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 128 | |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 129 | void ComputeFileBuildId(uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]); |
| 130 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 131 | DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick); |
| 132 | }; |
| 133 | |
| 134 | std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 135 | const InstructionSetFeatures* features, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 136 | const CompilerOptions* compiler_options, |
| 137 | File* elf_file) { |
| 138 | if (Is64BitInstructionSet(instruction_set)) { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 139 | return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, |
| 140 | features, |
| 141 | compiler_options, |
| 142 | elf_file); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 143 | } else { |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 144 | return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, |
| 145 | features, |
| 146 | compiler_options, |
| 147 | elf_file); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 148 | } |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 149 | } |
| 150 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 151 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 152 | ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set, |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 153 | const InstructionSetFeatures* features, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 154 | const CompilerOptions* compiler_options, |
| 155 | File* elf_file) |
| 156 | : ElfWriter(), |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 157 | instruction_set_features_(features), |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 158 | compiler_options_(compiler_options), |
| 159 | elf_file_(elf_file), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 160 | rodata_size_(0u), |
| 161 | text_size_(0u), |
| 162 | bss_size_(0u), |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 163 | output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))), |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 164 | builder_(new ElfBuilder<ElfTypes>(instruction_set, features, output_stream_.get())) {} |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 165 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 166 | template <typename ElfTypes> |
| 167 | ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {} |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 168 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 169 | template <typename ElfTypes> |
| 170 | void ElfWriterQuick<ElfTypes>::Start() { |
| 171 | builder_->Start(); |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 172 | if (compiler_options_->GetGenerateBuildId()) { |
| 173 | builder_->WriteBuildIdSection(); |
| 174 | } |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 175 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 176 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 177 | template <typename ElfTypes> |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 178 | void ElfWriterQuick<ElfTypes>::PrepareDynamicSection(size_t rodata_size, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 179 | size_t text_size, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 180 | size_t bss_size, |
| 181 | size_t bss_roots_offset) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 182 | DCHECK_EQ(rodata_size_, 0u); |
| 183 | rodata_size_ = rodata_size; |
| 184 | DCHECK_EQ(text_size_, 0u); |
| 185 | text_size_ = text_size; |
| 186 | DCHECK_EQ(bss_size_, 0u); |
| 187 | bss_size_ = bss_size; |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 188 | builder_->PrepareDynamicSection(elf_file_->GetPath(), |
| 189 | rodata_size_, |
| 190 | text_size_, |
| 191 | bss_size_, |
| 192 | bss_roots_offset); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 196 | OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() { |
| 197 | auto* rodata = builder_->GetRoData(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 198 | rodata->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 199 | return rodata; |
| 200 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 201 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 202 | template <typename ElfTypes> |
| 203 | void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) { |
| 204 | CHECK_EQ(builder_->GetRoData(), rodata); |
| 205 | builder_->GetRoData()->End(); |
| 206 | } |
| 207 | |
| 208 | template <typename ElfTypes> |
| 209 | OutputStream* ElfWriterQuick<ElfTypes>::StartText() { |
| 210 | auto* text = builder_->GetText(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 211 | text->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 212 | return text; |
| 213 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 214 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 215 | template <typename ElfTypes> |
| 216 | void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) { |
| 217 | CHECK_EQ(builder_->GetText(), text); |
| 218 | builder_->GetText()->End(); |
| 219 | } |
| 220 | |
| 221 | template <typename ElfTypes> |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame] | 222 | void ElfWriterQuick<ElfTypes>::WriteDynamicSection() { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 223 | if (bss_size_ != 0u) { |
| 224 | builder_->GetBss()->WriteNoBitsSection(bss_size_); |
| 225 | } |
Douglas Leung | 316a218 | 2015-09-17 15:26:25 -0700 | [diff] [blame] | 226 | if (builder_->GetIsa() == kMips || builder_->GetIsa() == kMips64) { |
| 227 | builder_->WriteMIPSabiflagsSection(); |
| 228 | } |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 229 | builder_->WriteDynamicSection(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | template <typename ElfTypes> |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 233 | void ElfWriterQuick<ElfTypes>::PrepareDebugInfo( |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 234 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) { |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 235 | if (!method_infos.empty() && compiler_options_->GetGenerateMiniDebugInfo()) { |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 236 | // Prepare the mini-debug-info in background while we do other I/O. |
| 237 | Thread* self = Thread::Current(); |
| 238 | debug_info_task_ = std::unique_ptr<DebugInfoTask>( |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 239 | new DebugInfoTask(builder_->GetIsa(), |
| 240 | instruction_set_features_, |
| 241 | rodata_size_, |
| 242 | text_size_, |
| 243 | method_infos)); |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 244 | debug_info_thread_pool_ = std::unique_ptr<ThreadPool>( |
| 245 | new ThreadPool("Mini-debug-info writer", 1)); |
| 246 | debug_info_thread_pool_->AddTask(self, debug_info_task_.get()); |
| 247 | debug_info_thread_pool_->StartWorkers(self); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 252 | void ElfWriterQuick<ElfTypes>::WriteDebugInfo( |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 253 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) { |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 254 | if (!method_infos.empty()) { |
| 255 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 256 | // Generate all the debug information we can. |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 257 | debug::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */); |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 258 | } |
| 259 | if (compiler_options_->GetGenerateMiniDebugInfo()) { |
| 260 | // Wait for the mini-debug-info generation to finish and write it to disk. |
| 261 | Thread* self = Thread::Current(); |
| 262 | DCHECK(debug_info_thread_pool_ != nullptr); |
| 263 | debug_info_thread_pool_->Wait(self, true, false); |
| 264 | builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult()); |
| 265 | } |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 266 | } |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 270 | bool ElfWriterQuick<ElfTypes>::End() { |
| 271 | builder_->End(); |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 272 | if (compiler_options_->GetGenerateBuildId()) { |
| 273 | uint8_t build_id[ElfBuilder<ElfTypes>::kBuildIdLen]; |
| 274 | ComputeFileBuildId(&build_id); |
| 275 | builder_->WriteBuildId(build_id); |
| 276 | } |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 277 | return builder_->Good(); |
| 278 | } |
| 279 | |
| 280 | template <typename ElfTypes> |
Alexey Alexandrov | ab40c11 | 2016-09-19 09:33:49 -0700 | [diff] [blame] | 281 | void ElfWriterQuick<ElfTypes>::ComputeFileBuildId( |
| 282 | uint8_t (*build_id)[ElfBuilder<ElfTypes>::kBuildIdLen]) { |
| 283 | constexpr int kBufSize = 8192; |
| 284 | std::vector<char> buffer(kBufSize); |
| 285 | int64_t offset = 0; |
| 286 | SHA_CTX ctx; |
| 287 | SHA1_Init(&ctx); |
| 288 | while (true) { |
| 289 | int64_t bytes_read = elf_file_->Read(buffer.data(), kBufSize, offset); |
| 290 | CHECK_GE(bytes_read, 0); |
| 291 | if (bytes_read == 0) { |
| 292 | // End of file. |
| 293 | break; |
| 294 | } |
| 295 | SHA1_Update(&ctx, buffer.data(), bytes_read); |
| 296 | offset += bytes_read; |
| 297 | } |
| 298 | SHA1_Final(*build_id, &ctx); |
| 299 | } |
| 300 | |
| 301 | template <typename ElfTypes> |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 302 | OutputStream* ElfWriterQuick<ElfTypes>::GetStream() { |
| 303 | return builder_->GetStream(); |
| 304 | } |
| 305 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 306 | template <typename ElfTypes> |
| 307 | size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() { |
| 308 | return builder_->GetLoadedSize(); |
| 309 | } |
| 310 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 311 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 312 | template class ElfWriterQuick<ElfTypes32>; |
| 313 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 314 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 315 | } // namespace art |