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" |
| 24 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 25 | #include "compiled_method.h" |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | #include "driver/compiler_driver.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 28 | #include "driver/compiler_options.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 29 | #include "elf_builder.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 30 | #include "elf_file.h" |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 31 | #include "elf_utils.h" |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 32 | #include "elf_writer_debug.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "globals.h" |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 34 | #include "leb128.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | #include "oat.h" |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 36 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
| 39 | namespace art { |
| 40 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 41 | // .eh_frame and .debug_frame are almost identical. |
| 42 | // Except for some minor formatting differences, the main difference |
| 43 | // is that .eh_frame is allocated within the running program because |
| 44 | // it is used by C++ exception handling (which we do not use so we |
| 45 | // can choose either). C++ compilers generally tend to use .eh_frame |
| 46 | // 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] | 47 | // Let's use .debug_frame because it is easier to strip or compress. |
| 48 | constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT; |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 49 | |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 50 | // The ARM specification defines three special mapping symbols |
| 51 | // $a, $t and $d which mark ARM, Thumb and data ranges respectively. |
| 52 | // These symbols can be used by tools, for example, to pretty |
| 53 | // print instructions correctly. Objdump will use them if they |
| 54 | // exist, but it will still work well without them. |
| 55 | // However, these extra symbols take space, so let's just generate |
| 56 | // one symbol which marks the whole .text section as code. |
| 57 | constexpr bool kGenerateSingleArmMappingSymbol = true; |
| 58 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 59 | template <typename ElfTypes> |
| 60 | bool ElfWriterQuick<ElfTypes>::Create(File* elf_file, |
| 61 | OatWriter* oat_writer, |
| 62 | const std::vector<const DexFile*>& dex_files, |
| 63 | const std::string& android_root, |
| 64 | bool is_host, |
| 65 | const CompilerDriver& driver) { |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 66 | ElfWriterQuick elf_writer(driver, elf_file); |
| 67 | return elf_writer.Write(oat_writer, dex_files, android_root, is_host); |
| 68 | } |
| 69 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 70 | template <typename ElfTypes> |
| 71 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 72 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 73 | template <typename ElfTypes> |
| 74 | bool ElfWriterQuick<ElfTypes>::Write( |
| 75 | OatWriter* oat_writer, |
| 76 | const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED, |
| 77 | const std::string& android_root_unused ATTRIBUTE_UNUSED, |
| 78 | bool is_host_unused ATTRIBUTE_UNUSED) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 79 | const InstructionSet isa = compiler_driver_->GetInstructionSet(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 80 | std::unique_ptr<BufferedOutputStream> output_stream( |
| 81 | new BufferedOutputStream(new FileOutputStream(elf_file_))); |
| 82 | std::unique_ptr<ElfBuilder<ElfTypes>> builder( |
| 83 | new ElfBuilder<ElfTypes>(isa, output_stream.get())); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 84 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 85 | builder->Start(); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 86 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 87 | auto* rodata = builder->GetRoData(); |
| 88 | auto* text = builder->GetText(); |
| 89 | auto* bss = builder->GetBss(); |
| 90 | |
| 91 | rodata->Start(); |
| 92 | if (!oat_writer->WriteRodata(rodata)) { |
| 93 | return false; |
| 94 | } |
| 95 | rodata->End(); |
| 96 | |
| 97 | text->Start(); |
| 98 | if (!oat_writer->WriteCode(text)) { |
| 99 | return false; |
| 100 | } |
| 101 | text->End(); |
| 102 | |
| 103 | if (oat_writer->GetBssSize() != 0) { |
| 104 | bss->Start(); |
| 105 | bss->SetSize(oat_writer->GetBssSize()); |
| 106 | bss->End(); |
| 107 | } |
| 108 | |
| 109 | builder->WriteDynamicSection(elf_file_->GetPath()); |
| 110 | |
| 111 | if (compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) { |
| 112 | const auto& method_infos = oat_writer->GetMethodDebugInfo(); |
| 113 | if (!method_infos.empty()) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 114 | // Add methods to .symtab. |
| 115 | WriteDebugSymbols(builder.get(), oat_writer); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 116 | // Generate CFI (stack unwinding information). |
| 117 | dwarf::WriteCFISection(builder.get(), method_infos, kCFIFormat); |
| 118 | // Write DWARF .debug_* sections. |
| 119 | dwarf::WriteDebugSections(builder.get(), method_infos); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 123 | // Add relocation section for .text. |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 124 | if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) { |
| 125 | // Note that ElfWriter::Fixup will be called regardless and therefore |
| 126 | // we need to include oat_patches for debug sections unconditionally. |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 127 | builder->WritePatches(".text.oat_patches", &oat_writer->GetAbsolutePatchLocations()); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 128 | } |
| 129 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 130 | builder->End(); |
| 131 | |
| 132 | return builder->Good() && output_stream->Flush(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 133 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 134 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 135 | template <typename ElfTypes> |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 136 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 137 | const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo(); |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 138 | bool generated_mapping_symbol = false; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 139 | auto* strtab = builder->GetStrTab(); |
| 140 | auto* symtab = builder->GetSymTab(); |
| 141 | |
| 142 | if (method_info.empty()) { |
| 143 | return; |
| 144 | } |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 145 | |
| 146 | // Find all addresses (low_pc) which contain deduped methods. |
| 147 | // The first instance of method is not marked deduped_, but the rest is. |
| 148 | std::unordered_set<uint32_t> deduped_addresses; |
| 149 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
| 150 | if (it->deduped_) { |
| 151 | deduped_addresses.insert(it->low_pc_); |
| 152 | } |
| 153 | } |
| 154 | |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 155 | strtab->Start(); |
| 156 | strtab->Write(""); // strtab should start with empty string. |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 157 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
David Srbecky | 6d73c9d | 2015-05-01 15:00:40 +0100 | [diff] [blame] | 158 | if (it->deduped_) { |
| 159 | continue; // Add symbol only for the first instance. |
| 160 | } |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 161 | std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 162 | if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) { |
| 163 | name += " [DEDUPED]"; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 164 | } |
| 165 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 166 | uint32_t low_pc = it->low_pc_; |
| 167 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 168 | low_pc += it->compiled_method_->CodeDelta(); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 169 | symtab->Add(strtab->Write(name), builder->GetText(), low_pc, |
| 170 | true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 171 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 172 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 173 | // instructions, so that disassembler tools can correctly disassemble. |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 174 | // Note that even if we generate just a single mapping symbol, ARM's Streamline |
| 175 | // requires it to match function symbol. Just address 0 does not work. |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 176 | if (it->compiled_method_->GetInstructionSet() == kThumb2) { |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 177 | if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 178 | symtab->Add(strtab->Write("$t"), builder->GetText(), it->low_pc_ & ~1, |
| 179 | true, 0, STB_LOCAL, STT_NOTYPE); |
David Srbecky | 388d286 | 2015-05-21 19:11:18 +0100 | [diff] [blame] | 180 | generated_mapping_symbol = true; |
| 181 | } |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 182 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 183 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame^] | 184 | strtab->End(); |
| 185 | |
| 186 | // Symbols are buffered and written after names (because they are smaller). |
| 187 | // We could also do two passes in this function to avoid the buffering. |
| 188 | symtab->Start(); |
| 189 | symtab->Write(); |
| 190 | symtab->End(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 193 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 194 | template class ElfWriterQuick<ElfTypes32>; |
| 195 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 196 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 197 | } // namespace art |