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_RUNTIME_ELF_FILE_H_
18 #define ART_RUNTIME_ELF_FILE_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "base/macros.h"
24 #include "base/os.h"
25 #include "elf/elf_utils.h"
26 
27 namespace art {
28 
29 class MemMap;
30 
31 template <typename ElfTypes>
32 class ElfFileImpl;
33 
34 // Explicitly instantiated in elf_file.cc
35 typedef ElfFileImpl<ElfTypes32> ElfFileImpl32;
36 typedef ElfFileImpl<ElfTypes64> ElfFileImpl64;
37 
38 // Used for compile time and runtime for ElfFile access. Because of
39 // the need for use at runtime, cannot directly use LLVM classes such as
40 // ELFObjectFile.
41 class ElfFile {
42  public:
43   static ElfFile* Open(File* file,
44                        bool writable,
45                        bool program_header_only,
46                        bool low_4gb,
47                        /*out*/std::string* error_msg);
48   // Open with specific mmap flags, Always maps in the whole file, not just the
49   // program header sections.
50   static ElfFile* Open(File* file,
51                        int mmap_prot,
52                        int mmap_flags,
53                        /*out*/std::string* error_msg);
54   ~ElfFile();
55 
56   // Load segments into memory based on PT_LOAD program headers
57   bool Load(File* file,
58             bool executable,
59             bool low_4gb,
60             /*inout*/MemMap* reservation,
61             /*out*/std::string* error_msg);
62 
63   const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
64 
65   size_t Size() const;
66 
67   // The start of the memory map address range for this ELF file.
68   uint8_t* Begin() const;
69 
70   // The end of the memory map address range for this ELF file.
71   uint8_t* End() const;
72 
73   const std::string& GetFilePath() const;
74 
75   bool GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) const;
76 
77   bool HasSection(const std::string& name) const;
78 
79   uint64_t FindSymbolAddress(unsigned section_type,
80                              const std::string& symbol_name,
81                              bool build_map);
82 
83   bool GetLoadedSize(size_t* size, std::string* error_msg) const;
84 
85   // Strip an ELF file of unneeded debugging information.
86   // Returns true on success, false on failure.
87   static bool Strip(File* file, std::string* error_msg);
88 
89   // Fixup an ELF file so that that oat header will be loaded at oat_begin.
90   // Returns true on success, false on failure.
91   static bool Fixup(File* file, uint64_t oat_data_begin);
92 
93   bool Fixup(uint64_t base_address);
94 
Is64Bit()95   bool Is64Bit() const {
96     return elf64_.get() != nullptr;
97   }
98 
GetImpl32()99   ElfFileImpl32* GetImpl32() const {
100     return elf32_.get();
101   }
102 
GetImpl64()103   ElfFileImpl64* GetImpl64() const {
104     return elf64_.get();
105   }
106 
107  private:
108   explicit ElfFile(ElfFileImpl32* elf32);
109   explicit ElfFile(ElfFileImpl64* elf64);
110 
111   const std::unique_ptr<ElfFileImpl32> elf32_;
112   const std::unique_ptr<ElfFileImpl64> elf64_;
113 
114   DISALLOW_COPY_AND_ASSIGN(ElfFile);
115 };
116 
117 }  // namespace art
118 
119 #endif  // ART_RUNTIME_ELF_FILE_H_
120