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