blob: 1dd0131b6fdbcfd4b47bbfd798dc8877bb53b113 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
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 Carlstrom265091e2013-01-30 14:08:26 -080020#include <stdint.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080021
Brian Carlstrom265091e2013-01-30 14:08:26 -080022#include <cstddef>
Ian Rogers1212a022013-03-04 10:48:41 -080023#include <vector>
24
Brian Carlstrom265091e2013-01-30 14:08:26 -080025#include <llvm/Support/ELF.h>
26
27#include "UniquePtr.h"
28#include "dex_file.h"
29#include "os.h"
30
31namespace mcld {
32class IRBuilder;
33class Input;
34class LDSection;
35class LDSymbol;
36class Linker;
37class LinkerConfig;
38class Module;
39} // namespace mcld
40
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041namespace art {
Brian Carlstrom265091e2013-01-30 14:08:26 -080042
43class CompiledCode;
Ian Rogers1212a022013-03-04 10:48:41 -080044class CompilerDriver;
Brian Carlstrom265091e2013-01-30 14:08:26 -080045class ElfFile;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046
47class ElfWriter {
48 public:
49 // Write an ELF file. Returns true on success, false on failure.
Brian Carlstrom265091e2013-01-30 14:08:26 -080050 static bool Create(File* file,
51 std::vector<uint8_t>& oat_contents,
52 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom3f47c122013-03-07 00:02:40 -080053 const std::string& android_root,
Brian Carlstrom265091e2013-01-30 14:08:26 -080054 bool is_host,
55 const CompilerDriver& driver)
56 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057
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 Carlstrom265091e2013-01-30 14:08:26 -080062 // Strip an ELF file of unneeded debugging information.
63 // Returns true on success, false on failure.
64 static bool Strip(File* file);
65
Brian Carlstrom700c8d32012-11-05 10:42:02 -080066 // 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 Carlstrom265091e2013-01-30 14:08:26 -080073 ElfWriter(const CompilerDriver& driver, File* elf_file);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080074 ~ElfWriter();
75
Brian Carlstrom265091e2013-01-30 14:08:26 -080076 bool Write(std::vector<uint8_t>& oat_contents,
77 const std::vector<const DexFile*>& dex_files,
Brian Carlstrom3f47c122013-03-07 00:02:40 -080078 const std::string& android_root,
Brian Carlstrom265091e2013-01-30 14:08:26 -080079 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 Carlstrom3f47c122013-03-07 00:02:40 -080086 void AddRuntimeInputs(const std::string& android_root, bool is_host);
Brian Carlstrom265091e2013-01-30 14:08:26 -080087 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 Carlstrom700c8d32012-11-05 10:42:02 -080095
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 Carlstrom265091e2013-01-30 14:08:26 -0800108 // 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 Carlstrom700c8d32012-11-05 10:42:02 -0800133};
134
135} // namespace art
136
137#endif // ART_SRC_ELF_WRITER_H_