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 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 19 | #include <unordered_map> |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 20 | #include <unordered_set> |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 21 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 22 | #include "base/casts.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 23 | #include "base/logging.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 24 | #include "base/stl_util.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 25 | #include "compiled_method.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 26 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 27 | #include "debug/method_debug_info.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 28 | #include "driver/compiler_options.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 29 | #include "elf.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 30 | #include "elf_builder.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 31 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "globals.h" |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 33 | #include "leb128.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 34 | #include "linker/buffered_output_stream.h" |
| 35 | #include "linker/file_output_stream.h" |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 36 | #include "thread-inl.h" |
| 37 | #include "thread_pool.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 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. |
David Srbecky | aaf143d | 2015-05-21 14:03:48 +0100 | [diff] [blame] | 48 | // Let's use .debug_frame because it is easier to strip or compress. |
| 49 | constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 50 | |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 51 | class DebugInfoTask : public Task { |
| 52 | public: |
| 53 | DebugInfoTask(InstructionSet isa, |
| 54 | size_t rodata_section_size, |
| 55 | size_t text_section_size, |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 56 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 57 | : isa_(isa), |
| 58 | rodata_section_size_(rodata_section_size), |
| 59 | text_section_size_(text_section_size), |
| 60 | method_infos_(method_infos) { |
| 61 | } |
| 62 | |
| 63 | void Run(Thread*) { |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 64 | result_ = debug::MakeMiniDebugInfo(isa_, |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 65 | rodata_section_size_, |
| 66 | text_section_size_, |
| 67 | method_infos_); |
| 68 | } |
| 69 | |
| 70 | std::vector<uint8_t>* GetResult() { |
| 71 | return &result_; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | InstructionSet isa_; |
| 76 | size_t rodata_section_size_; |
| 77 | size_t text_section_size_; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 78 | const ArrayRef<const debug::MethodDebugInfo>& method_infos_; |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 79 | std::vector<uint8_t> result_; |
| 80 | }; |
| 81 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 82 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 83 | class ElfWriterQuick FINAL : public ElfWriter { |
| 84 | public: |
| 85 | ElfWriterQuick(InstructionSet instruction_set, |
| 86 | const CompilerOptions* compiler_options, |
| 87 | File* elf_file); |
| 88 | ~ElfWriterQuick(); |
| 89 | |
| 90 | void Start() OVERRIDE; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 91 | void SetLoadedSectionSizes(size_t rodata_size, size_t text_size, size_t bss_size) OVERRIDE; |
| 92 | void PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 93 | OutputStream* StartRoData() OVERRIDE; |
| 94 | void EndRoData(OutputStream* rodata) OVERRIDE; |
| 95 | OutputStream* StartText() OVERRIDE; |
| 96 | void EndText(OutputStream* text) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 97 | void WriteDynamicSection() OVERRIDE; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 98 | void WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) OVERRIDE; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 99 | void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE; |
| 100 | bool End() OVERRIDE; |
| 101 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 102 | virtual OutputStream* GetStream() OVERRIDE; |
| 103 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 104 | size_t GetLoadedSize() OVERRIDE; |
| 105 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 106 | static void EncodeOatPatches(const std::vector<uintptr_t>& locations, |
| 107 | std::vector<uint8_t>* buffer); |
| 108 | |
| 109 | private: |
| 110 | const CompilerOptions* const compiler_options_; |
| 111 | File* const elf_file_; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 112 | size_t rodata_size_; |
| 113 | size_t text_size_; |
| 114 | size_t bss_size_; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 115 | std::unique_ptr<BufferedOutputStream> output_stream_; |
| 116 | std::unique_ptr<ElfBuilder<ElfTypes>> builder_; |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 117 | std::unique_ptr<DebugInfoTask> debug_info_task_; |
| 118 | std::unique_ptr<ThreadPool> debug_info_thread_pool_; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 119 | |
| 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick); |
| 121 | }; |
| 122 | |
| 123 | std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set, |
| 124 | const CompilerOptions* compiler_options, |
| 125 | File* elf_file) { |
| 126 | if (Is64BitInstructionSet(instruction_set)) { |
| 127 | return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file); |
| 128 | } else { |
| 129 | return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file); |
| 130 | } |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 131 | } |
| 132 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 133 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 134 | ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set, |
| 135 | const CompilerOptions* compiler_options, |
| 136 | File* elf_file) |
| 137 | : ElfWriter(), |
| 138 | compiler_options_(compiler_options), |
| 139 | elf_file_(elf_file), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 140 | rodata_size_(0u), |
| 141 | text_size_(0u), |
| 142 | bss_size_(0u), |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 143 | output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))), |
| 144 | builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {} |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 145 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 146 | template <typename ElfTypes> |
| 147 | ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {} |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 148 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 149 | template <typename ElfTypes> |
| 150 | void ElfWriterQuick<ElfTypes>::Start() { |
| 151 | builder_->Start(); |
| 152 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 153 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 154 | template <typename ElfTypes> |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 155 | void ElfWriterQuick<ElfTypes>::SetLoadedSectionSizes(size_t rodata_size, |
| 156 | size_t text_size, |
| 157 | size_t bss_size) { |
| 158 | DCHECK_EQ(rodata_size_, 0u); |
| 159 | rodata_size_ = rodata_size; |
| 160 | DCHECK_EQ(text_size_, 0u); |
| 161 | text_size_ = text_size; |
| 162 | DCHECK_EQ(bss_size_, 0u); |
| 163 | bss_size_ = bss_size; |
| 164 | builder_->PrepareDynamicSection(elf_file_->GetPath(), rodata_size_, text_size_, bss_size_); |
| 165 | } |
| 166 | |
| 167 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 168 | OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() { |
| 169 | auto* rodata = builder_->GetRoData(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 170 | rodata->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 171 | return rodata; |
| 172 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 173 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 174 | template <typename ElfTypes> |
| 175 | void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) { |
| 176 | CHECK_EQ(builder_->GetRoData(), rodata); |
| 177 | builder_->GetRoData()->End(); |
| 178 | } |
| 179 | |
| 180 | template <typename ElfTypes> |
| 181 | OutputStream* ElfWriterQuick<ElfTypes>::StartText() { |
| 182 | auto* text = builder_->GetText(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 183 | text->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 184 | return text; |
| 185 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 186 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 187 | template <typename ElfTypes> |
| 188 | void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) { |
| 189 | CHECK_EQ(builder_->GetText(), text); |
| 190 | builder_->GetText()->End(); |
| 191 | } |
| 192 | |
| 193 | template <typename ElfTypes> |
Vladimir Marko | 45724f9 | 2016-02-17 17:46:10 +0000 | [diff] [blame] | 194 | void ElfWriterQuick<ElfTypes>::WriteDynamicSection() { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 195 | if (bss_size_ != 0u) { |
| 196 | builder_->GetBss()->WriteNoBitsSection(bss_size_); |
| 197 | } |
| 198 | builder_->WriteDynamicSection(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | template <typename ElfTypes> |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 202 | void ElfWriterQuick<ElfTypes>::PrepareDebugInfo( |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 203 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) { |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 204 | if (!method_infos.empty() && compiler_options_->GetGenerateMiniDebugInfo()) { |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 205 | // Prepare the mini-debug-info in background while we do other I/O. |
| 206 | Thread* self = Thread::Current(); |
| 207 | debug_info_task_ = std::unique_ptr<DebugInfoTask>( |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 208 | new DebugInfoTask(builder_->GetIsa(), rodata_size_, text_size_, method_infos)); |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 209 | debug_info_thread_pool_ = std::unique_ptr<ThreadPool>( |
| 210 | new ThreadPool("Mini-debug-info writer", 1)); |
| 211 | debug_info_thread_pool_->AddTask(self, debug_info_task_.get()); |
| 212 | debug_info_thread_pool_->StartWorkers(self); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 217 | void ElfWriterQuick<ElfTypes>::WriteDebugInfo( |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 218 | const ArrayRef<const debug::MethodDebugInfo>& method_infos) { |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 219 | if (!method_infos.empty()) { |
| 220 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 221 | // Generate all the debug information we can. |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 222 | debug::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat, true /* write_oat_patches */); |
David Srbecky | 370339c | 2016-02-05 11:42:23 +0000 | [diff] [blame] | 223 | } |
| 224 | if (compiler_options_->GetGenerateMiniDebugInfo()) { |
| 225 | // Wait for the mini-debug-info generation to finish and write it to disk. |
| 226 | Thread* self = Thread::Current(); |
| 227 | DCHECK(debug_info_thread_pool_ != nullptr); |
| 228 | debug_info_thread_pool_->Wait(self, true, false); |
| 229 | builder_->WriteSection(".gnu_debugdata", debug_info_task_->GetResult()); |
| 230 | } |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 231 | } |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | template <typename ElfTypes> |
| 235 | void ElfWriterQuick<ElfTypes>::WritePatchLocations( |
| 236 | const ArrayRef<const uintptr_t>& patch_locations) { |
| 237 | // Add relocation section for .text. |
| 238 | if (compiler_options_->GetIncludePatchInformation()) { |
| 239 | // Note that ElfWriter::Fixup will be called regardless and therefore |
| 240 | // we need to include oat_patches for debug sections unconditionally. |
| 241 | builder_->WritePatches(".text.oat_patches", patch_locations); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | template <typename ElfTypes> |
| 246 | bool ElfWriterQuick<ElfTypes>::End() { |
| 247 | builder_->End(); |
| 248 | |
| 249 | return builder_->Good(); |
| 250 | } |
| 251 | |
| 252 | template <typename ElfTypes> |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 253 | OutputStream* ElfWriterQuick<ElfTypes>::GetStream() { |
| 254 | return builder_->GetStream(); |
| 255 | } |
| 256 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 257 | template <typename ElfTypes> |
| 258 | size_t ElfWriterQuick<ElfTypes>::GetLoadedSize() { |
| 259 | return builder_->GetLoadedSize(); |
| 260 | } |
| 261 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 262 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 263 | template class ElfWriterQuick<ElfTypes32>; |
| 264 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 265 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 266 | } // namespace art |