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