1 /*
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 
19 #include <unordered_map>
20 #include <unordered_set>
21 
22 #include "base/casts.h"
23 #include "base/logging.h"
24 #include "base/unix_file/fd_file.h"
25 #include "compiled_method.h"
26 #include "dex_file-inl.h"
27 #include "driver/compiler_driver.h"
28 #include "driver/compiler_options.h"
29 #include "elf_builder.h"
30 #include "elf_file.h"
31 #include "elf_utils.h"
32 #include "elf_writer_debug.h"
33 #include "globals.h"
34 #include "leb128.h"
35 #include "oat.h"
36 #include "oat_writer.h"
37 #include "utils.h"
38 
39 namespace art {
40 
41 // .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.
47 constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_EH_FRAME_FORMAT;
48 
49 // The ARM specification defines three special mapping symbols
50 // $a, $t and $d which mark ARM, Thumb and data ranges respectively.
51 // These symbols can be used by tools, for example, to pretty
52 // print instructions correctly.  Objdump will use them if they
53 // exist, but it will still work well without them.
54 // However, these extra symbols take space, so let's just generate
55 // one symbol which marks the whole .text section as code.
56 constexpr bool kGenerateSingleArmMappingSymbol = true;
57 
58 template <typename ElfTypes>
Create(File * elf_file,OatWriter * oat_writer,const std::vector<const DexFile * > & dex_files,const std::string & android_root,bool is_host,const CompilerDriver & driver)59 bool ElfWriterQuick<ElfTypes>::Create(File* elf_file,
60                                       OatWriter* oat_writer,
61                                       const std::vector<const DexFile*>& dex_files,
62                                       const std::string& android_root,
63                                       bool is_host,
64                                       const CompilerDriver& driver) {
65   ElfWriterQuick elf_writer(driver, elf_file);
66   return elf_writer.Write(oat_writer, dex_files, android_root, is_host);
67 }
68 
69 template <typename ElfTypes>
70 static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer);
71 
72 // Encode patch locations as LEB128 list of deltas between consecutive addresses.
73 template <typename ElfTypes>
EncodeOatPatches(const std::vector<uintptr_t> & locations,std::vector<uint8_t> * buffer)74 void ElfWriterQuick<ElfTypes>::EncodeOatPatches(const std::vector<uintptr_t>& locations,
75                                                 std::vector<uint8_t>* buffer) {
76   buffer->reserve(buffer->size() + locations.size() * 2);  // guess 2 bytes per ULEB128.
77   uintptr_t address = 0;  // relative to start of section.
78   for (uintptr_t location : locations) {
79     DCHECK_GE(location, address) << "Patch locations are not in sorted order";
80     EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
81     address = location;
82   }
83 }
84 
85 class RodataWriter FINAL : public CodeOutput {
86  public:
RodataWriter(OatWriter * oat_writer)87   explicit RodataWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
88 
Write(OutputStream * out)89   bool Write(OutputStream* out) OVERRIDE {
90     return oat_writer_->WriteRodata(out);
91   }
92 
93  private:
94   OatWriter* oat_writer_;
95 };
96 
97 class TextWriter FINAL : public CodeOutput {
98  public:
TextWriter(OatWriter * oat_writer)99   explicit TextWriter(OatWriter* oat_writer) : oat_writer_(oat_writer) {}
100 
Write(OutputStream * out)101   bool Write(OutputStream* out) OVERRIDE {
102     return oat_writer_->WriteCode(out);
103   }
104 
105  private:
106   OatWriter* oat_writer_;
107 };
108 
109 enum PatchResult {
110   kAbsoluteAddress,  // Absolute memory location.
111   kPointerRelativeAddress,  // Offset relative to the location of the pointer.
112   kSectionRelativeAddress,  // Offset relative to start of containing section.
113 };
114 
115 // Patch memory addresses within a buffer.
116 // It assumes that the unpatched addresses are offsets relative to base_address.
117 // (which generally means method's low_pc relative to the start of .text)
118 template <typename Elf_Addr, typename Address, PatchResult kPatchResult>
Patch(const std::vector<uintptr_t> & patch_locations,Elf_Addr buffer_address,Elf_Addr base_address,std::vector<uint8_t> * buffer)119 static void Patch(const std::vector<uintptr_t>& patch_locations,
120                   Elf_Addr buffer_address, Elf_Addr base_address,
121                   std::vector<uint8_t>* buffer) {
122   for (uintptr_t location : patch_locations) {
123     typedef __attribute__((__aligned__(1))) Address UnalignedAddress;
124     auto* to_patch = reinterpret_cast<UnalignedAddress*>(buffer->data() + location);
125     switch (kPatchResult) {
126       case kAbsoluteAddress:
127         *to_patch = (base_address + *to_patch);
128         break;
129       case kPointerRelativeAddress:
130         *to_patch = (base_address + *to_patch) - (buffer_address + location);
131         break;
132       case kSectionRelativeAddress:
133         *to_patch = (base_address + *to_patch) - buffer_address;
134         break;
135     }
136   }
137 }
138 
139 template <typename ElfTypes>
Write(OatWriter * oat_writer,const std::vector<const DexFile * > & dex_files_unused ATTRIBUTE_UNUSED,const std::string & android_root_unused ATTRIBUTE_UNUSED,bool is_host_unused ATTRIBUTE_UNUSED)140 bool ElfWriterQuick<ElfTypes>::Write(
141     OatWriter* oat_writer,
142     const std::vector<const DexFile*>& dex_files_unused ATTRIBUTE_UNUSED,
143     const std::string& android_root_unused ATTRIBUTE_UNUSED,
144     bool is_host_unused ATTRIBUTE_UNUSED) {
145   using Elf_Addr = typename ElfTypes::Addr;
146   const InstructionSet isa = compiler_driver_->GetInstructionSet();
147 
148   // Setup the builder with the main OAT sections (.rodata .text .bss).
149   const size_t rodata_size = oat_writer->GetOatHeader().GetExecutableOffset();
150   const size_t text_size = oat_writer->GetSize() - rodata_size;
151   const size_t bss_size = oat_writer->GetBssSize();
152   RodataWriter rodata_writer(oat_writer);
153   TextWriter text_writer(oat_writer);
154   std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(
155       isa, rodata_size, &rodata_writer, text_size, &text_writer, bss_size));
156 
157   // Add debug sections.
158   // They are allocated here (in the same scope as the builder),
159   // but they are registered with the builder only if they are used.
160   using RawSection = typename ElfBuilder<ElfTypes>::RawSection;
161   const auto* text = builder->GetText();
162   const bool is64bit = Is64BitInstructionSet(isa);
163   const int pointer_size = GetInstructionSetPointerSize(isa);
164   std::unique_ptr<RawSection> eh_frame(new RawSection(
165       ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
166       is64bit ? Patch<Elf_Addr, uint64_t, kPointerRelativeAddress> :
167                 Patch<Elf_Addr, uint32_t, kPointerRelativeAddress>,
168       text));
169   std::unique_ptr<RawSection> eh_frame_hdr(new RawSection(
170       ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0,
171       Patch<Elf_Addr, uint32_t, kSectionRelativeAddress>, text));
172   std::unique_ptr<RawSection> debug_frame(new RawSection(
173       ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, pointer_size, 0,
174       is64bit ? Patch<Elf_Addr, uint64_t, kAbsoluteAddress> :
175                 Patch<Elf_Addr, uint32_t, kAbsoluteAddress>,
176       text));
177   std::unique_ptr<RawSection> debug_frame_oat_patches(new RawSection(
178       ".debug_frame.oat_patches", SHT_OAT_PATCH));
179   std::unique_ptr<RawSection> debug_info(new RawSection(
180       ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
181       Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text));
182   std::unique_ptr<RawSection> debug_info_oat_patches(new RawSection(
183       ".debug_info.oat_patches", SHT_OAT_PATCH));
184   std::unique_ptr<RawSection> debug_abbrev(new RawSection(
185       ".debug_abbrev", SHT_PROGBITS));
186   std::unique_ptr<RawSection> debug_str(new RawSection(
187       ".debug_str", SHT_PROGBITS));
188   std::unique_ptr<RawSection> debug_line(new RawSection(
189       ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0,
190       Patch<Elf_Addr, uint32_t, kAbsoluteAddress>, text));
191   std::unique_ptr<RawSection> debug_line_oat_patches(new RawSection(
192       ".debug_line.oat_patches", SHT_OAT_PATCH));
193   if (!oat_writer->GetMethodDebugInfo().empty()) {
194     if (compiler_driver_->GetCompilerOptions().GetGenerateDebugInfo()) {
195       // Generate CFI (stack unwinding information).
196       if (kCFIFormat == dwarf::DW_EH_FRAME_FORMAT) {
197         dwarf::WriteCFISection(
198             compiler_driver_, oat_writer,
199             dwarf::DW_EH_PE_pcrel, kCFIFormat,
200             eh_frame->GetBuffer(), eh_frame->GetPatchLocations(),
201             eh_frame_hdr->GetBuffer(), eh_frame_hdr->GetPatchLocations());
202         builder->RegisterSection(eh_frame.get());
203         builder->RegisterSection(eh_frame_hdr.get());
204       } else {
205         DCHECK(kCFIFormat == dwarf::DW_DEBUG_FRAME_FORMAT);
206         dwarf::WriteCFISection(
207             compiler_driver_, oat_writer,
208             dwarf::DW_EH_PE_absptr, kCFIFormat,
209             debug_frame->GetBuffer(), debug_frame->GetPatchLocations(),
210             nullptr, nullptr);
211         builder->RegisterSection(debug_frame.get());
212         EncodeOatPatches(*debug_frame->GetPatchLocations(),
213                          debug_frame_oat_patches->GetBuffer());
214         builder->RegisterSection(debug_frame_oat_patches.get());
215       }
216       // Add methods to .symtab.
217       WriteDebugSymbols(builder.get(), oat_writer);
218       // Generate DWARF .debug_* sections.
219       dwarf::WriteDebugSections(
220           compiler_driver_, oat_writer,
221           debug_info->GetBuffer(), debug_info->GetPatchLocations(),
222           debug_abbrev->GetBuffer(),
223           debug_str->GetBuffer(),
224           debug_line->GetBuffer(), debug_line->GetPatchLocations());
225       builder->RegisterSection(debug_info.get());
226       EncodeOatPatches(*debug_info->GetPatchLocations(),
227                        debug_info_oat_patches->GetBuffer());
228       builder->RegisterSection(debug_info_oat_patches.get());
229       builder->RegisterSection(debug_abbrev.get());
230       builder->RegisterSection(debug_str.get());
231       builder->RegisterSection(debug_line.get());
232       EncodeOatPatches(*debug_line->GetPatchLocations(),
233                        debug_line_oat_patches->GetBuffer());
234       builder->RegisterSection(debug_line_oat_patches.get());
235     }
236   }
237 
238   // Add relocation section for .text.
239   std::unique_ptr<RawSection> text_oat_patches(new RawSection(
240       ".text.oat_patches", SHT_OAT_PATCH));
241   if (compiler_driver_->GetCompilerOptions().GetIncludePatchInformation()) {
242     // Note that ElfWriter::Fixup will be called regardless and therefore
243     // we need to include oat_patches for debug sections unconditionally.
244     EncodeOatPatches(oat_writer->GetAbsolutePatchLocations(),
245                      text_oat_patches->GetBuffer());
246     builder->RegisterSection(text_oat_patches.get());
247   }
248 
249   return builder->Write(elf_file_);
250 }
251 
252 template <typename ElfTypes>
WriteDebugSymbols(ElfBuilder<ElfTypes> * builder,OatWriter * oat_writer)253 static void WriteDebugSymbols(ElfBuilder<ElfTypes>* builder, OatWriter* oat_writer) {
254   const std::vector<OatWriter::DebugInfo>& method_info = oat_writer->GetMethodDebugInfo();
255   bool generated_mapping_symbol = false;
256 
257   // Find all addresses (low_pc) which contain deduped methods.
258   // The first instance of method is not marked deduped_, but the rest is.
259   std::unordered_set<uint32_t> deduped_addresses;
260   for (auto it = method_info.begin(); it != method_info.end(); ++it) {
261     if (it->deduped_) {
262       deduped_addresses.insert(it->low_pc_);
263     }
264   }
265 
266   auto* symtab = builder->GetSymtab();
267   for (auto it = method_info.begin(); it != method_info.end(); ++it) {
268     if (it->deduped_) {
269       continue;  // Add symbol only for the first instance.
270     }
271     std::string name = PrettyMethod(it->dex_method_index_, *it->dex_file_, true);
272     if (deduped_addresses.find(it->low_pc_) != deduped_addresses.end()) {
273       name += " [DEDUPED]";
274     }
275 
276     uint32_t low_pc = it->low_pc_;
277     // Add in code delta, e.g., thumb bit 0 for Thumb2 code.
278     low_pc += it->compiled_method_->CodeDelta();
279     symtab->AddSymbol(name, builder->GetText(), low_pc,
280                       true, it->high_pc_ - it->low_pc_, STB_GLOBAL, STT_FUNC);
281 
282     // Conforming to aaelf, add $t mapping symbol to indicate start of a sequence of thumb2
283     // instructions, so that disassembler tools can correctly disassemble.
284     // Note that even if we generate just a single mapping symbol, ARM's Streamline
285     // requires it to match function symbol.  Just address 0 does not work.
286     if (it->compiled_method_->GetInstructionSet() == kThumb2) {
287       if (!generated_mapping_symbol || !kGenerateSingleArmMappingSymbol) {
288         symtab->AddSymbol("$t", builder->GetText(), it->low_pc_ & ~1, true,
289                           0, STB_LOCAL, STT_NOTYPE);
290         generated_mapping_symbol = true;
291       }
292     }
293   }
294 }
295 
296 // Explicit instantiations
297 template class ElfWriterQuick<ElfTypes32>;
298 template class ElfWriterQuick<ElfTypes64>;
299 
300 }  // namespace art
301