blob: 949fcabc8e7feb1ae66fe06b9e4395c251d90efa [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"
Brian Carlstromc6dfdac2013-08-26 18:57:31 -070024#include "buffered_output_stream.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000025#include "compiled_method.h"
David Srbecky0df9e1f2015-04-07 19:02:58 +010026#include "dex_file-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070029#include "elf_builder.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070030#include "elf_file.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000031#include "elf_utils.h"
David Srbecky3b9d57a2015-04-10 00:22:14 +010032#include "elf_writer_debug.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070033#include "file_output_stream.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070035#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "oat.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070037#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038#include "utils.h"
39
40namespace art {
41
David Srbecky533c2072015-04-22 12:20:22 +010042template <typename ElfTypes>
43bool 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 Carlstromb12f3472014-06-11 14:54:46 -070049 ElfWriterQuick elf_writer(driver, elf_file);
50 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
51}
52
Ian Rogers0279ebb2014-10-08 17:27:48 -070053class OatWriterWrapper FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070054 public:
55 explicit OatWriterWrapper(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
56
Vladimir Markof4da6752014-08-01 19:04:18 +010057 void SetCodeOffset(size_t offset) {
58 oat_writer_->SetOatDataOffset(offset);
59 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -070060 bool Write(OutputStream* out) OVERRIDE {
61 return oat_writer_->Write(out);
62 }
63 private:
Ian Rogers0279ebb2014-10-08 17:27:48 -070064 OatWriter* const oat_writer_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -070065};
66
David Srbecky533c2072015-04-22 12:20:22 +010067template <typename ElfTypes>
68static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070069
David Srbecky2f6cdb02015-04-11 00:17:53 +010070// Encode patch locations in .oat_patches format.
David Srbecky533c2072015-04-22 12:20:22 +010071template <typename ElfTypes>
72void ElfWriterQuick<ElfTypes>::EncodeOatPatches(
73 const OatWriter::PatchLocationsMap& sections,
74 std::vector<uint8_t>* buffer) {
David Srbecky2f6cdb02015-04-11 00:17:53 +010075 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 Srbecky527c9c72015-04-17 21:14:10 +0100102template<typename AddressType, bool SubtractPatchLocation = false>
103static 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 Srbecky533c2072015-04-22 12:20:22 +0100115template <typename ElfTypes>
116bool 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 Gampe79273802014-08-05 20:21:05 -0700121 constexpr bool debug = false;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700122 const OatHeader& oat_header = oat_writer->GetOatHeader();
David Srbecky533c2072015-04-22 12:20:22 +0100123 typename ElfTypes::Word oat_data_size = oat_header.GetExecutableOffset();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700124 uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000125 uint32_t oat_bss_size = oat_writer->GetBssSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700126
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700127 OatWriterWrapper wrapper(oat_writer);
128
David Srbecky533c2072015-04-22 12:20:22 +0100129 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 Light78382fa2014-06-06 15:45:32 -0700141
David Srbecky527c9c72015-04-17 21:14:10 +0100142 InstructionSet isa = compiler_driver_->GetInstructionSet();
143 int alignment = GetInstructionSetPointerSize(isa);
David Srbecky533c2072015-04-22 12:20:22 +0100144 typedef ElfRawSectionBuilder<ElfTypes> RawSection;
David Srbecky527c9c72015-04-17 21:14:10 +0100145 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 Carlstrom18a49cc2014-08-29 16:20:48 -0700166 if (!builder->Init()) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700167 return false;
168 }
169
David Srbecky527c9c72015-04-17 21:14:10 +0100170 std::vector<uintptr_t>* debug_info_patches = nullptr;
171 std::vector<uintptr_t>* debug_line_patches = nullptr;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100172 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols() &&
173 !oat_writer->GetMethodDebugInfo().empty()) {
David Srbecky527c9c72015-04-17 21:14:10 +0100174 // 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 Carlstromb12f3472014-06-11 14:54:46 -0700188 }
189
David Srbecky2f6cdb02015-04-11 00:17:53 +0100190 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 Srbecky2f6cdb02015-04-11 00:17:53 +0100194 EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer());
David Srbecky527c9c72015-04-17 21:14:10 +0100195 builder->RegisterRawSection(&oat_patches);
196 }
197
198 // We know where .text and .eh_frame will be located, so patch the addresses.
David Srbecky533c2072015-04-22 12:20:22 +0100199 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 Srbecky527c9c72015-04-17 21:14:10 +0100201 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 Light53cb16b2014-06-12 11:26:29 -0700213 }
214
Brian Carlstrom18a49cc2014-08-29 16:20:48 -0700215 return builder->Write();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700216}
Mark Mendellae9fd932014-02-10 16:14:35 -0800217
David Srbecky533c2072015-04-22 12:20:22 +0100218template <typename ElfTypes>
Andreas Gampe86830382014-12-12 21:41:29 -0800219// Do not inline to avoid Clang stack frame problems. b/18738594
220NO_INLINE
David Srbecky533c2072015-04-22 12:20:22 +0100221static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100222 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky626a1662015-04-12 13:12:26 +0100223
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 Srbecky533c2072015-04-22 12:20:22 +0100233 ElfSymtabBuilder<ElfTypes>* symtab = builder->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100235 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100236 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
237 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100238 }
239
David Srbecky6f715892015-03-30 14:21:42 +0100240 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 Srbecky0df9e1f2015-04-07 19:02:58 +0100243 symtab->AddSymbol(name, &builder->GetTextBuilder(), low_pc,
David Srbecky6f715892015-03-30 14:21:42 +0100244 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700245
Ningsheng Jianf9734552014-10-27 14:56:34 +0800246 // 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 Gampe54fc26c2014-09-04 21:47:42 -0700252 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253}
254
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000255// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100256template class ElfWriterQuick<ElfTypes32>;
257template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000258
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259} // namespace art