blob: 429cd851e0aa6c51d05440720b330488479a7dad [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
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +000042template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr,
43 typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr,
44 typename Elf_Phdr, typename Elf_Shdr>
45bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +000046 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Create(File* elf_file,
Brian Carlstromb12f3472014-06-11 14:54:46 -070047 OatWriter* oat_writer,
48 const std::vector<const DexFile*>& dex_files,
49 const std::string& android_root,
50 bool is_host,
51 const CompilerDriver& driver) {
52 ElfWriterQuick elf_writer(driver, elf_file);
53 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
54}
55
Ian Rogers0279ebb2014-10-08 17:27:48 -070056class OatWriterWrapper FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070057 public:
58 explicit OatWriterWrapper(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
59
Vladimir Markof4da6752014-08-01 19:04:18 +010060 void SetCodeOffset(size_t offset) {
61 oat_writer_->SetOatDataOffset(offset);
62 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -070063 bool Write(OutputStream* out) OVERRIDE {
64 return oat_writer_->Write(out);
65 }
66 private:
Ian Rogers0279ebb2014-10-08 17:27:48 -070067 OatWriter* const oat_writer_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -070068};
69
70template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr,
71 typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr,
72 typename Elf_Phdr, typename Elf_Shdr>
73static void WriteDebugSymbols(const CompilerDriver* compiler_driver,
74 ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
75 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder,
76 OatWriter* oat_writer);
77
David Srbecky2f6cdb02015-04-11 00:17:53 +010078// Encode patch locations in .oat_patches format.
79template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr,
80 typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr,
81 typename Elf_Phdr, typename Elf_Shdr>
82void ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn, Elf_Sym, Elf_Ehdr,
83 Elf_Phdr, Elf_Shdr>::EncodeOatPatches(const OatWriter::PatchLocationsMap& sections,
84 std::vector<uint8_t>* buffer) {
85 for (const auto& section : sections) {
86 const std::string& name = section.first;
87 std::vector<uintptr_t>* locations = section.second.get();
88 DCHECK(!name.empty());
89 std::sort(locations->begin(), locations->end());
90 // Reserve buffer space - guess 2 bytes per ULEB128.
91 buffer->reserve(buffer->size() + name.size() + locations->size() * 2);
92 // Write null-terminated section name.
93 const uint8_t* name_data = reinterpret_cast<const uint8_t*>(name.c_str());
94 buffer->insert(buffer->end(), name_data, name_data + name.size() + 1);
95 // Write placeholder for data length.
96 size_t length_pos = buffer->size();
97 EncodeUnsignedLeb128(buffer, UINT32_MAX);
98 // Write LEB128 encoded list of advances (deltas between consequtive addresses).
99 size_t data_pos = buffer->size();
100 uintptr_t address = 0; // relative to start of section.
101 for (uintptr_t location : *locations) {
102 DCHECK_LT(location - address, UINT32_MAX) << "Large gap between patch locations";
103 EncodeUnsignedLeb128(buffer, location - address);
104 address = location;
105 }
106 // Update length.
107 UpdateUnsignedLeb128(buffer->data() + length_pos, buffer->size() - data_pos);
108 }
109 buffer->push_back(0); // End of sections.
110}
111
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000112template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr,
113 typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr,
114 typename Elf_Phdr, typename Elf_Shdr>
115bool ElfWriterQuick<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
116 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>::Write(OatWriter* oat_writer,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700117 const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
118 const std::string& android_root_unused ATTRIBUTE_UNUSED,
119 bool is_host_unused ATTRIBUTE_UNUSED) {
Andreas Gampe79273802014-08-05 20:21:05 -0700120 constexpr bool debug = false;
Brian Carlstromb12f3472014-06-11 14:54:46 -0700121 const OatHeader& oat_header = oat_writer->GetOatHeader();
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000122 Elf_Word oat_data_size = oat_header.GetExecutableOffset();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700123 uint32_t oat_exec_size = oat_writer->GetSize() - oat_data_size;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000124 uint32_t oat_bss_size = oat_writer->GetBssSize();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700125
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700126 OatWriterWrapper wrapper(oat_writer);
127
128 std::unique_ptr<ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
129 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr> > builder(
130 new ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
131 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>(
132 &wrapper,
133 elf_file_,
134 compiler_driver_->GetInstructionSet(),
135 0,
136 oat_data_size,
137 oat_data_size,
138 oat_exec_size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000139 RoundUp(oat_data_size + oat_exec_size, kPageSize),
140 oat_bss_size,
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700141 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols(),
142 debug));
Alex Light78382fa2014-06-06 15:45:32 -0700143
Brian Carlstrom18a49cc2014-08-29 16:20:48 -0700144 if (!builder->Init()) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700145 return false;
146 }
147
David Srbecky8dc73242015-04-12 11:40:39 +0100148 if (compiler_driver_->GetCompilerOptions().GetIncludeCFI() &&
149 !oat_writer->GetMethodDebugInfo().empty()) {
150 ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> eh_frame(
151 ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0);
152 dwarf::WriteEhFrame(compiler_driver_, oat_writer,
153 builder->GetTextBuilder().GetSection()->sh_addr,
154 eh_frame.GetBuffer());
155 builder->RegisterRawSection(eh_frame);
156 }
157
David Srbecky3b9d57a2015-04-10 00:22:14 +0100158 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols() &&
159 !oat_writer->GetMethodDebugInfo().empty()) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700160 WriteDebugSymbols(compiler_driver_, builder.get(), oat_writer);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700161 }
162
David Srbecky2f6cdb02015-04-11 00:17:53 +0100163 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() ||
164 // ElfWriter::Fixup will be called regardless and it needs to be able
165 // to patch debug sections so we have to include patches for them.
166 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167 ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> oat_patches(
David Srbecky2f6cdb02015-04-11 00:17:53 +0100168 ".oat_patches", SHT_OAT_PATCH, 0, NULL, 0, 1, 0);
169 EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer());
Brian Carlstrom18a49cc2014-08-29 16:20:48 -0700170 builder->RegisterRawSection(oat_patches);
Alex Light53cb16b2014-06-12 11:26:29 -0700171 }
172
Brian Carlstrom18a49cc2014-08-29 16:20:48 -0700173 return builder->Write();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700174}
Mark Mendellae9fd932014-02-10 16:14:35 -0800175
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700176template <typename Elf_Word, typename Elf_Sword, typename Elf_Addr,
177 typename Elf_Dyn, typename Elf_Sym, typename Elf_Ehdr,
178 typename Elf_Phdr, typename Elf_Shdr>
Andreas Gampe86830382014-12-12 21:41:29 -0800179// Do not inline to avoid Clang stack frame problems. b/18738594
180NO_INLINE
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181static void WriteDebugSymbols(const CompilerDriver* compiler_driver,
182 ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
183 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr>* builder,
184 OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100185 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky626a1662015-04-12 13:12:26 +0100186
187 // Find all addresses (low_pc) which contain deduped methods.
188 // The first instance of method is not marked deduped_, but the rest is.
189 std::unordered_set<uint32_t> deduped_addresses;
190 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
191 if (it->deduped_) {
192 deduped_addresses.insert(it->low_pc_);
193 }
194 }
195
Ian Rogers0279ebb2014-10-08 17:27:48 -0700196 ElfSymtabBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Sym, Elf_Shdr>* symtab =
197 builder->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100199 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100200 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
201 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100202 }
203
David Srbecky6f715892015-03-30 14:21:42 +0100204 uint32_t low_pc = it->low_pc_;
205 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
206 low_pc += it->compiled_method_->CodeDelta();
David Srbecky0df9e1f2015-04-07 19:02:58 +0100207 symtab->AddSymbol(name, &builder->GetTextBuilder(), low_pc,
David Srbecky6f715892015-03-30 14:21:42 +0100208 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700209
Ningsheng Jianf9734552014-10-27 14:56:34 +0800210 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
211 // instructions, so that disassembler tools can correctly disassemble.
212 if (it->compiled_method_->GetInstructionSet() == kThumb2) {
213 symtab->AddSymbol("$t", &builder->GetTextBuilder(), it->low_pc_ & ~1, true,
214 0, STB_LOCAL, STT_NOTYPE);
215 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 }
217
David Srbecky3b9d57a2015-04-10 00:22:14 +0100218 typedef ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> Section;
David Srbecky3b9d57a2015-04-10 00:22:14 +0100219 Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
220 Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
221 Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
222 Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223
David Srbecky3b9d57a2015-04-10 00:22:14 +0100224 dwarf::WriteDebugSections(compiler_driver,
225 oat_writer,
226 builder->GetTextBuilder().GetSection()->sh_addr,
David Srbecky3b9d57a2015-04-10 00:22:14 +0100227 debug_info.GetBuffer(),
228 debug_abbrev.GetBuffer(),
229 debug_str.GetBuffer(),
230 debug_line.GetBuffer());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231
David Srbecky3b9d57a2015-04-10 00:22:14 +0100232 builder->RegisterRawSection(debug_info);
233 builder->RegisterRawSection(debug_abbrev);
234 builder->RegisterRawSection(debug_str);
235 builder->RegisterRawSection(debug_line);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236}
237
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000238// Explicit instantiations
239template class ElfWriterQuick<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
240 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>;
241template class ElfWriterQuick<Elf64_Word, Elf64_Sword, Elf64_Addr, Elf64_Dyn,
242 Elf64_Sym, Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>;
243
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244} // namespace art