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_COMPILER_ELF_WRITER_MCLINKER_H_
18#define ART_COMPILER_ELF_WRITER_MCLINKER_H_
19
20#include <memory>
21
22#include "elf_writer.h"
23#include "safe_map.h"
24
25namespace mcld {
26class IRBuilder;
27class Input;
28class LDSection;
29class LDSymbol;
30class Linker;
31class LinkerConfig;
32class LinkerScript;
33class Module;
34}  // namespace mcld
35
36namespace art {
37
38class CompiledCode;
39
40class ElfWriterMclinker FINAL : public ElfWriter {
41 public:
42  // Write an ELF file. Returns true on success, false on failure.
43  static bool Create(File* file,
44                     OatWriter* oat_writer,
45                     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:
52  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
57      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
58
59 private:
60  ElfWriterMclinker(const CompilerDriver& driver, File* elf_file);
61  ~ElfWriterMclinker();
62
63  void Init();
64  void AddOatInput(std::vector<uint8_t>& oat_contents);
65  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);
68  bool Link();
69  void FixupOatMethodOffsets(const std::vector<const DexFile*>& dex_files)
70      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71  uint32_t FixupCompiledCodeOffset(ElfFile& elf_file,
72                                   uint32_t oatdata_address,
73                                   const CompiledCode& compiled_code);
74
75  // Setup by Init()
76  std::unique_ptr<mcld::LinkerConfig> linker_config_;
77  std::unique_ptr<mcld::LinkerScript> linker_script_;
78  std::unique_ptr<mcld::Module> module_;
79  std::unique_ptr<mcld::IRBuilder> ir_builder_;
80  std::unique_ptr<mcld::Linker> linker_;
81
82  // Setup by AddOatInput()
83  // TODO: ownership of oat_input_?
84  mcld::Input* oat_input_;
85
86  // Setup by AddCompiledCodeInput
87  // set of symbols for already added mcld::Inputs
88  SafeMap<const std::string*, const std::string*> added_symbols_;
89
90  // Setup by FixupCompiledCodeOffset
91  // map of symbol names to oatdata offset
92  SafeMap<const std::string*, uint32_t> symbol_to_compiled_code_offset_;
93
94  DISALLOW_IMPLICIT_CONSTRUCTORS(ElfWriterMclinker);
95};
96
97}  // namespace art
98
99#endif  // ART_COMPILER_ELF_WRITER_MCLINKER_H_
100