blob: d679468315eaf8ea926cec4852d12084b8c6169f [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 Srbecky533c2072015-04-22 12:20:22 +0100110template <typename ElfTypes>
111bool ElfWriterQuick<ElfTypes>::Write(
112 OatWriter* oat_writer,
113 const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
114 const std::string& android_root_unused ATTRIBUTE_UNUSED,
115 bool is_host_unused ATTRIBUTE_UNUSED) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100116 const InstructionSet isa = compiler_driver_->GetInstructionSet();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700117
David Srbeckybc90fd02015-04-22 19:40:27 +0100118 // Setup the builder with the main OAT sections (.rodata .text .bss).
119 const size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
120 const size_t text_size = oat_writer->GetSize() - rodata_size;
121 const size_t bss_size = oat_writer->GetBssSize();
122 RodataWriter rodata_writer(oat_writer);
123 TextWriter text_writer(oat_writer);
David Srbecky533c2072015-04-22 12:20:22 +0100124 std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(
David Srbeckybc90fd02015-04-22 19:40:27 +0100125 isa, rodata_size, &rodata_writer, text_size, &text_writer, bss_size));
Alex Light78382fa2014-06-06 15:45:32 -0700126
David Srbeckybc90fd02015-04-22 19:40:27 +0100127 // Add debug sections.
128 // They are stack allocated here (in the same scope as the builder),
129 // but they are registred with the builder only if they are used.
130 using RawSection = typename ElfBuilder<ElfTypes>::RawSection;
131 const int pointer_size = GetInstructionSetPointerSize(isa);
132 const auto* text = builder->GetText();
133 constexpr bool absolute = false; // patch to make absolute addresses.
134 constexpr bool relative = true; // patch to make relative addresses.
135 const bool is64bit = Is64BitInstructionSet(isa);
136 RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC,
137 nullptr, 0, pointer_size, 0, text, relative, is64bit);
138 RawSection eh_frame_hdr(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC,
139 nullptr, 0, 4, 0);
140 RawSection debug_info(".debug_info", SHT_PROGBITS, 0,
141 nullptr, 0, 1, 0, text, absolute, false /* 32bit */);
142 RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0,
143 nullptr, 0, 1, 0);
144 RawSection debug_str(".debug_str", SHT_PROGBITS, 0,
145 nullptr, 0, 1, 0);
146 RawSection debug_line(".debug_line", SHT_PROGBITS, 0,
147 nullptr, 0, 1, 0, text, absolute, false /* 32bit */);
148 if (!oat_writer->GetMethodDebugInfo().empty()) {
149 if (compiler_driver_->GetCompilerOptions().GetIncludeCFI()) {
150 dwarf::WriteEhFrame(
151 compiler_driver_, oat_writer, dwarf::DW_EH_PE_pcrel,
152 eh_frame.GetBuffer(), eh_frame.GetPatchLocations(),
153 eh_frame_hdr.GetBuffer());
154 builder->RegisterSection(&eh_frame);
155 builder->RegisterSection(&eh_frame_hdr);
156 }
157 if (compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
158 // Add methods to .symtab.
159 WriteDebugSymbols(builder.get(), oat_writer);
160 // Generate DWARF .debug_* sections.
161 dwarf::WriteDebugSections(
162 compiler_driver_, oat_writer,
163 debug_info.GetBuffer(), debug_info.GetPatchLocations(),
164 debug_abbrev.GetBuffer(),
165 debug_str.GetBuffer(),
166 debug_line.GetBuffer(), debug_line.GetPatchLocations());
167 builder->RegisterSection(&debug_info);
168 builder->RegisterSection(&debug_abbrev);
169 builder->RegisterSection(&debug_str);
170 builder->RegisterSection(&debug_line);
171 *oat_writer->GetAbsolutePatchLocationsFor(".debug_info") =
172 *debug_info.GetPatchLocations();
173 *oat_writer->GetAbsolutePatchLocationsFor(".debug_line") =
174 *debug_line.GetPatchLocations();
175 }
176 }
177
178 // Add relocation section.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 RawSection oat_patches(".oat_patches", SHT_OAT_PATCH, 0, nullptr, 0, 1, 0);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100180 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation() ||
181 // ElfWriter::Fixup will be called regardless and it needs to be able
182 // to patch debug sections so we have to include patches for them.
183 compiler_driver_->GetCompilerOptions().GetIncludeDebugSymbols()) {
David Srbecky2f6cdb02015-04-11 00:17:53 +0100184 EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(), oat_patches.GetBuffer());
David Srbeckybc90fd02015-04-22 19:40:27 +0100185 builder->RegisterSection(&oat_patches);
David Srbecky527c9c72015-04-17 21:14:10 +0100186 }
187
David Srbeckybc90fd02015-04-22 19:40:27 +0100188 return builder->Write(elf_file_);
Brian Carlstromb12f3472014-06-11 14:54:46 -0700189}
Mark Mendellae9fd932014-02-10 16:14:35 -0800190
David Srbecky533c2072015-04-22 12:20:22 +0100191template <typename ElfTypes>
David Srbecky533c2072015-04-22 12:20:22 +0100192static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100193 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky626a1662015-04-12 13:12:26 +0100194
195 // Find all addresses (low_pc) which contain deduped methods.
196 // The first instance of method is not marked deduped_, but the rest is.
197 std::unordered_set<uint32_t> deduped_addresses;
198 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
199 if (it->deduped_) {
200 deduped_addresses.insert(it->low_pc_);
201 }
202 }
203
David Srbeckybc90fd02015-04-22 19:40:27 +0100204 auto* symtab = builder->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky0df9e1f2015-04-07 19:02:58 +0100206 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100207 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
208 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100209 }
210
David Srbecky6f715892015-03-30 14:21:42 +0100211 uint32_t low_pc = it->low_pc_;
212 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
213 low_pc += it->compiled_method_->CodeDelta();
David Srbeckybc90fd02015-04-22 19:40:27 +0100214 symtab->AddSymbol(name, builder->GetText(), low_pc,
David Srbecky6f715892015-03-30 14:21:42 +0100215 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216
Ningsheng Jianf9734552014-10-27 14:56:34 +0800217 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
218 // instructions, so that disassembler tools can correctly disassemble.
219 if (it->compiled_method_->GetInstructionSet() == kThumb2) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100220 symtab->AddSymbol("$t", builder->GetText(), it->low_pc_ & ~1, true,
Ningsheng Jianf9734552014-10-27 14:56:34 +0800221 0, STB_LOCAL, STT_NOTYPE);
222 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224}
225
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000226// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100227template class ElfWriterQuick<ElfTypes32>;
228template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000229
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230} // namespace art