blob: 79f99556471a77b5f8b7532573fbe1ba88c0c859 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Roubane3ea8382014-08-08 16:29:38 +070019#include <unordered_map>
David Srbecky626a1662015-04-12 13:12:26 +010020#include <unordered_set>
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070021
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "base/logging.h"
23#include "base/unix_file/fd_file.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000024#include "compiled_method.h"
David Srbecky0df9e1f2015-04-07 19:02:58 +010025#include "dex_file-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070028#include "elf_builder.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070029#include "elf_file.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000030#include "elf_utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010031#include "elf_writer_debug.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070033#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "oat.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070035#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "utils.h"
37
38namespace art {
39
David Srbecky533c2072015-04-22 12:20:22 +010040template <typename ElfTypes>
41bool 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 Carlstromb12f3472014-06-11 14:54:46 -070047 ElfWriterQuick elf_writer(driver, elf_file);
48 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
49}
50
David Srbecky533c2072015-04-22 12:20:22 +010051template <typename ElfTypes>
52static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070053
David Srbecky2f6cdb02015-04-11 00:17:53 +010054// Encode patch locations in .oat_patches format.
David Srbecky533c2072015-04-22 12:20:22 +010055template <typename ElfTypes>
56void ElfWriterQuick<ElfTypes>::EncodeOatPatches(
57 const OatWriter::PatchLocationsMap& sections,
58 std::vector<uint8_t>* buffer) {
David Srbecky2f6cdb02015-04-11 00:17:53 +010059 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 Srbeckybc90fd02015-04-22 19:40:27 +010086class 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 Srbecky527c9c72015-04-17 21:14:10 +010092 }
David Srbeckybc90fd02015-04-22 19:40:27 +010093
94 private:
95 OatWriter* oat_writer_;
96};
97
98class 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 Srbecky527c9c72015-04-17 21:14:10 +0100109
David Srbecky033d7452015-04-30 19:57:35 +0100110enum PatchResult {
111 kAbsoluteAddress, // Absolute memory location.
112 kPointerRelativeAddress, // Offset relative to the location of the pointer.
113 kSectionRelativeAddress, // Offset relative to start of containing section.
114};
115
116// Patch memory addresses within a buffer.
117// It assumes that the unpatched addresses are offsets relative to base_address.
118// (which generally means method's low_pc relative to the start of .text)
119template <typename Elf_Addr, typename Address, PatchResult kPatchResult>
120static void Patch(const std::vector<uintptr_t>& patch_locations,
121 Elf_Addr buffer_address, Elf_Addr base_address,
122 std::vector<uint8_t>* buffer) {
123 for (uintptr_t location : patch_locations) {
124 typedef __attribute__((__aligned__(1))) Address UnalignedAddress;
125 auto* to_patch = reinterpret_cast<UnalignedAddress*>(buffer->data() + location);
126 switch (kPatchResult) {
127 case kAbsoluteAddress:
128 *to_patch = (base_address + *to_patch);
129 break;
130 case kPointerRelativeAddress:
131 *to_patch = (base_address + *to_patch) - (buffer_address + location);
132 break;
133 case kSectionRelativeAddress:
134 *to_patch = (base_address + *to_patch) - buffer_address;
135 break;
136 }
137 }
138}
139
David Srbecky533c2072015-04-22 12:20:22 +0100140template <typename ElfTypes>
141bool ElfWriterQuick<ElfTypes>::Write(
142 OatWriter* oat_writer,
143 const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
144 const std::string& android_root_unused ATTRIBUTE_UNUSED,
145 bool is_host_unused ATTRIBUTE_UNUSED) {
David Srbecky033d7452015-04-30 19:57:35 +0100146 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +0100147 const InstructionSet isa = compiler_driver_->GetInstructionSet();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700148
David Srbeckybc90fd02015-04-22 19:40:27 +0100149 // Setup the builder with the main OAT sections (.rodata .text .bss).
150 const size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
151 const size_t text_size = oat_writer->GetSize() - rodata_size;
152 const size_t bss_size = oat_writer->GetBssSize();
153 RodataWriter rodata_writer(oat_writer);
154 TextWriter text_writer(oat_writer);
David Srbecky533c2072015-04-22 12:20:22 +0100155 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(
David Srbeckybc90fd02015-04-22 19:40:27 +0100156 isa, rodata_size, &rodata_writer, text_size, &text_writer, bss_size));
Alex Light78382fa2014-06-06 15:45:32 -0700157
David Srbeckybc90fd02015-04-22 19:40:27 +0100158 // Add debug sections.
159 // They are stack allocated here (in the same scope as the builder),
160 // but they are registred with the builder only if they are used.
161 using RawSection = typename ElfBuilder<ElfTypes>::RawSection;
David Srbeckybc90fd02015-04-22 19:40:27 +0100162 const auto* text = builder->GetText();
David Srbeckybc90fd02015-04-22 19:40:27 +0100163 const bool is64bit = Is64BitInstructionSet(isa);
David Srbecky033d7452015-04-30 19:57:35 +0100164 RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
165 is64bit ? Patch<Elf_Addr, uint64_t, kPointerRelativeAddress> :
166 Patch<Elf_Addr, uint32_t, kPointerRelativeAddress>,
167 text);
168 RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0,
169 Patch<Elf_Addr, uint32_t, kSectionRelativeAddress>, text);
170 RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
171 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
172 RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
173 RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
174 RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
175 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
David Srbeckybc90fd02015-04-22 19:40:27 +0100176 if (!oat_writer->GetMethodDebugInfo().empty()) {
177 if (compiler_driver_->GetCompilerOptions().GetIncludeCFI()) {
178 dwarf::WriteEhFrame(
179 compiler_driver_, oat_writer, dwarf::DW_EH_PE_pcrel,
180 eh_frame.GetBuffer(), eh_frame.GetPatchLocations(),
David Srbecky033d7452015-04-30 19:57:35 +0100181 eh_frame_hdr.GetBuffer(), eh_frame_hdr.GetPatchLocations());
David Srbeckybc90fd02015-04-22 19:40:27 +0100182 builder->RegisterSection(&eh_frame);
183 builder->RegisterSection(&eh_frame_hdr);
184 }
185 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
186 // Add methods to .symtab.
187 WriteDebugSymbols(builder.get(), oat_writer);
188 // Generate DWARF .debug_* sections.
189 dwarf::WriteDebugSections(
190 compiler_driver_, oat_writer,
191 debug_info.GetBuffer(), debug_info.GetPatchLocations(),
192 debug_abbrev.GetBuffer(),
193 debug_str.GetBuffer(),
194 debug_line.GetBuffer(), debug_line.GetPatchLocations());
195 builder->RegisterSection(&debug_info);
196 builder->RegisterSection(&debug_abbrev);
197 builder->RegisterSection(&debug_str);
198 builder->RegisterSection(&debug_line);
199 *oat_writer->GetAbsolutePatchLocationsFor(".debug_info") =
200 *debug_info.GetPatchLocations();
201 *oat_writer->GetAbsolutePatchLocationsFor(".debug_line") =
202 *debug_line.GetPatchLocations();
203 }
204 }
205
206 // Add relocation section.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700207 RawSection oat_patches(".oat_patches", SHT_OAT_PATCH, 0, nullptr, 0, 1, 0);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100208 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() ||
209 // ElfWriter::Fixup will be called regardless and it needs to be able
210 // to patch debug sections so we have to include patches for them.
211 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
David Srbecky2f6cdb02015-04-11 00:17:53 +0100212 EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer());
David Srbeckybc90fd02015-04-22 19:40:27 +0100213 builder->RegisterSection(&oat_patches);
David Srbecky527c9c72015-04-17 21:14:10 +0100214 }
215
David Srbeckybc90fd02015-04-22 19:40:27 +0100216 return builder->Write(elf_file_);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700217}
Mark Mendellae9fd932014-02-10 16:14:35 -0800218
David Srbecky533c2072015-04-22 12:20:22 +0100219template <typename ElfTypes>
David Srbecky533c2072015-04-22 12:20:22 +0100220static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100221 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky626a1662015-04-12 13:12:26 +0100222
223 // Find all addresses (low_pc) which contain deduped methods.
224 // The first instance of method is not marked deduped_, but the rest is.
225 std::unordered_set<uint32_t> deduped_addresses;
226 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
227 if (it->deduped_) {
228 deduped_addresses.insert(it->low_pc_);
229 }
230 }
231
David Srbeckybc90fd02015-04-22 19:40:27 +0100232 auto* symtab = builder->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700233 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100234 if (it->deduped_) {
235 continue; // Add symbol only for the first instance.
236 }
David Srbecky0df9e1f2015-04-07 19:02:58 +0100237 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100238 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
239 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100240 }
241
David Srbecky6f715892015-03-30 14:21:42 +0100242 uint32_t low_pc = it->low_pc_;
243 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
244 low_pc += it->compiled_method_->CodeDelta();
David Srbeckybc90fd02015-04-22 19:40:27 +0100245 symtab->AddSymbol(name, builder->GetText(), low_pc,
David Srbecky6f715892015-03-30 14:21:42 +0100246 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247
Ningsheng Jianf9734552014-10-27 14:56:34 +0800248 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
249 // instructions, so that disassembler tools can correctly disassemble.
250 if (it->compiled_method_->GetInstructionSet() == kThumb2) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100251 symtab->AddSymbol("$t", builder->GetText(), it->low_pc_ & ~1, true,
Ningsheng Jianf9734552014-10-27 14:56:34 +0800252 0, STB_LOCAL, STT_NOTYPE);
253 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700255}
256
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000257// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100258template class ElfWriterQuick<ElfTypes32>;
259template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000260
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261} // namespace art