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 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include "base/logging.h" |
| 23 | #include "base/unix_file/fd_file.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 | 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 "oat.h" |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 35 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "utils.h" |
| 37 | |
| 38 | namespace art { |
| 39 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 40 | template <typename ElfTypes> |
| 41 | bool ElfWriterQuick<ElfTypes>::Create(File* elf_file, |
| 42 | OatWriter* oat_writer, |
| 43 | const std::vector<const DexFile*>& dex_files, |
| 44 | const std::string& android_root, |
| 45 | bool is_host, |
| 46 | const CompilerDriver& driver) { |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 47 | ElfWriterQuick elf_writer(driver, elf_file); |
| 48 | return elf_writer.Write(oat_writer, dex_files, android_root, is_host); |
| 49 | } |
| 50 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 51 | template <typename ElfTypes> |
| 52 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 53 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 54 | // Encode patch locations in .oat_patches format. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 55 | template <typename ElfTypes> |
| 56 | void ElfWriterQuick<ElfTypes>::EncodeOatPatches( |
| 57 | const OatWriter::PatchLocationsMap& sections, |
| 58 | std::vector<uint8_t>* buffer) { |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 59 | for (const auto& section : sections) { |
| 60 | const std::string& name = section.first; |
| 61 | std::vector<uintptr_t>* locations = section.second.get(); |
| 62 | DCHECK(!name.empty()); |
| 63 | std::sort(locations->begin(), locations->end()); |
| 64 | // Reserve buffer space - guess 2 bytes per ULEB128. |
| 65 | buffer->reserve(buffer->size() + name.size() + locations->size() * 2); |
| 66 | // Write null-terminated section name. |
| 67 | const uint8_t* name_data = reinterpret_cast<const uint8_t*>(name.c_str()); |
| 68 | buffer->insert(buffer->end(), name_data, name_data + name.size() + 1); |
| 69 | // Write placeholder for data length. |
| 70 | size_t length_pos = buffer->size(); |
| 71 | EncodeUnsignedLeb128(buffer, UINT32_MAX); |
| 72 | // Write LEB128 encoded list of advances (deltas between consequtive addresses). |
| 73 | size_t data_pos = buffer->size(); |
| 74 | uintptr_t address = 0; // relative to start of section. |
| 75 | for (uintptr_t location : *locations) { |
| 76 | DCHECK_LT(location - address, UINT32_MAX) << "Large gap between patch locations"; |
| 77 | EncodeUnsignedLeb128(buffer, location - address); |
| 78 | address = location; |
| 79 | } |
| 80 | // Update length. |
| 81 | UpdateUnsignedLeb128(buffer->data() + length_pos, buffer->size() - data_pos); |
| 82 | } |
| 83 | buffer->push_back(0); // End of sections. |
| 84 | } |
| 85 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 86 | class RodataWriter FINAL : public CodeOutput { |
| 87 | public: |
| 88 | explicit RodataWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {} |
| 89 | |
| 90 | bool Write(OutputStream* out) OVERRIDE { |
| 91 | return oat_writer_->WriteRodata(out); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 92 | } |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 93 | |
| 94 | private: |
| 95 | OatWriter* oat_writer_; |
| 96 | }; |
| 97 | |
| 98 | class TextWriter FINAL : public CodeOutput { |
| 99 | public: |
| 100 | explicit TextWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {} |
| 101 | |
| 102 | bool Write(OutputStream* out) OVERRIDE { |
| 103 | return oat_writer_->WriteCode(out); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | OatWriter* oat_writer_; |
| 108 | }; |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 109 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 110 | template <typename ElfTypes> |
| 111 | bool ElfWriterQuick<ElfTypes>::Write( |
| 112 | OatWriter* oat_writer, |
| 113 | const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED, |
| 114 | const std::string& android_root_unused ATTRIBUTE_UNUSED, |
| 115 | bool is_host_unused ATTRIBUTE_UNUSED) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 116 | const InstructionSet isa = compiler_driver_->GetInstructionSet(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 117 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 118 | // Setup the builder with the main OAT sections (.rodata .text .bss). |
| 119 | const size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset(); |
| 120 | const size_t text_size = oat_writer->GetSize() - rodata_size; |
| 121 | const size_t bss_size = oat_writer->GetBssSize(); |
| 122 | RodataWriter rodata_writer(oat_writer); |
| 123 | TextWriter text_writer(oat_writer); |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 124 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>( |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 125 | isa, rodata_size, &rodata_writer, text_size, &text_writer, bss_size)); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 126 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 127 | // Add debug sections. |
| 128 | // They are stack allocated here (in the same scope as the builder), |
| 129 | // but they are registred with the builder only if they are used. |
| 130 | using RawSection = typename ElfBuilder<ElfTypes>::RawSection; |
| 131 | const int pointer_size = GetInstructionSetPointerSize(isa); |
| 132 | const auto* text = builder->GetText(); |
| 133 | constexpr bool absolute = false; // patch to make absolute addresses. |
| 134 | constexpr bool relative = true; // patch to make relative addresses. |
| 135 | const bool is64bit = Is64BitInstructionSet(isa); |
| 136 | RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, |
| 137 | nullptr, 0, pointer_size, 0, text, relative, is64bit); |
| 138 | RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, |
| 139 | nullptr, 0, 4, 0); |
| 140 | RawSection debug_info(".debug_info", SHT_PROGBITS, 0, |
| 141 | nullptr, 0, 1, 0, text, absolute, false /* 32bit */); |
| 142 | RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, |
| 143 | nullptr, 0, 1, 0); |
| 144 | RawSection debug_str(".debug_str", SHT_PROGBITS, 0, |
| 145 | nullptr, 0, 1, 0); |
| 146 | RawSection debug_line(".debug_line", SHT_PROGBITS, 0, |
| 147 | nullptr, 0, 1, 0, text, absolute, false /* 32bit */); |
| 148 | if (!oat_writer->GetMethodDebugInfo().empty()) { |
| 149 | if (compiler_driver_->GetCompilerOptions().GetIncludeCFI()) { |
| 150 | dwarf::WriteEhFrame( |
| 151 | compiler_driver_, oat_writer, dwarf::DW_EH_PE_pcrel, |
| 152 | eh_frame.GetBuffer(), eh_frame.GetPatchLocations(), |
| 153 | eh_frame_hdr.GetBuffer()); |
| 154 | builder->RegisterSection(&eh_frame); |
| 155 | builder->RegisterSection(&eh_frame_hdr); |
| 156 | } |
| 157 | if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
| 158 | // Add methods to .symtab. |
| 159 | WriteDebugSymbols(builder.get(), oat_writer); |
| 160 | // Generate DWARF .debug_* sections. |
| 161 | dwarf::WriteDebugSections( |
| 162 | compiler_driver_, oat_writer, |
| 163 | debug_info.GetBuffer(), debug_info.GetPatchLocations(), |
| 164 | debug_abbrev.GetBuffer(), |
| 165 | debug_str.GetBuffer(), |
| 166 | debug_line.GetBuffer(), debug_line.GetPatchLocations()); |
| 167 | builder->RegisterSection(&debug_info); |
| 168 | builder->RegisterSection(&debug_abbrev); |
| 169 | builder->RegisterSection(&debug_str); |
| 170 | builder->RegisterSection(&debug_line); |
| 171 | *oat_writer->GetAbsolutePatchLocationsFor(".debug_info") = |
| 172 | *debug_info.GetPatchLocations(); |
| 173 | *oat_writer->GetAbsolutePatchLocationsFor(".debug_line") = |
| 174 | *debug_line.GetPatchLocations(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Add relocation section. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 179 | RawSection oat_patches(".oat_patches", SHT_OAT_PATCH, 0, nullptr, 0, 1, 0); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 180 | if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() || |
| 181 | // ElfWriter::Fixup will be called regardless and it needs to be able |
| 182 | // to patch debug sections so we have to include patches for them. |
| 183 | compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 184 | EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer()); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 185 | builder->RegisterSection(&oat_patches); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 186 | } |
| 187 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 188 | return builder->Write(elf_file_); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 189 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 190 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 191 | template <typename ElfTypes> |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 192 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 193 | const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo(); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 194 | |
| 195 | // Find all addresses (low_pc) which contain deduped methods. |
| 196 | // The first instance of method is not marked deduped_, but the rest is. |
| 197 | std::unordered_set<uint32_t> deduped_addresses; |
| 198 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
| 199 | if (it->deduped_) { |
| 200 | deduped_addresses.insert(it->low_pc_); |
| 201 | } |
| 202 | } |
| 203 | |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 204 | auto* symtab = builder->GetSymtab(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 205 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 206 | std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 207 | if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) { |
| 208 | name += " [DEDUPED]"; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 209 | } |
| 210 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 211 | uint32_t low_pc = it->low_pc_; |
| 212 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 213 | low_pc += it->compiled_method_->CodeDelta(); |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 214 | symtab->AddSymbol(name, builder->GetText(), low_pc, |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 215 | true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 216 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 217 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 218 | // instructions, so that disassembler tools can correctly disassemble. |
| 219 | if (it->compiled_method_->GetInstructionSet() == kThumb2) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame^] | 220 | symtab->AddSymbol("$t", builder->GetText(), it->low_pc_ & ~1, true, |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 221 | 0, STB_LOCAL, STT_NOTYPE); |
| 222 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 223 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 226 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 227 | template class ElfWriterQuick<ElfTypes32>; |
| 228 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 229 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 230 | } // namespace art |