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> |
| 20 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | #include "base/logging.h" |
| 22 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 23 | #include "buffered_output_stream.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 24 | #include "compiled_method.h" |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include "driver/compiler_driver.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 27 | #include "driver/compiler_options.h" |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 28 | #include "elf_builder.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 29 | #include "elf_file.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 | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 32 | #include "file_output_stream.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 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 41 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 42 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 43 | typename Elf_Phdr, typename Elf_Shdr> |
| 44 | bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 45 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Create(File* elf_file, |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 46 | OatWriter* oat_writer, |
| 47 | const std::vector<const DexFile*>& dex_files, |
| 48 | const std::string& android_root, |
| 49 | bool is_host, |
| 50 | const CompilerDriver& driver) { |
| 51 | ElfWriterQuick elf_writer(driver, elf_file); |
| 52 | return elf_writer.Write(oat_writer, dex_files, android_root, is_host); |
| 53 | } |
| 54 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 55 | class OatWriterWrapper FINAL : public CodeOutput { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 56 | public: |
| 57 | explicit OatWriterWrapper(OatWriter* oat_writer) : oat_writer_(oat_writer) {} |
| 58 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 59 | void SetCodeOffset(size_t offset) { |
| 60 | oat_writer_->SetOatDataOffset(offset); |
| 61 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 62 | bool Write(OutputStream* out) OVERRIDE { |
| 63 | return oat_writer_->Write(out); |
| 64 | } |
| 65 | private: |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 66 | OatWriter* const oat_writer_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 70 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 71 | typename Elf_Phdr, typename Elf_Shdr> |
| 72 | static void WriteDebugSymbols(const CompilerDriver* compiler_driver, |
| 73 | ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 74 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder, |
| 75 | OatWriter* oat_writer); |
| 76 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 77 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 78 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 79 | typename Elf_Phdr, typename Elf_Shdr> |
| 80 | bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 81 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Write(OatWriter* oat_writer, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 82 | const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED, |
| 83 | const std::string& android_root_unused ATTRIBUTE_UNUSED, |
| 84 | bool is_host_unused ATTRIBUTE_UNUSED) { |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 85 | constexpr bool debug = false; |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 86 | const OatHeader& oat_header = oat_writer->GetOatHeader(); |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 87 | Elf_Word oat_data_size = oat_header.GetExecutableOffset(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 88 | uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 89 | uint32_t oat_bss_size = oat_writer->GetBssSize(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 90 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 91 | OatWriterWrapper wrapper(oat_writer); |
| 92 | |
| 93 | std::unique_ptr<ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 94 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr> > builder( |
| 95 | new ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 96 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>( |
| 97 | &wrapper, |
| 98 | elf_file_, |
| 99 | compiler_driver_->GetInstructionSet(), |
| 100 | 0, |
| 101 | oat_data_size, |
| 102 | oat_data_size, |
| 103 | oat_exec_size, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 104 | RoundUp(oat_data_size + oat_exec_size, kPageSize), |
| 105 | oat_bss_size, |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 106 | compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols(), |
| 107 | debug)); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 108 | |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 109 | if (!builder->Init()) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 113 | if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols() && |
| 114 | !oat_writer->GetMethodDebugInfo().empty()) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 115 | WriteDebugSymbols(compiler_driver_, builder.get(), oat_writer); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 118 | if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 119 | ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> oat_patches( |
| 120 | ".oat_patches", SHT_OAT_PATCH, 0, NULL, 0, sizeof(uintptr_t), sizeof(uintptr_t)); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 121 | const std::vector<uintptr_t>& locations = oat_writer->GetAbsolutePatchLocations(); |
| 122 | const uint8_t* begin = reinterpret_cast<const uint8_t*>(&locations[0]); |
| 123 | const uint8_t* end = begin + locations.size() * sizeof(locations[0]); |
| 124 | oat_patches.GetBuffer()->assign(begin, end); |
| 125 | if (debug) { |
| 126 | LOG(INFO) << "Prepared .oat_patches for " << locations.size() << " patches."; |
| 127 | } |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 128 | builder->RegisterRawSection(oat_patches); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 131 | return builder->Write(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 132 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 133 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 134 | template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr, |
| 135 | typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr, |
| 136 | typename Elf_Phdr, typename Elf_Shdr> |
Andreas Gampe | 8683038 | 2014-12-12 21:41:29 -0800 | [diff] [blame] | 137 | // Do not inline to avoid Clang stack frame problems. b/18738594 |
| 138 | NO_INLINE |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 139 | static void WriteDebugSymbols(const CompilerDriver* compiler_driver, |
| 140 | ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, |
| 141 | Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder, |
| 142 | OatWriter* oat_writer) { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 143 | // Iterate over the compiled methods. |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 144 | const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo(); |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 145 | ElfSymtabBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Sym, Elf_Shdr>* symtab = |
| 146 | builder->GetSymtabBuilder(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 147 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 148 | std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true); |
| 149 | if (it->deduped_) { |
| 150 | // TODO We should place the DEDUPED tag on the first instance of a deduplicated symbol |
| 151 | // so that it will show up in a debuggerd crash report. |
| 152 | name += " [ DEDUPED ]"; |
| 153 | } |
| 154 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 155 | uint32_t low_pc = it->low_pc_; |
| 156 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 157 | low_pc += it->compiled_method_->CodeDelta(); |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 158 | symtab->AddSymbol(name, &builder->GetTextBuilder(), low_pc, |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 159 | true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 160 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 161 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 162 | // instructions, so that disassembler tools can correctly disassemble. |
| 163 | if (it->compiled_method_->GetInstructionSet() == kThumb2) { |
| 164 | symtab->AddSymbol("$t", &builder->GetTextBuilder(), it->low_pc_ & ~1, true, |
| 165 | 0, STB_LOCAL, STT_NOTYPE); |
| 166 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 169 | typedef ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> Section; |
| 170 | Section eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0); |
| 171 | Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 172 | Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 173 | Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 174 | Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 175 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 176 | dwarf::WriteDebugSections(compiler_driver, |
| 177 | oat_writer, |
| 178 | builder->GetTextBuilder().GetSection()->sh_addr, |
| 179 | eh_frame.GetBuffer(), |
| 180 | debug_info.GetBuffer(), |
| 181 | debug_abbrev.GetBuffer(), |
| 182 | debug_str.GetBuffer(), |
| 183 | debug_line.GetBuffer()); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 184 | |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 185 | builder->RegisterRawSection(eh_frame); |
| 186 | builder->RegisterRawSection(debug_info); |
| 187 | builder->RegisterRawSection(debug_abbrev); |
| 188 | builder->RegisterRawSection(debug_str); |
| 189 | builder->RegisterRawSection(debug_line); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 192 | // Explicit instantiations |
| 193 | template class ElfWriterQuick<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn, |
| 194 | Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>; |
| 195 | template class ElfWriterQuick<Elf64_Word, Elf64_Sword, Elf64_Addr, Elf64_Dyn, |
| 196 | Elf64_Sym, Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>; |
| 197 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 198 | } // namespace art |