blob: 489fefb2840b186ba7e7058756aaf4eff54f0224 [file] [log] [blame]
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_ELF_WRITER_MCLINKER_H_
18#define ART_COMPILER_ELF_WRITER_MCLINKER_H_
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070021
Ian Rogers700a4022014-05-19 16:49:03 -070022#include "elf_writer.h"
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070023#include "safe_map.h"
24
25namespace mcld {
26class IRBuilder;
27class Input;
28class LDSection;
29class LDSymbol;
30class Linker;
31class LinkerConfig;
Brian Carlstrom4e3b2842014-01-18 11:26:51 -080032class LinkerScript;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070033class Module;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070034} // namespace mcld
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070035
36namespace art {
37
38class CompiledCode;
39
Ian Rogers3d504072014-03-01 09:16:49 -080040class ElfWriterMclinker FINAL : public ElfWriter {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070041 public:
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070042 // Write an ELF file. Returns true on success, false on failure.
43 static bool Create(File* file,
Ian Rogers3d504072014-03-01 09:16:49 -080044 OatWriter* oat_writer,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070045 const std::vector<const DexFile*>& dex_files,
46 const std::string& android_root,
47 bool is_host,
48 const CompilerDriver& driver)
49 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
50
51 protected:
Ian Rogers3d504072014-03-01 09:16:49 -080052 bool Write(OatWriter* oat_writer,
53 const std::vector<const DexFile*>& dex_files,
54 const std::string& android_root,
55 bool is_host)
56 OVERRIDE
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
58
59 private:
60 ElfWriterMclinker(const CompilerDriver& driver, File* elf_file);
61 ~ElfWriterMclinker();
62
63 void Init();
Vladimir Markof4da6752014-08-01 19:04:18 +010064 mcld::LDSection* AddOatInput(OatWriter* oat_writer, std::vector<uint8_t>* oat_contents);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070065 void AddMethodInputs(const std::vector<const DexFile*>& dex_files);
66 void AddCompiledCodeInput(const CompiledCode& compiled_code);
67 void AddRuntimeInputs(const std::string& android_root, bool is_host);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070068 void FixupOatMethodOffsets(const std::vector<const DexFile*>& dex_files)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70 uint32_t FixupCompiledCodeOffset(ElfFile& elf_file,
Ian Rogers3d504072014-03-01 09:16:49 -080071 uint32_t oatdata_address,
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070072 const CompiledCode& compiled_code);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070073
74 // Setup by Init()
Ian Rogers700a4022014-05-19 16:49:03 -070075 std::unique_ptr<mcld::LinkerConfig> linker_config_;
76 std::unique_ptr<mcld::LinkerScript> linker_script_;
77 std::unique_ptr<mcld::Module> module_;
78 std::unique_ptr<mcld::IRBuilder> ir_builder_;
79 std::unique_ptr<mcld::Linker> linker_;
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070080
81 // Setup by AddOatInput()
82 // TODO: ownership of oat_input_?
83 mcld::Input* oat_input_;
84
85 // Setup by AddCompiledCodeInput
86 // set of symbols for already added mcld::Inputs
87 SafeMap<const std::string*, const std::string*> added_symbols_;
88
89 // Setup by FixupCompiledCodeOffset
90 // map of symbol names to oatdata offset
91 SafeMap<const std::string*, uint32_t> symbol_to_compiled_code_offset_;
92
93 DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterMclinker);
94};
95
96} // namespace art
97
Brian Carlstromfc0e3212013-07-17 14:40:12 -070098#endif // ART_COMPILER_ELF_WRITER_MCLINKER_H_