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" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 26 | #include "driver/compiler_options.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 27 | #include "dwarf/method_debug_info.h" |
| 28 | #include "elf.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 29 | #include "elf_builder.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 30 | #include "elf_utils.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 31 | #include "elf_writer_debug.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" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 38 | // .eh_frame and .debug_frame are almost identical. |
| 39 | // Except for some minor formatting differences, the main difference |
| 40 | // is that .eh_frame is allocated within the running program because |
| 41 | // it is used by C++ exception handling (which we do not use so we |
| 42 | // can choose either). C++ compilers generally tend to use .eh_frame |
| 43 | // 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] | 44 | // Let's use .debug_frame because it is easier to strip or compress. |
| 45 | constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 46 | |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 47 | // The ARM specification defines three special mapping symbols |
| 48 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 49 | // These symbols can be used by tools, for example, to pretty |
| 50 | // print instructions correctly. Objdump will use them if they |
| 51 | // exist, but it will still work well without them. |
| 52 | // However, these extra symbols take space, so let's just generate |
| 53 | // one symbol which marks the whole .text section as code. |
| 54 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 55 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 56 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 57 | class ElfWriterQuick FINAL : public ElfWriter { |
| 58 | public: |
| 59 | ElfWriterQuick(InstructionSet instruction_set, |
| 60 | const CompilerOptions* compiler_options, |
| 61 | File* elf_file); |
| 62 | ~ElfWriterQuick(); |
| 63 | |
| 64 | void Start() OVERRIDE; |
| 65 | OutputStream* StartRoData() OVERRIDE; |
| 66 | void EndRoData(OutputStream* rodata) OVERRIDE; |
| 67 | OutputStream* StartText() OVERRIDE; |
| 68 | void EndText(OutputStream* text) OVERRIDE; |
| 69 | void SetBssSize(size_t bss_size) OVERRIDE; |
| 70 | void WriteDynamicSection() OVERRIDE; |
| 71 | void WriteDebugInfo(const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE; |
| 72 | void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE; |
| 73 | bool End() OVERRIDE; |
| 74 | |
| 75 | static void EncodeOatPatches(const std::vector<uintptr_t>& locations, |
| 76 | std::vector<uint8_t>* buffer); |
| 77 | |
| 78 | private: |
| 79 | const CompilerOptions* const compiler_options_; |
| 80 | File* const elf_file_; |
| 81 | std::unique_ptr<BufferedOutputStream> output_stream_; |
| 82 | std::unique_ptr<ElfBuilder<ElfTypes>> builder_; |
| 83 | |
| 84 | DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick); |
| 85 | }; |
| 86 | |
| 87 | std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set, |
| 88 | const CompilerOptions* compiler_options, |
| 89 | File* elf_file) { |
| 90 | if (Is64BitInstructionSet(instruction_set)) { |
| 91 | return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file); |
| 92 | } else { |
| 93 | return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file); |
| 94 | } |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 95 | } |
| 96 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 97 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 98 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 99 | const ArrayRef<const dwarf::MethodDebugInfo>& method_infos); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 100 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 101 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 102 | ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set, |
| 103 | const CompilerOptions* compiler_options, |
| 104 | File* elf_file) |
| 105 | : ElfWriter(), |
| 106 | compiler_options_(compiler_options), |
| 107 | elf_file_(elf_file), |
| 108 | output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))), |
| 109 | builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {} |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 110 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 111 | template <typename ElfTypes> |
| 112 | ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {} |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 113 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 114 | template <typename ElfTypes> |
| 115 | void ElfWriterQuick<ElfTypes>::Start() { |
| 116 | builder_->Start(); |
| 117 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 118 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 119 | template <typename ElfTypes> |
| 120 | OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() { |
| 121 | auto* rodata = builder_->GetRoData(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 122 | rodata->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 123 | return rodata; |
| 124 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 125 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 126 | template <typename ElfTypes> |
| 127 | void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) { |
| 128 | CHECK_EQ(builder_->GetRoData(), rodata); |
| 129 | builder_->GetRoData()->End(); |
| 130 | } |
| 131 | |
| 132 | template <typename ElfTypes> |
| 133 | OutputStream* ElfWriterQuick<ElfTypes>::StartText() { |
| 134 | auto* text = builder_->GetText(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 135 | text->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 136 | return text; |
| 137 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 138 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 139 | template <typename ElfTypes> |
| 140 | void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) { |
| 141 | CHECK_EQ(builder_->GetText(), text); |
| 142 | builder_->GetText()->End(); |
| 143 | } |
| 144 | |
| 145 | template <typename ElfTypes> |
| 146 | void ElfWriterQuick<ElfTypes>::SetBssSize(size_t bss_size) { |
| 147 | auto* bss = builder_->GetBss(); |
| 148 | if (bss_size != 0u) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 149 | bss->Start(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 150 | bss->SetSize(bss_size); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 151 | bss->End(); |
| 152 | } |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 153 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 154 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 155 | template <typename ElfTypes> |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 156 | void ElfWriterQuick<ElfTypes>::WriteDynamicSection() { |
| 157 | builder_->WriteDynamicSection(elf_file_->GetPath()); |
| 158 | } |
| 159 | |
| 160 | template <typename ElfTypes> |
| 161 | void ElfWriterQuick<ElfTypes>::WriteDebugInfo( |
| 162 | const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) { |
| 163 | if (compiler_options_->GetGenerateDebugInfo()) { |
| 164 | if (!method_infos.empty()) { |
| 165 | // Add methods to .symtab. |
| 166 | WriteDebugSymbols(builder_.get(), method_infos); |
| 167 | // Generate CFI (stack unwinding information). |
| 168 | dwarf::WriteCFISection(builder_.get(), method_infos, kCFIFormat); |
| 169 | // Write DWARF .debug_* sections. |
| 170 | dwarf::WriteDebugSections(builder_.get(), method_infos); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | template <typename ElfTypes> |
| 176 | void ElfWriterQuick<ElfTypes>::WritePatchLocations( |
| 177 | const ArrayRef<const uintptr_t>& patch_locations) { |
| 178 | // Add relocation section for .text. |
| 179 | if (compiler_options_->GetIncludePatchInformation()) { |
| 180 | // Note that ElfWriter::Fixup will be called regardless and therefore |
| 181 | // we need to include oat_patches for debug sections unconditionally. |
| 182 | builder_->WritePatches(".text.oat_patches", patch_locations); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | template <typename ElfTypes> |
| 187 | bool ElfWriterQuick<ElfTypes>::End() { |
| 188 | builder_->End(); |
| 189 | |
| 190 | return builder_->Good(); |
| 191 | } |
| 192 | |
| 193 | template <typename ElfTypes> |
| 194 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, |
| 195 | const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) { |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 196 | bool generated_mapping_symbol = false; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 197 | auto* strtab = builder->GetStrTab(); |
| 198 | auto* symtab = builder->GetSymTab(); |
| 199 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 200 | if (method_infos.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 203 | |
| 204 | // Find all addresses (low_pc) which contain deduped methods. |
| 205 | // The first instance of method is not marked deduped_, but the rest is. |
| 206 | std::unordered_set<uint32_t> deduped_addresses; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 207 | for (const dwarf::MethodDebugInfo& info : method_infos) { |
| 208 | if (info.deduped_) { |
| 209 | deduped_addresses.insert(info.low_pc_); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 213 | strtab->Start(); |
| 214 | strtab->Write(""); // strtab should start with empty string. |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 215 | for (const dwarf::MethodDebugInfo& info : method_infos) { |
| 216 | if (info.deduped_) { |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 217 | continue; // Add symbol only for the first instance. |
| 218 | } |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 219 | std::string name = PrettyMethod(info.dex_method_index_, *info.dex_file_, true); |
| 220 | if (deduped_addresses.find(info.low_pc_) != deduped_addresses.end()) { |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 221 | name += " [DEDUPED]"; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 224 | uint32_t low_pc = info.low_pc_; |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 225 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 226 | low_pc += info.compiled_method_->CodeDelta(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 227 | symtab->Add(strtab->Write(name), builder->GetText(), low_pc, |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 228 | true, info.high_pc_ - info.low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 229 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 230 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 231 | // instructions, so that disassembler tools can correctly disassemble. |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 232 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 233 | // requires it to match function symbol. Just address 0 does not work. |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 234 | if (info.compiled_method_->GetInstructionSet() == kThumb2) { |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 235 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame^] | 236 | symtab->Add(strtab->Write("$t"), builder->GetText(), info.low_pc_ & ~1, |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 237 | true, 0, STB_LOCAL, STT_NOTYPE); |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 238 | generated_mapping_symbol = true; |
| 239 | } |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 240 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 241 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 242 | strtab->End(); |
| 243 | |
| 244 | // Symbols are buffered and written after names (because they are smaller). |
| 245 | // We could also do two passes in this function to avoid the buffering. |
| 246 | symtab->Start(); |
| 247 | symtab->Write(); |
| 248 | symtab->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 251 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 252 | template class ElfWriterQuick<ElfTypes32>; |
| 253 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 254 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 255 | } // namespace art |