blob: af42364f2cd784a9e7b9aa424b6bac35f771ddfd [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 Srbeckyad5fa8c2015-05-06 18:27:35 +010040// .eh_frame and .debug_frame are almost identical.
41// Except for some minor formatting differences, the main difference
42// is that .eh_frame is allocated within the running program because
43// it is used by C++ exception handling (which we do not use so we
44// can choose either). C++ compilers generally tend to use .eh_frame
45// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010046// Let's use .debug_frame because it is easier to strip or compress.
47constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010048
David Srbecky533c2072015-04-22 12:20:22 +010049template <typename ElfTypes>
50bool ElfWriterQuick<ElfTypes>::Create(File* elf_file,
51 OatWriter* oat_writer,
52 const std::vector<const DexFile*>& dex_files,
53 const std::string& android_root,
54 bool is_host,
55 const CompilerDriver& driver) {
Brian Carlstromb12f3472014-06-11 14:54:46 -070056 ElfWriterQuick elf_writer(driver, elf_file);
57 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
58}
59
David Srbecky533c2072015-04-22 12:20:22 +010060template <typename ElfTypes>
61static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070062
David Srbecky2f6cdb02015-04-11 00:17:53 +010063// Encode patch locations in .oat_patches format.
David Srbecky533c2072015-04-22 12:20:22 +010064template <typename ElfTypes>
65void ElfWriterQuick<ElfTypes>::EncodeOatPatches(
66 const OatWriter::PatchLocationsMap& sections,
67 std::vector<uint8_t>* buffer) {
David Srbecky2f6cdb02015-04-11 00:17:53 +010068 for (const auto& section : sections) {
69 const std::string& name = section.first;
70 std::vector<uintptr_t>* locations = section.second.get();
71 DCHECK(!name.empty());
72 std::sort(locations->begin(), locations->end());
73 // Reserve buffer space - guess 2 bytes per ULEB128.
74 buffer->reserve(buffer->size() + name.size() + locations->size() * 2);
75 // Write null-terminated section name.
76 const uint8_t* name_data = reinterpret_cast<const uint8_t*>(name.c_str());
77 buffer->insert(buffer->end(), name_data, name_data + name.size() + 1);
78 // Write placeholder for data length.
79 size_t length_pos = buffer->size();
80 EncodeUnsignedLeb128(buffer, UINT32_MAX);
81 // Write LEB128 encoded list of advances (deltas between consequtive addresses).
82 size_t data_pos = buffer->size();
83 uintptr_t address = 0; // relative to start of section.
84 for (uintptr_t location : *locations) {
85 DCHECK_LT(location - address, UINT32_MAX) << "Large gap between patch locations";
86 EncodeUnsignedLeb128(buffer, location - address);
87 address = location;
88 }
89 // Update length.
90 UpdateUnsignedLeb128(buffer->data() + length_pos, buffer->size() - data_pos);
91 }
92 buffer->push_back(0); // End of sections.
93}
94
David Srbeckybc90fd02015-04-22 19:40:27 +010095class RodataWriter FINAL : public CodeOutput {
96 public:
97 explicit RodataWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
98
99 bool Write(OutputStream* out) OVERRIDE {
100 return oat_writer_->WriteRodata(out);
David Srbecky527c9c72015-04-17 21:14:10 +0100101 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100102
103 private:
104 OatWriter* oat_writer_;
105};
106
107class TextWriter FINAL : public CodeOutput {
108 public:
109 explicit TextWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
110
111 bool Write(OutputStream* out) OVERRIDE {
112 return oat_writer_->WriteCode(out);
113 }
114
115 private:
116 OatWriter* oat_writer_;
117};
David Srbecky527c9c72015-04-17 21:14:10 +0100118
David Srbecky033d7452015-04-30 19:57:35 +0100119enum PatchResult {
120 kAbsoluteAddress, // Absolute memory location.
121 kPointerRelativeAddress, // Offset relative to the location of the pointer.
122 kSectionRelativeAddress, // Offset relative to start of containing section.
123};
124
125// Patch memory addresses within a buffer.
126// It assumes that the unpatched addresses are offsets relative to base_address.
127// (which generally means method's low_pc relative to the start of .text)
128template <typename Elf_Addr, typename Address, PatchResult kPatchResult>
129static void Patch(const std::vector<uintptr_t>& patch_locations,
130 Elf_Addr buffer_address, Elf_Addr base_address,
131 std::vector<uint8_t>* buffer) {
132 for (uintptr_t location : patch_locations) {
133 typedef __attribute__((__aligned__(1))) Address UnalignedAddress;
134 auto* to_patch = reinterpret_cast<UnalignedAddress*>(buffer->data() + location);
135 switch (kPatchResult) {
136 case kAbsoluteAddress:
137 *to_patch = (base_address + *to_patch);
138 break;
139 case kPointerRelativeAddress:
140 *to_patch = (base_address + *to_patch) - (buffer_address + location);
141 break;
142 case kSectionRelativeAddress:
143 *to_patch = (base_address + *to_patch) - buffer_address;
144 break;
145 }
146 }
147}
148
David Srbecky533c2072015-04-22 12:20:22 +0100149template <typename ElfTypes>
150bool ElfWriterQuick<ElfTypes>::Write(
151 OatWriter* oat_writer,
152 const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
153 const std::string& android_root_unused ATTRIBUTE_UNUSED,
154 bool is_host_unused ATTRIBUTE_UNUSED) {
David Srbecky033d7452015-04-30 19:57:35 +0100155 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +0100156 const InstructionSet isa = compiler_driver_->GetInstructionSet();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700157
David Srbeckybc90fd02015-04-22 19:40:27 +0100158 // Setup the builder with the main OAT sections (.rodata .text .bss).
159 const size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
160 const size_t text_size = oat_writer->GetSize() - rodata_size;
161 const size_t bss_size = oat_writer->GetBssSize();
162 RodataWriter rodata_writer(oat_writer);
163 TextWriter text_writer(oat_writer);
David Srbecky533c2072015-04-22 12:20:22 +0100164 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(
David Srbeckybc90fd02015-04-22 19:40:27 +0100165 isa, rodata_size, &rodata_writer, text_size, &text_writer, bss_size));
Alex Light78382fa2014-06-06 15:45:32 -0700166
David Srbeckybc90fd02015-04-22 19:40:27 +0100167 // Add debug sections.
168 // They are stack allocated here (in the same scope as the builder),
169 // but they are registred with the builder only if they are used.
170 using RawSection = typename ElfBuilder<ElfTypes>::RawSection;
David Srbeckybc90fd02015-04-22 19:40:27 +0100171 const auto* text = builder->GetText();
David Srbeckybc90fd02015-04-22 19:40:27 +0100172 const bool is64bit = Is64BitInstructionSet(isa);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100173 const int pointer_size = GetInstructionSetPointerSize(isa);
David Srbecky033d7452015-04-30 19:57:35 +0100174 RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
175 is64bit ? Patch<Elf_Addr, uint64_t, kPointerRelativeAddress> :
176 Patch<Elf_Addr, uint32_t, kPointerRelativeAddress>,
177 text);
178 RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0,
179 Patch<Elf_Addr, uint32_t, kSectionRelativeAddress>, text);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100180 RawSection debug_frame(".debug_frame", SHT_PROGBITS, 0, nullptr, 0, pointer_size, 0,
181 is64bit ? Patch<Elf_Addr, uint64_t, kAbsoluteAddress> :
182 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>,
183 text);
David Srbecky033d7452015-04-30 19:57:35 +0100184 RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
185 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
186 RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
187 RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
188 RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
189 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text);
David Srbeckybc90fd02015-04-22 19:40:27 +0100190 if (!oat_writer->GetMethodDebugInfo().empty()) {
191 if (compiler_driver_->GetCompilerOptions().GetIncludeCFI()) {
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100192 if (kCFIFormat == dwarf::DW_EH_FRAME_FORMAT) {
193 dwarf::WriteCFISection(
194 compiler_driver_, oat_writer,
195 dwarf::DW_EH_PE_pcrel, kCFIFormat,
196 eh_frame.GetBuffer(), eh_frame.GetPatchLocations(),
197 eh_frame_hdr.GetBuffer(), eh_frame_hdr.GetPatchLocations());
198 builder->RegisterSection(&eh_frame);
199 builder->RegisterSection(&eh_frame_hdr);
200 } else {
201 DCHECK(kCFIFormat == dwarf::DW_DEBUG_FRAME_FORMAT);
202 dwarf::WriteCFISection(
203 compiler_driver_, oat_writer,
204 dwarf::DW_EH_PE_absptr, kCFIFormat,
205 debug_frame.GetBuffer(), debug_frame.GetPatchLocations(),
206 nullptr, nullptr);
207 builder->RegisterSection(&debug_frame);
208 *oat_writer->GetAbsolutePatchLocationsFor(".debug_frame") =
209 *debug_frame.GetPatchLocations();
210 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100211 }
212 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
213 // Add methods to .symtab.
214 WriteDebugSymbols(builder.get(), oat_writer);
215 // Generate DWARF .debug_* sections.
216 dwarf::WriteDebugSections(
217 compiler_driver_, oat_writer,
218 debug_info.GetBuffer(), debug_info.GetPatchLocations(),
219 debug_abbrev.GetBuffer(),
220 debug_str.GetBuffer(),
221 debug_line.GetBuffer(), debug_line.GetPatchLocations());
222 builder->RegisterSection(&debug_info);
223 builder->RegisterSection(&debug_abbrev);
224 builder->RegisterSection(&debug_str);
225 builder->RegisterSection(&debug_line);
226 *oat_writer->GetAbsolutePatchLocationsFor(".debug_info") =
227 *debug_info.GetPatchLocations();
228 *oat_writer->GetAbsolutePatchLocationsFor(".debug_line") =
229 *debug_line.GetPatchLocations();
230 }
231 }
232
233 // Add relocation section.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 RawSection oat_patches(".oat_patches", SHT_OAT_PATCH, 0, nullptr, 0, 1, 0);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100235 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() ||
236 // ElfWriter::Fixup will be called regardless and it needs to be able
237 // to patch debug sections so we have to include patches for them.
238 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
David Srbecky2f6cdb02015-04-11 00:17:53 +0100239 EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer());
David Srbeckybc90fd02015-04-22 19:40:27 +0100240 builder->RegisterSection(&oat_patches);
David Srbecky527c9c72015-04-17 21:14:10 +0100241 }
242
David Srbeckybc90fd02015-04-22 19:40:27 +0100243 return builder->Write(elf_file_);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700244}
Mark Mendellae9fd932014-02-10 16:14:35 -0800245
David Srbecky533c2072015-04-22 12:20:22 +0100246template <typename ElfTypes>
David Srbecky533c2072015-04-22 12:20:22 +0100247static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100248 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky626a1662015-04-12 13:12:26 +0100249
250 // Find all addresses (low_pc) which contain deduped methods.
251 // The first instance of method is not marked deduped_, but the rest is.
252 std::unordered_set<uint32_t> deduped_addresses;
253 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
254 if (it->deduped_) {
255 deduped_addresses.insert(it->low_pc_);
256 }
257 }
258
David Srbeckybc90fd02015-04-22 19:40:27 +0100259 auto* symtab = builder->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100261 if (it->deduped_) {
262 continue; // Add symbol only for the first instance.
263 }
David Srbecky0df9e1f2015-04-07 19:02:58 +0100264 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100265 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
266 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100267 }
268
David Srbecky6f715892015-03-30 14:21:42 +0100269 uint32_t low_pc = it->low_pc_;
270 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
271 low_pc += it->compiled_method_->CodeDelta();
David Srbeckybc90fd02015-04-22 19:40:27 +0100272 symtab->AddSymbol(name, builder->GetText(), low_pc,
David Srbecky6f715892015-03-30 14:21:42 +0100273 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274
Ningsheng Jianf9734552014-10-27 14:56:34 +0800275 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
276 // instructions, so that disassembler tools can correctly disassemble.
277 if (it->compiled_method_->GetInstructionSet() == kThumb2) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100278 symtab->AddSymbol("$t", builder->GetText(), it->low_pc_ & ~1, true,
Ningsheng Jianf9734552014-10-27 14:56:34 +0800279 0, STB_LOCAL, STT_NOTYPE);
280 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282}
283
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000284// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100285template class ElfWriterQuick<ElfTypes32>;
286template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000287
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288} // namespace art