Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 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 | #ifndef ART_SRC_ELF_WRITER_H_ |
| 18 | #define ART_SRC_ELF_WRITER_H_ |
| 19 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 21 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 22 | #include <cstddef> |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 25 | #include <llvm/Support/ELF.h> |
| 26 | |
| 27 | #include "UniquePtr.h" |
| 28 | #include "dex_file.h" |
| 29 | #include "os.h" |
| 30 | |
| 31 | namespace mcld { |
| 32 | class IRBuilder; |
| 33 | class Input; |
| 34 | class LDSection; |
| 35 | class LDSymbol; |
| 36 | class Linker; |
| 37 | class LinkerConfig; |
| 38 | class Module; |
| 39 | } // namespace mcld |
| 40 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 41 | namespace art { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 42 | |
| 43 | class CompiledCode; |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 44 | class CompilerDriver; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 45 | class ElfFile; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 46 | |
| 47 | class ElfWriter { |
| 48 | public: |
| 49 | // Write an ELF file. Returns true on success, false on failure. |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 50 | static bool Create(File* file, |
| 51 | std::vector<uint8_t>& oat_contents, |
| 52 | const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 53 | const std::string& android_root, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 54 | bool is_host, |
| 55 | const CompilerDriver& driver) |
| 56 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 57 | |
| 58 | // Fixup an ELF file so that that oat header will be loaded at oat_begin. |
| 59 | // Returns true on success, false on failure. |
| 60 | static bool Fixup(File* file, uintptr_t oat_data_begin); |
| 61 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 62 | // Strip an ELF file of unneeded debugging information. |
| 63 | // Returns true on success, false on failure. |
| 64 | static bool Strip(File* file); |
| 65 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 66 | // Looks up information about location of oat file in elf file container. |
| 67 | // Used for ImageWriter to perform memory layout. |
| 68 | static void GetOatElfInformation(File* file, |
| 69 | size_t& oat_loaded_size, |
| 70 | size_t& oat_data_offset); |
| 71 | |
| 72 | private: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 73 | ElfWriter(const CompilerDriver& driver, File* elf_file); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 74 | ~ElfWriter(); |
| 75 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 76 | bool Write(std::vector<uint8_t>& oat_contents, |
| 77 | const std::vector<const DexFile*>& dex_files, |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 78 | const std::string& android_root, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 79 | bool is_host) |
| 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 81 | |
| 82 | void Init(); |
| 83 | void AddOatInput(std::vector<uint8_t>& oat_contents); |
| 84 | void AddMethodInputs(const std::vector<const DexFile*>& dex_files); |
| 85 | void AddCompiledCodeInput(const CompiledCode& compiled_code); |
Brian Carlstrom | 3f47c12 | 2013-03-07 00:02:40 -0800 | [diff] [blame] | 86 | void AddRuntimeInputs(const std::string& android_root, bool is_host); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 87 | bool Link(); |
| 88 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 89 | void FixupOatMethodOffsets(const std::vector<const DexFile*>& dex_files) |
| 90 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 91 | uint32_t FixupCompiledCodeOffset(ElfFile& elf_file, |
| 92 | llvm::ELF::Elf32_Addr oatdata_address, |
| 93 | const CompiledCode& compiled_code); |
| 94 | #endif |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 95 | |
| 96 | // Fixup .dynamic d_ptr values for the expected base_address. |
| 97 | static bool FixupDynamic(ElfFile& elf_file, uintptr_t base_address); |
| 98 | |
| 99 | // Fixup Elf32_Shdr p_vaddr to load at the desired address. |
| 100 | static bool FixupSectionHeaders(ElfFile& elf_file,uintptr_t base_address); |
| 101 | |
| 102 | // Fixup Elf32_Phdr p_vaddr to load at the desired address. |
| 103 | static bool FixupProgramHeaders(ElfFile& elf_file,uintptr_t base_address); |
| 104 | |
| 105 | // Fixup symbol table |
| 106 | static bool FixupSymbols(ElfFile& elf_file, uintptr_t base_address, bool dynamic); |
| 107 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 108 | // Fixup dynamic relocations |
| 109 | static bool FixupRelocations(ElfFile& elf_file, uintptr_t base_address); |
| 110 | |
| 111 | // Setup by constructor |
| 112 | const CompilerDriver* compiler_driver_; |
| 113 | File* elf_file_; |
| 114 | |
| 115 | // Setup by Init() |
| 116 | UniquePtr<mcld::LinkerConfig> linker_config_; |
| 117 | UniquePtr<mcld::Module> module_; |
| 118 | UniquePtr<mcld::IRBuilder> ir_builder_; |
| 119 | UniquePtr<mcld::Linker> linker_; |
| 120 | |
| 121 | // Setup by AddOatInput() |
| 122 | // TODO: ownership of oat_input_? |
| 123 | mcld::Input* oat_input_; |
| 124 | |
| 125 | // Setup by AddCompiledCodeInput |
| 126 | // set of symbols for already added mcld::Inputs |
| 127 | SafeMap<const std::string*, const std::string*> added_symbols_; |
| 128 | |
| 129 | // Setup by FixupCompiledCodeOffset |
| 130 | // map of symbol names to oatdata offset |
| 131 | SafeMap<const std::string*, uint32_t> symbol_to_compiled_code_offset_; |
| 132 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | } // namespace art |
| 136 | |
| 137 | #endif // ART_SRC_ELF_WRITER_H_ |