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" |
Brian Carlstrom | c6dfdac | 2013-08-26 18:57:31 -0700 | [diff] [blame] | 24 | #include "buffered_output_stream.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 | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 33 | #include "file_output_stream.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "globals.h" |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 35 | #include "leb128.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "oat.h" |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 37 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 42 | template <typename ElfTypes> |
| 43 | bool ElfWriterQuick<ElfTypes>::Create(File* elf_file, |
| 44 | OatWriter* oat_writer, |
| 45 | const std::vector<const DexFile*>& dex_files, |
| 46 | const std::string& android_root, |
| 47 | bool is_host, |
| 48 | const CompilerDriver& driver) { |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 49 | ElfWriterQuick elf_writer(driver, elf_file); |
| 50 | return elf_writer.Write(oat_writer, dex_files, android_root, is_host); |
| 51 | } |
| 52 | |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 53 | class OatWriterWrapper FINAL : public CodeOutput { |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 54 | public: |
| 55 | explicit OatWriterWrapper(OatWriter* oat_writer) : oat_writer_(oat_writer) {} |
| 56 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 57 | void SetCodeOffset(size_t offset) { |
| 58 | oat_writer_->SetOatDataOffset(offset); |
| 59 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 60 | bool Write(OutputStream* out) OVERRIDE { |
| 61 | return oat_writer_->Write(out); |
| 62 | } |
| 63 | private: |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 64 | OatWriter* const oat_writer_; |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 67 | template <typename ElfTypes> |
| 68 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 69 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 70 | // Encode patch locations in .oat_patches format. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 71 | template <typename ElfTypes> |
| 72 | void ElfWriterQuick<ElfTypes>::EncodeOatPatches( |
| 73 | const OatWriter::PatchLocationsMap& sections, |
| 74 | std::vector<uint8_t>* buffer) { |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 75 | for (const auto& section : sections) { |
| 76 | const std::string& name = section.first; |
| 77 | std::vector<uintptr_t>* locations = section.second.get(); |
| 78 | DCHECK(!name.empty()); |
| 79 | std::sort(locations->begin(), locations->end()); |
| 80 | // Reserve buffer space - guess 2 bytes per ULEB128. |
| 81 | buffer->reserve(buffer->size() + name.size() + locations->size() * 2); |
| 82 | // Write null-terminated section name. |
| 83 | const uint8_t* name_data = reinterpret_cast<const uint8_t*>(name.c_str()); |
| 84 | buffer->insert(buffer->end(), name_data, name_data + name.size() + 1); |
| 85 | // Write placeholder for data length. |
| 86 | size_t length_pos = buffer->size(); |
| 87 | EncodeUnsignedLeb128(buffer, UINT32_MAX); |
| 88 | // Write LEB128 encoded list of advances (deltas between consequtive addresses). |
| 89 | size_t data_pos = buffer->size(); |
| 90 | uintptr_t address = 0; // relative to start of section. |
| 91 | for (uintptr_t location : *locations) { |
| 92 | DCHECK_LT(location - address, UINT32_MAX) << "Large gap between patch locations"; |
| 93 | EncodeUnsignedLeb128(buffer, location - address); |
| 94 | address = location; |
| 95 | } |
| 96 | // Update length. |
| 97 | UpdateUnsignedLeb128(buffer->data() + length_pos, buffer->size() - data_pos); |
| 98 | } |
| 99 | buffer->push_back(0); // End of sections. |
| 100 | } |
| 101 | |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 102 | template<typename AddressType, bool SubtractPatchLocation = false> |
| 103 | static void PatchAddresses(const std::vector<uintptr_t>* patch_locations, |
| 104 | AddressType delta, std::vector<uint8_t>* buffer) { |
| 105 | // Addresses in .debug_* sections are unaligned. |
| 106 | typedef __attribute__((__aligned__(1))) AddressType UnalignedAddressType; |
| 107 | if (patch_locations != nullptr) { |
| 108 | for (uintptr_t patch_location : *patch_locations) { |
| 109 | *reinterpret_cast<UnalignedAddressType*>(buffer->data() + patch_location) += |
| 110 | delta - (SubtractPatchLocation ? patch_location : 0); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 115 | template <typename ElfTypes> |
| 116 | bool ElfWriterQuick<ElfTypes>::Write( |
| 117 | OatWriter* oat_writer, |
| 118 | const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED, |
| 119 | const std::string& android_root_unused ATTRIBUTE_UNUSED, |
| 120 | bool is_host_unused ATTRIBUTE_UNUSED) { |
Andreas Gampe | 7927380 | 2014-08-05 20:21:05 -0700 | [diff] [blame] | 121 | constexpr bool debug = false; |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 122 | const OatHeader& oat_header = oat_writer->GetOatHeader(); |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 123 | typename ElfTypes::Word oat_data_size = oat_header.GetExecutableOffset(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 124 | uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 125 | uint32_t oat_bss_size = oat_writer->GetBssSize(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 126 | |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 127 | OatWriterWrapper wrapper(oat_writer); |
| 128 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 129 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>( |
| 130 | &wrapper, |
| 131 | elf_file_, |
| 132 | compiler_driver_->GetInstructionSet(), |
| 133 | 0, |
| 134 | oat_data_size, |
| 135 | oat_data_size, |
| 136 | oat_exec_size, |
| 137 | RoundUp(oat_data_size + oat_exec_size, kPageSize), |
| 138 | oat_bss_size, |
| 139 | compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols(), |
| 140 | debug)); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 141 | |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 142 | InstructionSet isa = compiler_driver_->GetInstructionSet(); |
| 143 | int alignment = GetInstructionSetPointerSize(isa); |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 144 | typedef ElfRawSectionBuilder<ElfTypes> RawSection; |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 145 | RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, alignment, 0); |
| 146 | RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0); |
| 147 | RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 148 | RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 149 | RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 150 | RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 151 | RawSection oat_patches(".oat_patches", SHT_OAT_PATCH, 0, NULL, 0, 1, 0); |
| 152 | |
| 153 | // Do not add to .oat_patches since we will make the addresses relative. |
| 154 | std::vector<uintptr_t> eh_frame_patches; |
| 155 | if (compiler_driver_->GetCompilerOptions().GetIncludeCFI() && |
| 156 | !oat_writer->GetMethodDebugInfo().empty()) { |
| 157 | dwarf::WriteEhFrame(compiler_driver_, oat_writer, |
| 158 | dwarf::DW_EH_PE_pcrel, |
| 159 | eh_frame.GetBuffer(), &eh_frame_patches, |
| 160 | eh_frame_hdr.GetBuffer()); |
| 161 | builder->RegisterRawSection(&eh_frame); |
| 162 | builder->RegisterRawSection(&eh_frame_hdr); |
| 163 | } |
| 164 | |
| 165 | // Must be done after .eh_frame is created since it is used in the Elf layout. |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 166 | if (!builder->Init()) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 170 | std::vector<uintptr_t>* debug_info_patches = nullptr; |
| 171 | std::vector<uintptr_t>* debug_line_patches = nullptr; |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 172 | if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols() && |
| 173 | !oat_writer->GetMethodDebugInfo().empty()) { |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 174 | // Add methods to .symtab. |
| 175 | WriteDebugSymbols(builder.get(), oat_writer); |
| 176 | // Generate DWARF .debug_* sections. |
| 177 | debug_info_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_info"); |
| 178 | debug_line_patches = oat_writer->GetAbsolutePatchLocationsFor(".debug_line"); |
| 179 | dwarf::WriteDebugSections(compiler_driver_, oat_writer, |
| 180 | debug_info.GetBuffer(), debug_info_patches, |
| 181 | debug_abbrev.GetBuffer(), |
| 182 | debug_str.GetBuffer(), |
| 183 | debug_line.GetBuffer(), debug_line_patches); |
| 184 | builder->RegisterRawSection(&debug_info); |
| 185 | builder->RegisterRawSection(&debug_abbrev); |
| 186 | builder->RegisterRawSection(&debug_str); |
| 187 | builder->RegisterRawSection(&debug_line); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 190 | if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() || |
| 191 | // ElfWriter::Fixup will be called regardless and it needs to be able |
| 192 | // to patch debug sections so we have to include patches for them. |
| 193 | compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) { |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 194 | EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer()); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 195 | builder->RegisterRawSection(&oat_patches); |
| 196 | } |
| 197 | |
| 198 | // We know where .text and .eh_frame will be located, so patch the addresses. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 199 | typename ElfTypes::Addr text_addr = builder->GetTextBuilder().GetSection()->sh_addr; |
| 200 | // TODO: Simplify once we use Elf64 - we can use ElfTypes::Addr instead of branching. |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 201 | if (Is64BitInstructionSet(compiler_driver_->GetInstructionSet())) { |
| 202 | // relative_address = (text_addr + address) - (eh_frame_addr + patch_location); |
| 203 | PatchAddresses<uint64_t, true>(&eh_frame_patches, |
| 204 | text_addr - eh_frame.GetSection()->sh_addr, eh_frame.GetBuffer()); |
| 205 | PatchAddresses<uint64_t>(debug_info_patches, text_addr, debug_info.GetBuffer()); |
| 206 | PatchAddresses<uint64_t>(debug_line_patches, text_addr, debug_line.GetBuffer()); |
| 207 | } else { |
| 208 | // relative_address = (text_addr + address) - (eh_frame_addr + patch_location); |
| 209 | PatchAddresses<uint32_t, true>(&eh_frame_patches, |
| 210 | text_addr - eh_frame.GetSection()->sh_addr, eh_frame.GetBuffer()); |
| 211 | PatchAddresses<uint32_t>(debug_info_patches, text_addr, debug_info.GetBuffer()); |
| 212 | PatchAddresses<uint32_t>(debug_line_patches, text_addr, debug_line.GetBuffer()); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Brian Carlstrom | 18a49cc | 2014-08-29 16:20:48 -0700 | [diff] [blame] | 215 | return builder->Write(); |
Brian Carlstrom | b12f347 | 2014-06-11 14:54:46 -0700 | [diff] [blame] | 216 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 217 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 218 | template <typename ElfTypes> |
Andreas Gampe | 8683038 | 2014-12-12 21:41:29 -0800 | [diff] [blame] | 219 | // Do not inline to avoid Clang stack frame problems. b/18738594 |
| 220 | NO_INLINE |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 221 | static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) { |
David Srbecky | 3b9d57a | 2015-04-10 00:22:14 +0100 | [diff] [blame] | 222 | const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo(); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 223 | |
| 224 | // Find all addresses (low_pc) which contain deduped methods. |
| 225 | // The first instance of method is not marked deduped_, but the rest is. |
| 226 | std::unordered_set<uint32_t> deduped_addresses; |
| 227 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
| 228 | if (it->deduped_) { |
| 229 | deduped_addresses.insert(it->low_pc_); |
| 230 | } |
| 231 | } |
| 232 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 233 | ElfSymtabBuilder<ElfTypes>* symtab = builder->GetSymtabBuilder(); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 234 | for (auto it = method_info.begin(); it != method_info.end(); ++it) { |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 235 | std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true); |
David Srbecky | 626a166 | 2015-04-12 13:12:26 +0100 | [diff] [blame] | 236 | if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) { |
| 237 | name += " [DEDUPED]"; |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 238 | } |
| 239 | |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 240 | uint32_t low_pc = it->low_pc_; |
| 241 | // Add in code delta, e.g., thumb bit 0 for Thumb2 code. |
| 242 | low_pc += it->compiled_method_->CodeDelta(); |
David Srbecky | 0df9e1f | 2015-04-07 19:02:58 +0100 | [diff] [blame] | 243 | symtab->AddSymbol(name, &builder->GetTextBuilder(), low_pc, |
David Srbecky | 6f71589 | 2015-03-30 14:21:42 +0100 | [diff] [blame] | 244 | true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC); |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 245 | |
Ningsheng Jian | f973455 | 2014-10-27 14:56:34 +0800 | [diff] [blame] | 246 | // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2 |
| 247 | // instructions, so that disassembler tools can correctly disassemble. |
| 248 | if (it->compiled_method_->GetInstructionSet() == kThumb2) { |
| 249 | symtab->AddSymbol("$t", &builder->GetTextBuilder(), it->low_pc_ & ~1, true, |
| 250 | 0, STB_LOCAL, STT_NOTYPE); |
| 251 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 252 | } |
Andreas Gampe | 54fc26c | 2014-09-04 21:47:42 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 255 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame^] | 256 | template class ElfWriterQuick<ElfTypes32>; |
| 257 | template class ElfWriterQuick<ElfTypes64>; |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 258 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 259 | } // namespace art |