blob: 5c059e1e82cf42544d1abe3407eebdb8b840bca1 [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
David Srbeckyf8980872015-05-22 17:04:47 +010022#include "base/casts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "base/logging.h"
24#include "base/unix_file/fd_file.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 Carlstrom7940e442013-07-12 13:46:57 -070033#include "globals.h"
Andreas Gampe79273802014-08-05 20:21:05 -070034#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "oat.h"
Brian Carlstromc50d8e12013-07-23 22:35:16 -070036#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037#include "utils.h"
38
39namespace art {
40
David Srbeckyad5fa8c2015-05-06 18:27:35 +010041// .eh_frame and .debug_frame are almost identical.
42// Except for some minor formatting differences, the main difference
43// is that .eh_frame is allocated within the running program because
44// it is used by C++ exception handling (which we do not use so we
45// can choose either). C++ compilers generally tend to use .eh_frame
46// because if they need it sometimes, they might as well always use it.
David Srbeckyaaf143d2015-05-21 14:03:48 +010047// Let's use .debug_frame because it is easier to strip or compress.
48constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
David Srbeckyad5fa8c2015-05-06 18:27:35 +010049
David Srbecky388d2862015-05-21 19:11:18 +010050// The ARM specification defines three special mapping symbols
51// $a, $t and $d which mark ARM, Thumb and data ranges respectively.
52// These symbols can be used by tools, for example, to pretty
53// print instructions correctly. Objdump will use them if they
54// exist, but it will still work well without them.
55// However, these extra symbols take space, so let's just generate
56// one symbol which marks the whole .text section as code.
57constexpr bool kGenerateSingleArmMappingSymbol = true;
58
David Srbecky533c2072015-04-22 12:20:22 +010059template <typename ElfTypes>
60bool ElfWriterQuick<ElfTypes>::Create(File* elf_file,
61 OatWriter* oat_writer,
62 const std::vector<const DexFile*>& dex_files,
63 const std::string& android_root,
64 bool is_host,
65 const CompilerDriver& driver) {
Brian Carlstromb12f3472014-06-11 14:54:46 -070066 ElfWriterQuick elf_writer(driver, elf_file);
67 return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
68}
69
David Srbecky533c2072015-04-22 12:20:22 +010070template <typename ElfTypes>
71static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070072
David Srbecky533c2072015-04-22 12:20:22 +010073template <typename ElfTypes>
74bool ElfWriterQuick<ElfTypes>::Write(
75 OatWriter* oat_writer,
76 const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
77 const std::string& android_root_unused ATTRIBUTE_UNUSED,
78 bool is_host_unused ATTRIBUTE_UNUSED) {
David Srbeckybc90fd02015-04-22 19:40:27 +010079 const InstructionSet isa = compiler_driver_->GetInstructionSet();
David Srbecky6d8c8f02015-10-26 10:57:09 +000080 std::unique_ptr<BufferedOutputStream> output_stream(
81 new BufferedOutputStream(new FileOutputStream(elf_file_)));
82 std::unique_ptr<ElfBuilder<ElfTypes>> builder(
83 new ElfBuilder<ElfTypes>(isa, output_stream.get()));
Brian Carlstromb12f3472014-06-11 14:54:46 -070084
David Srbecky6d8c8f02015-10-26 10:57:09 +000085 builder->Start();
Alex Light78382fa2014-06-06 15:45:32 -070086
David Srbecky6d8c8f02015-10-26 10:57:09 +000087 auto* rodata = builder->GetRoData();
88 auto* text = builder->GetText();
89 auto* bss = builder->GetBss();
90
91 rodata->Start();
92 if (!oat_writer->WriteRodata(rodata)) {
93 return false;
94 }
95 rodata->End();
96
97 text->Start();
98 if (!oat_writer->WriteCode(text)) {
99 return false;
100 }
101 text->End();
102
103 if (oat_writer->GetBssSize() != 0) {
104 bss->Start();
105 bss->SetSize(oat_writer->GetBssSize());
106 bss->End();
107 }
108
109 builder->WriteDynamicSection(elf_file_->GetPath());
110
111 if (compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
112 const auto& method_infos = oat_writer->GetMethodDebugInfo();
113 if (!method_infos.empty()) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100114 // Add methods to .symtab.
115 WriteDebugSymbols(builder.get(), oat_writer);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000116 // Generate CFI (stack unwinding information).
117 dwarf::WriteCFISection(builder.get(), method_infos, kCFIFormat);
118 // Write DWARF .debug_* sections.
119 dwarf::WriteDebugSections(builder.get(), method_infos);
David Srbeckybc90fd02015-04-22 19:40:27 +0100120 }
121 }
122
David Srbeckyf8980872015-05-22 17:04:47 +0100123 // Add relocation section for .text.
David Srbeckyf8980872015-05-22 17:04:47 +0100124 if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) {
125 // Note that ElfWriter::Fixup will be called regardless and therefore
126 // we need to include oat_patches for debug sections unconditionally.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127 builder->WritePatches(".text.oat_patches", &oat_writer->GetAbsolutePatchLocations());
David Srbecky527c9c72015-04-17 21:14:10 +0100128 }
129
David Srbecky6d8c8f02015-10-26 10:57:09 +0000130 builder->End();
131
132 return builder->Good() && output_stream->Flush();
Brian Carlstromb12f3472014-06-11 14:54:46 -0700133}
Mark Mendellae9fd932014-02-10 16:14:35 -0800134
David Srbecky533c2072015-04-22 12:20:22 +0100135template <typename ElfTypes>
David Srbecky533c2072015-04-22 12:20:22 +0100136static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
David Srbecky3b9d57a2015-04-10 00:22:14 +0100137 const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
David Srbecky388d2862015-05-21 19:11:18 +0100138 bool generated_mapping_symbol = false;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 auto* strtab = builder->GetStrTab();
140 auto* symtab = builder->GetSymTab();
141
142 if (method_info.empty()) {
143 return;
144 }
David Srbecky626a1662015-04-12 13:12:26 +0100145
146 // Find all addresses (low_pc) which contain deduped methods.
147 // The first instance of method is not marked deduped_, but the rest is.
148 std::unordered_set<uint32_t> deduped_addresses;
149 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
150 if (it->deduped_) {
151 deduped_addresses.insert(it->low_pc_);
152 }
153 }
154
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155 strtab->Start();
156 strtab->Write(""); // strtab should start with empty string.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700157 for (auto it = method_info.begin(); it != method_info.end(); ++it) {
David Srbecky6d73c9d2015-05-01 15:00:40 +0100158 if (it->deduped_) {
159 continue; // Add symbol only for the first instance.
160 }
David Srbecky0df9e1f2015-04-07 19:02:58 +0100161 std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
David Srbecky626a1662015-04-12 13:12:26 +0100162 if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
163 name += " [DEDUPED]";
David Srbecky0df9e1f2015-04-07 19:02:58 +0100164 }
165
David Srbecky6f715892015-03-30 14:21:42 +0100166 uint32_t low_pc = it->low_pc_;
167 // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
168 low_pc += it->compiled_method_->CodeDelta();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000169 symtab->Add(strtab->Write(name), builder->GetText(), low_pc,
170 true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171
Ningsheng Jianf9734552014-10-27 14:56:34 +0800172 // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
173 // instructions, so that disassembler tools can correctly disassemble.
David Srbecky388d2862015-05-21 19:11:18 +0100174 // Note that even if we generate just a single mapping symbol, ARM's Streamline
175 // requires it to match function symbol. Just address 0 does not work.
Ningsheng Jianf9734552014-10-27 14:56:34 +0800176 if (it->compiled_method_->GetInstructionSet() == kThumb2) {
David Srbecky388d2862015-05-21 19:11:18 +0100177 if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000178 symtab->Add(strtab->Write("$t"), builder->GetText(), it->low_pc_ & ~1,
179 true, 0, STB_LOCAL, STT_NOTYPE);
David Srbecky388d2862015-05-21 19:11:18 +0100180 generated_mapping_symbol = true;
181 }
Ningsheng Jianf9734552014-10-27 14:56:34 +0800182 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000184 strtab->End();
185
186 // Symbols are buffered and written after names (because they are smaller).
187 // We could also do two passes in this function to avoid the buffering.
188 symtab->Start();
189 symtab->Write();
190 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191}
192
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000193// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100194template class ElfWriterQuick<ElfTypes32>;
195template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000196
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197} // namespace art