blob: 16d385708677d77d164ef17b5fda2c4c11154df5 [file] [log] [blame]
Tong Shen62d1ca32014-09-03 17:24:56 -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
17#ifndef ART_RUNTIME_ELF_FILE_IMPL_H_
18#define ART_RUNTIME_ELF_FILE_IMPL_H_
19
20#include <map>
21#include <memory>
Andreas Gampe3c54b002015-04-07 16:09:30 -070022#include <type_traits>
Tong Shen62d1ca32014-09-03 17:24:56 -070023#include <vector>
24
Ian Rogersd4c4d952014-10-16 20:31:53 -070025// Explicitly include our own elf.h to avoid Linux and other dependencies.
26#include "./elf.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070027#include "mem_map.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070028
29namespace art {
30
31extern "C" {
32 struct JITCodeEntry;
33}
34
35template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
36 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
37 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
38class ElfFileImpl {
39 public:
Igor Murashkin46774762014-10-22 11:37:02 -070040 static ElfFileImpl* Open(File* file, bool writable, bool program_header_only,
41 std::string* error_msg, uint8_t* requested_base = nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -070042 static ElfFileImpl* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
43 ~ElfFileImpl();
44
45 const File& GetFile() const {
46 return *file_;
47 }
48
Ian Rogers13735952014-10-08 12:43:28 -070049 uint8_t* Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -070050 return map_->Begin();
51 }
52
Ian Rogers13735952014-10-08 12:43:28 -070053 uint8_t* End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -070054 return map_->End();
55 }
56
57 size_t Size() const {
58 return map_->Size();
59 }
60
61 Elf_Ehdr& GetHeader() const;
62
63 Elf_Word GetProgramHeaderNum() const;
64 Elf_Phdr* GetProgramHeader(Elf_Word) const;
65
66 Elf_Word GetSectionHeaderNum() const;
67 Elf_Shdr* GetSectionHeader(Elf_Word) const;
68 Elf_Shdr* FindSectionByType(Elf_Word type) const;
69 Elf_Shdr* FindSectionByName(const std::string& name) const;
70
71 Elf_Shdr* GetSectionNameStringSection() const;
72
73 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Ian Rogers13735952014-10-08 12:43:28 -070074 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070075
76 static bool IsSymbolSectionType(Elf_Word section_type);
77 Elf_Word GetSymbolNum(Elf_Shdr&) const;
78 Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const;
79
80 // Find address of symbol in specified table, returning 0 if it is
81 // not found. See FindSymbolByName for an explanation of build_map.
82 Elf_Addr FindSymbolAddress(Elf_Word section_type,
83 const std::string& symbol_name,
84 bool build_map);
85
86 // Lookup a string given string section and offset. Returns nullptr for
87 // special 0 offset.
88 const char* GetString(Elf_Shdr&, Elf_Word) const;
89
90 Elf_Word GetDynamicNum() const;
91 Elf_Dyn& GetDynamic(Elf_Word) const;
92
93 Elf_Word GetRelNum(Elf_Shdr&) const;
94 Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const;
95
96 Elf_Word GetRelaNum(Elf_Shdr&) const;
97 Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const;
98
99 // Returns the expected size when the file is loaded at runtime
100 size_t GetLoadedSize() const;
101
102 // Load segments into memory based on PT_LOAD program headers.
103 // executable is true at run time, false at compile time.
104 bool Load(bool executable, std::string* error_msg);
105
Andreas Gampe3c54b002015-04-07 16:09:30 -0700106 bool Fixup(Elf_Addr base_address);
107 bool FixupDynamic(Elf_Addr base_address);
108 bool FixupSectionHeaders(Elf_Addr base_address);
109 bool FixupProgramHeaders(Elf_Addr base_address);
110 bool FixupSymbols(Elf_Addr base_address, bool dynamic);
111 bool FixupRelocations(Elf_Addr base_address);
112 bool FixupDebugSections(typename std::make_signed<Elf_Off>::type base_address_delta);
Tong Shen62d1ca32014-09-03 17:24:56 -0700113
114 bool Strip(std::string* error_msg);
115
116 private:
Igor Murashkin46774762014-10-22 11:37:02 -0700117 ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -0700118
119 bool Setup(int prot, int flags, std::string* error_msg);
120
121 bool SetMap(MemMap* map, std::string* error_msg);
122
Ian Rogers13735952014-10-08 12:43:28 -0700123 uint8_t* GetProgramHeadersStart() const;
124 uint8_t* GetSectionHeadersStart() const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700125 Elf_Phdr& GetDynamicProgramHeader() const;
126 Elf_Dyn* GetDynamicSectionStart() const;
127 Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const;
128 const char* GetStringSectionStart(Elf_Word section_type) const;
129 Elf_Rel* GetRelSectionStart(Elf_Shdr&) const;
130 Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const;
131 Elf_Word* GetHashSectionStart() const;
132 Elf_Word GetHashBucketNum() const;
133 Elf_Word GetHashChainNum() const;
134 Elf_Word GetHashBucket(size_t i, bool* ok) const;
135 Elf_Word GetHashChain(size_t i, bool* ok) const;
136
137 typedef std::map<std::string, Elf_Sym*> SymbolTable;
138 SymbolTable** GetSymbolTable(Elf_Word section_type);
139
Ian Rogers13735952014-10-08 12:43:28 -0700140 bool ValidPointer(const uint8_t* start) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700141
142 const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
143
144 // Check that certain sections and their dependencies exist.
145 bool CheckSectionsExist(std::string* error_msg) const;
146
147 // Check that the link of the first section links to the second section.
Ian Rogers13735952014-10-08 12:43:28 -0700148 bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700149
150 // Check whether the offset is in range, and set to target to Begin() + offset if OK.
Ian Rogers13735952014-10-08 12:43:28 -0700151 bool CheckAndSet(Elf32_Off offset, const char* label, uint8_t** target, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700152
153 // Find symbol in specified table, returning nullptr if it is not found.
154 //
155 // If build_map is true, builds a map to speed repeated access. The
156 // map does not included untyped symbol values (aka STT_NOTYPE)
157 // since they can contain duplicates. If build_map is false, the map
158 // will be used if it was already created. Typically build_map
159 // should be set unless only a small number of symbols will be
160 // looked up.
161 Elf_Sym* FindSymbolByName(Elf_Word section_type,
162 const std::string& symbol_name,
163 bool build_map);
164
165 Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const;
166
167 Elf_Dyn* FindDynamicByType(Elf_Sword type) const;
168 Elf_Word FindDynamicValueByType(Elf_Sword type) const;
169
170 // Lookup a string by section type. Returns nullptr for special 0 offset.
171 const char* GetString(Elf_Word section_type, Elf_Word) const;
172
173 const File* const file_;
174 const bool writable_;
175 const bool program_header_only_;
176
177 // ELF header mapping. If program_header_only_ is false, will
178 // actually point to the entire elf file.
179 std::unique_ptr<MemMap> map_;
180 Elf_Ehdr* header_;
181 std::vector<MemMap*> segments_;
182
183 // Pointer to start of first PT_LOAD program segment after Load()
184 // when program_header_only_ is true.
Ian Rogers13735952014-10-08 12:43:28 -0700185 uint8_t* base_address_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700186
187 // The program header should always available but use GetProgramHeadersStart() to be sure.
Ian Rogers13735952014-10-08 12:43:28 -0700188 uint8_t* program_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700189
190 // Conditionally available values. Use accessors to ensure they exist if they are required.
Ian Rogers13735952014-10-08 12:43:28 -0700191 uint8_t* section_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700192 Elf_Phdr* dynamic_program_header_;
193 Elf_Dyn* dynamic_section_start_;
194 Elf_Sym* symtab_section_start_;
195 Elf_Sym* dynsym_section_start_;
196 char* strtab_section_start_;
197 char* dynstr_section_start_;
198 Elf_Word* hash_section_start_;
199
200 SymbolTable* symtab_symbol_table_;
201 SymbolTable* dynsym_symbol_table_;
202
203 // Support for GDB JIT
Ian Rogers13735952014-10-08 12:43:28 -0700204 uint8_t* jit_elf_image_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700205 JITCodeEntry* jit_gdb_entry_;
206 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
207 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel,
208 Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_;
209 void GdbJITSupport();
Tong Shen62d1ca32014-09-03 17:24:56 -0700210
Igor Murashkin46774762014-10-22 11:37:02 -0700211 // Override the 'base' p_vaddr in the first LOAD segment with this value (if non-null).
212 uint8_t* requested_base_;
213
Ian Rogersd4c4d952014-10-16 20:31:53 -0700214 DISALLOW_COPY_AND_ASSIGN(ElfFileImpl);
215};
Tong Shen62d1ca32014-09-03 17:24:56 -0700216
217} // namespace art
218
219#endif // ART_RUNTIME_ELF_FILE_IMPL_H_