• 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_OAT_ELF_FILE_IMPL_H_
18 #define ART_RUNTIME_OAT_ELF_FILE_IMPL_H_
19 
20 #include <map>
21 #include <memory>
22 #include <type_traits>
23 #include <vector>
24 
25 #include "base/macros.h"
26 #include "base/mem_map.h"
27 #include "elf/elf_utils.h"
28 
29 namespace art HIDDEN {
30 
31 template <typename ElfTypes>
32 class ElfFileImpl {
33  public:
34   using Elf_Addr = typename ElfTypes::Addr;
35   using Elf_Off = typename ElfTypes::Off;
36   using Elf_Half = typename ElfTypes::Half;
37   using Elf_Word = typename ElfTypes::Word;
38   using Elf_Sword = typename ElfTypes::Sword;
39   using Elf_Ehdr = typename ElfTypes::Ehdr;
40   using Elf_Shdr = typename ElfTypes::Shdr;
41   using Elf_Sym = typename ElfTypes::Sym;
42   using Elf_Rel = typename ElfTypes::Rel;
43   using Elf_Rela = typename ElfTypes::Rela;
44   using Elf_Phdr = typename ElfTypes::Phdr;
45   using Elf_Dyn = typename ElfTypes::Dyn;
46 
47   static ElfFileImpl* Open(File* file,
48                            bool writable,
49                            bool program_header_only,
50                            bool low_4gb,
51                            /*out*/std::string* error_msg);
52   static ElfFileImpl* Open(File* file,
53                            int mmap_prot,
54                            int mmap_flags,
55                            bool low_4gb,
56                            /*out*/std::string* error_msg);
57   ~ElfFileImpl();
58 
GetFilePath()59   const std::string& GetFilePath() const {
60     return file_path_;
61   }
62 
GetBaseAddress()63   uint8_t* GetBaseAddress() const {
64     return base_address_;
65   }
66 
Begin()67   uint8_t* Begin() const {
68     return map_.Begin();
69   }
70 
End()71   uint8_t* End() const {
72     return map_.End();
73   }
74 
Size()75   size_t Size() const {
76     return map_.Size();
77   }
78 
79   Elf_Ehdr& GetHeader() const;
80 
81   Elf_Word GetProgramHeaderNum() const;
82   Elf_Phdr* GetProgramHeader(Elf_Word) const;
83 
84   Elf_Word GetSectionHeaderNum() const;
85   Elf_Shdr* GetSectionHeader(Elf_Word) const;
86   Elf_Shdr* FindSectionByType(Elf_Word type) const;
87   Elf_Shdr* FindSectionByName(const std::string& name) const;
88 
89   Elf_Shdr* GetSectionNameStringSection() const;
90 
91   // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
92   const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
93 
94   static bool IsSymbolSectionType(Elf_Word section_type);
95   Elf_Word GetSymbolNum(Elf_Shdr&) const;
96   Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const;
97 
98   // Find address of symbol in specified table, returning 0 if it is
99   // not found. See FindSymbolByName for an explanation of build_map.
100   Elf_Addr FindSymbolAddress(Elf_Word section_type,
101                              const std::string& symbol_name,
102                              bool build_map);
103 
104   // Lookup a string given string section and offset. Returns null for special 0 offset.
105   const char* GetString(Elf_Shdr&, Elf_Word) const;
106 
107   Elf_Word GetDynamicNum() const;
108   Elf_Dyn& GetDynamic(Elf_Word) const;
109 
110   Elf_Word GetRelNum(Elf_Shdr&) const;
111   Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const;
112 
113   Elf_Word GetRelaNum(Elf_Shdr&) const;
114   Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const;
115 
116   // Retrieves the expected size when the file is loaded at runtime. Returns true if successful.
117   bool GetLoadedSize(size_t* size, std::string* error_msg) const;
118 
119   // Get the alignment of the first loadable program segment. Return 0 if no loadable segment found.
120   size_t GetElfSegmentAlignmentFromFile() const;
121 
122   // Load segments into memory based on PT_LOAD program headers.
123   // executable is true at run time, false at compile time.
124   bool Load(File* file,
125             bool executable,
126             bool low_4gb,
127             /*inout*/MemMap* reservation,
128             /*out*/std::string* error_msg);
129 
130   bool Strip(File* file, std::string* error_msg);
131 
132  private:
133   ElfFileImpl(File* file, bool writable, bool program_header_only);
134 
135   bool GetLoadedAddressRange(/*out*/uint8_t** vaddr_begin,
136                              /*out*/size_t* vaddr_size,
137                              /*out*/std::string* error_msg) const;
138 
139   bool Setup(File* file, int prot, int flags, bool low_4gb, std::string* error_msg);
140 
141   bool SetMap(File* file, MemMap&& map, std::string* error_msg);
142 
143   uint8_t* GetProgramHeadersStart() const;
144   uint8_t* GetSectionHeadersStart() const;
145   Elf_Phdr& GetDynamicProgramHeader() const;
146   Elf_Dyn* GetDynamicSectionStart() const;
147   Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const;
148   const char* GetStringSectionStart(Elf_Word section_type) const;
149   Elf_Rel* GetRelSectionStart(Elf_Shdr&) const;
150   Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const;
151   Elf_Word* GetHashSectionStart() const;
152   Elf_Word GetHashBucketNum() const;
153   Elf_Word GetHashChainNum() const;
154   Elf_Word GetHashBucket(size_t i, bool* ok) const;
155   Elf_Word GetHashChain(size_t i, bool* ok) const;
156 
157   using SymbolTable = std::map<std::string, Elf_Sym*>;
158   SymbolTable** GetSymbolTable(Elf_Word section_type);
159 
160   bool ValidPointer(const uint8_t* start) const;
161 
162   const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
163 
164   // Check that certain sections and their dependencies exist.
165   bool CheckSectionsExist(File* file, std::string* error_msg) const;
166 
167   // Check that the link of the first section links to the second section.
168   bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const;
169 
170   // Check whether the offset is in range, and set to target to Begin() + offset if OK.
171   bool CheckAndSet(Elf32_Off offset, const char* label, uint8_t** target, std::string* error_msg);
172 
173   // Find symbol in specified table, returning null if it is not found.
174   //
175   // If build_map is true, builds a map to speed repeated access. The
176   // map does not included untyped symbol values (aka STT_NOTYPE)
177   // since they can contain duplicates. If build_map is false, the map
178   // will be used if it was already created. Typically build_map
179   // should be set unless only a small number of symbols will be
180   // looked up.
181   Elf_Sym* FindSymbolByName(Elf_Word section_type,
182                             const std::string& symbol_name,
183                             bool build_map);
184 
185   Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const;
186 
187   Elf_Dyn* FindDynamicByType(Elf_Sword type) const;
188   Elf_Word FindDynamicValueByType(Elf_Sword type) const;
189 
190   // Lookup a string by section type. Returns null for special 0 offset.
191   const char* GetString(Elf_Word section_type, Elf_Word) const;
192 
193   const std::string file_path_;
194   const bool writable_;
195   const bool program_header_only_;
196 
197   // ELF header mapping. If program_header_only_ is false, will
198   // actually point to the entire elf file.
199   MemMap map_;
200   Elf_Ehdr* header_;
201   std::vector<MemMap> segments_;
202 
203   // Pointer to start of first PT_LOAD program segment after Load()
204   // when program_header_only_ is true.
205   uint8_t* base_address_;
206 
207   // The program header should always available but use GetProgramHeadersStart() to be sure.
208   uint8_t* program_headers_start_;
209 
210   // Conditionally available values. Use accessors to ensure they exist if they are required.
211   uint8_t* section_headers_start_;
212   Elf_Phdr* dynamic_program_header_;
213   Elf_Dyn* dynamic_section_start_;
214   Elf_Sym* symtab_section_start_;
215   Elf_Sym* dynsym_section_start_;
216   char* strtab_section_start_;
217   char* dynstr_section_start_;
218   Elf_Word* hash_section_start_;
219 
220   SymbolTable* symtab_symbol_table_;
221   SymbolTable* dynsym_symbol_table_;
222 
223   DISALLOW_COPY_AND_ASSIGN(ElfFileImpl);
224 };
225 
226 }  // namespace art
227 
228 #endif  // ART_RUNTIME_OAT_ELF_FILE_IMPL_H_
229