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