blob: 6bf080a083bda48d6190c7e24f2020dab70ef9cf [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"
Vladimir Marko10c13562015-11-25 14:33:36 +000024#include "base/stl_util.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000025#include "compiled_method.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000027#include "dwarf/method_debug_info.h"
28#include "elf.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070029#include "elf_builder.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"
Vladimir Marko131980f2015-12-03 18:29:23 +000034#include "linker/buffered_output_stream.h"
35#include "linker/file_output_stream.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>
Vladimir Marko10c13562015-11-25 14:33:36 +000050class ElfWriterQuick FINAL : public ElfWriter {
51 public:
52 ElfWriterQuick(InstructionSet instruction_set,
53 const CompilerOptions* compiler_options,
54 File* elf_file);
55 ~ElfWriterQuick();
56
57 void Start() OVERRIDE;
58 OutputStream* StartRoData() OVERRIDE;
59 void EndRoData(OutputStream* rodata) OVERRIDE;
60 OutputStream* StartText() OVERRIDE;
61 void EndText(OutputStream* text) OVERRIDE;
62 void SetBssSize(size_t bss_size) OVERRIDE;
63 void WriteDynamicSection() OVERRIDE;
64 void WriteDebugInfo(const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) OVERRIDE;
65 void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) OVERRIDE;
66 bool End() OVERRIDE;
67
Vladimir Marko131980f2015-12-03 18:29:23 +000068 virtual OutputStream* GetStream() OVERRIDE;
69
Vladimir Marko10c13562015-11-25 14:33:36 +000070 static void EncodeOatPatches(const std::vector<uintptr_t>& locations,
71 std::vector<uint8_t>* buffer);
72
73 private:
74 const CompilerOptions* const compiler_options_;
75 File* const elf_file_;
76 std::unique_ptr<BufferedOutputStream> output_stream_;
77 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
78
79 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterQuick);
80};
81
82std::unique_ptr<ElfWriter> CreateElfWriterQuick(InstructionSet instruction_set,
83 const CompilerOptions* compiler_options,
84 File* elf_file) {
85 if (Is64BitInstructionSet(instruction_set)) {
86 return MakeUnique<ElfWriterQuick<ElfTypes64>>(instruction_set, compiler_options, elf_file);
87 } else {
88 return MakeUnique<ElfWriterQuick<ElfTypes32>>(instruction_set, compiler_options, elf_file);
89 }
Brian Carlstromb12f3472014-06-11 14:54:46 -070090}
91
David Srbecky533c2072015-04-22 12:20:22 +010092template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +000093ElfWriterQuick<ElfTypes>::ElfWriterQuick(InstructionSet instruction_set,
94 const CompilerOptions* compiler_options,
95 File* elf_file)
96 : ElfWriter(),
97 compiler_options_(compiler_options),
98 elf_file_(elf_file),
99 output_stream_(MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))),
100 builder_(new ElfBuilder<ElfTypes>(instruction_set, output_stream_.get())) {}
Brian Carlstromb12f3472014-06-11 14:54:46 -0700101
Vladimir Marko10c13562015-11-25 14:33:36 +0000102template <typename ElfTypes>
103ElfWriterQuick<ElfTypes>::~ElfWriterQuick() {}
Alex Light78382fa2014-06-06 15:45:32 -0700104
Vladimir Marko10c13562015-11-25 14:33:36 +0000105template <typename ElfTypes>
106void ElfWriterQuick<ElfTypes>::Start() {
107 builder_->Start();
108}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000109
Vladimir Marko10c13562015-11-25 14:33:36 +0000110template <typename ElfTypes>
111OutputStream* ElfWriterQuick<ElfTypes>::StartRoData() {
112 auto* rodata = builder_->GetRoData();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000113 rodata->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000114 return rodata;
115}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000116
Vladimir Marko10c13562015-11-25 14:33:36 +0000117template <typename ElfTypes>
118void ElfWriterQuick<ElfTypes>::EndRoData(OutputStream* rodata) {
119 CHECK_EQ(builder_->GetRoData(), rodata);
120 builder_->GetRoData()->End();
121}
122
123template <typename ElfTypes>
124OutputStream* ElfWriterQuick<ElfTypes>::StartText() {
125 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126 text->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000127 return text;
128}
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129
Vladimir Marko10c13562015-11-25 14:33:36 +0000130template <typename ElfTypes>
131void ElfWriterQuick<ElfTypes>::EndText(OutputStream* text) {
132 CHECK_EQ(builder_->GetText(), text);
133 builder_->GetText()->End();
134}
135
136template <typename ElfTypes>
137void ElfWriterQuick<ElfTypes>::SetBssSize(size_t bss_size) {
138 auto* bss = builder_->GetBss();
139 if (bss_size != 0u) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000140 bss->WriteNoBitsSection(bss_size);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 }
Brian Carlstromb12f3472014-06-11 14:54:46 -0700142}
Mark Mendellae9fd932014-02-10 16:14:35 -0800143
David Srbecky533c2072015-04-22 12:20:22 +0100144template <typename ElfTypes>
Vladimir Marko10c13562015-11-25 14:33:36 +0000145void ElfWriterQuick<ElfTypes>::WriteDynamicSection() {
146 builder_->WriteDynamicSection(elf_file_->GetPath());
147}
148
149template <typename ElfTypes>
150void ElfWriterQuick<ElfTypes>::WriteDebugInfo(
151 const ArrayRef<const dwarf::MethodDebugInfo>& method_infos) {
152 if (compiler_options_->GetGenerateDebugInfo()) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000153 // Generate all the debug information we can.
Tamas Berghammerfffbee42016-01-15 13:09:34 +0000154 dwarf::WriteDebugInfo(builder_.get(), method_infos, kCFIFormat);
Vladimir Marko10c13562015-11-25 14:33:36 +0000155 }
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000156 if (compiler_options_->GetGenerateMiniDebugInfo()) {
157 // Generate only some information and compress it.
158 dwarf::WriteMiniDebugInfo(builder_.get(), method_infos);
159 }
Vladimir Marko10c13562015-11-25 14:33:36 +0000160}
161
162template <typename ElfTypes>
163void ElfWriterQuick<ElfTypes>::WritePatchLocations(
164 const ArrayRef<const uintptr_t>& patch_locations) {
165 // Add relocation section for .text.
166 if (compiler_options_->GetIncludePatchInformation()) {
167 // Note that ElfWriter::Fixup will be called regardless and therefore
168 // we need to include oat_patches for debug sections unconditionally.
169 builder_->WritePatches(".text.oat_patches", patch_locations);
170 }
171}
172
173template <typename ElfTypes>
174bool ElfWriterQuick<ElfTypes>::End() {
175 builder_->End();
176
177 return builder_->Good();
178}
179
180template <typename ElfTypes>
Vladimir Marko131980f2015-12-03 18:29:23 +0000181OutputStream* ElfWriterQuick<ElfTypes>::GetStream() {
182 return builder_->GetStream();
183}
184
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000185// Explicit instantiations
David Srbecky533c2072015-04-22 12:20:22 +0100186template class ElfWriterQuick<ElfTypes32>;
187template class ElfWriterQuick<ElfTypes64>;
Nicolas Geoffrayf9b87b12014-09-02 08:12:09 +0000188
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189} // namespace art