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