blob: 383dc41e721d1e5e3d425da56e6fe0c33949d65d [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);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100113 bool ApplyOatPatchesTo(const char* target_section_name,
114 typename std::make_signed<Elf_Off>::type base_address_delta);
115 static bool ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end,
116 const char* target_section_name,
117 typename std::make_signed<Elf_Off>::type delta,
118 uint8_t* to_patch, const uint8_t* to_patch_end);
Tong Shen62d1ca32014-09-03 17:24:56 -0700119
120 bool Strip(std::string* error_msg);
121
122 private:
Igor Murashkin46774762014-10-22 11:37:02 -0700123 ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -0700124
125 bool Setup(int prot, int flags, std::string* error_msg);
126
127 bool SetMap(MemMap* map, std::string* error_msg);
128
Ian Rogers13735952014-10-08 12:43:28 -0700129 uint8_t* GetProgramHeadersStart() const;
130 uint8_t* GetSectionHeadersStart() const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700131 Elf_Phdr& GetDynamicProgramHeader() const;
132 Elf_Dyn* GetDynamicSectionStart() const;
133 Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const;
134 const char* GetStringSectionStart(Elf_Word section_type) const;
135 Elf_Rel* GetRelSectionStart(Elf_Shdr&) const;
136 Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const;
137 Elf_Word* GetHashSectionStart() const;
138 Elf_Word GetHashBucketNum() const;
139 Elf_Word GetHashChainNum() const;
140 Elf_Word GetHashBucket(size_t i, bool* ok) const;
141 Elf_Word GetHashChain(size_t i, bool* ok) const;
142
143 typedef std::map<std::string, Elf_Sym*> SymbolTable;
144 SymbolTable** GetSymbolTable(Elf_Word section_type);
145
Ian Rogers13735952014-10-08 12:43:28 -0700146 bool ValidPointer(const uint8_t* start) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700147
148 const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
149
150 // Check that certain sections and their dependencies exist.
151 bool CheckSectionsExist(std::string* error_msg) const;
152
153 // Check that the link of the first section links to the second section.
Ian Rogers13735952014-10-08 12:43:28 -0700154 bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700155
156 // Check whether the offset is in range, and set to target to Begin() + offset if OK.
Ian Rogers13735952014-10-08 12:43:28 -0700157 bool CheckAndSet(Elf32_Off offset, const char* label, uint8_t** target, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700158
159 // Find symbol in specified table, returning nullptr if it is not found.
160 //
161 // If build_map is true, builds a map to speed repeated access. The
162 // map does not included untyped symbol values (aka STT_NOTYPE)
163 // since they can contain duplicates. If build_map is false, the map
164 // will be used if it was already created. Typically build_map
165 // should be set unless only a small number of symbols will be
166 // looked up.
167 Elf_Sym* FindSymbolByName(Elf_Word section_type,
168 const std::string& symbol_name,
169 bool build_map);
170
171 Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const;
172
173 Elf_Dyn* FindDynamicByType(Elf_Sword type) const;
174 Elf_Word FindDynamicValueByType(Elf_Sword type) const;
175
176 // Lookup a string by section type. Returns nullptr for special 0 offset.
177 const char* GetString(Elf_Word section_type, Elf_Word) const;
178
179 const File* const file_;
180 const bool writable_;
181 const bool program_header_only_;
182
183 // ELF header mapping. If program_header_only_ is false, will
184 // actually point to the entire elf file.
185 std::unique_ptr<MemMap> map_;
186 Elf_Ehdr* header_;
187 std::vector<MemMap*> segments_;
188
189 // Pointer to start of first PT_LOAD program segment after Load()
190 // when program_header_only_ is true.
Ian Rogers13735952014-10-08 12:43:28 -0700191 uint8_t* base_address_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700192
193 // The program header should always available but use GetProgramHeadersStart() to be sure.
Ian Rogers13735952014-10-08 12:43:28 -0700194 uint8_t* program_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700195
196 // Conditionally available values. Use accessors to ensure they exist if they are required.
Ian Rogers13735952014-10-08 12:43:28 -0700197 uint8_t* section_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700198 Elf_Phdr* dynamic_program_header_;
199 Elf_Dyn* dynamic_section_start_;
200 Elf_Sym* symtab_section_start_;
201 Elf_Sym* dynsym_section_start_;
202 char* strtab_section_start_;
203 char* dynstr_section_start_;
204 Elf_Word* hash_section_start_;
205
206 SymbolTable* symtab_symbol_table_;
207 SymbolTable* dynsym_symbol_table_;
208
209 // Support for GDB JIT
Ian Rogers13735952014-10-08 12:43:28 -0700210 uint8_t* jit_elf_image_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700211 JITCodeEntry* jit_gdb_entry_;
212 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
213 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel,
214 Elf_Rela, Elf_Dyn, Elf_Off>> gdb_file_mapping_;
215 void GdbJITSupport();
Tong Shen62d1ca32014-09-03 17:24:56 -0700216
Igor Murashkin46774762014-10-22 11:37:02 -0700217 // Override the 'base' p_vaddr in the first LOAD segment with this value (if non-null).
218 uint8_t* requested_base_;
219
Ian Rogersd4c4d952014-10-16 20:31:53 -0700220 DISALLOW_COPY_AND_ASSIGN(ElfFileImpl);
221};
Tong Shen62d1ca32014-09-03 17:24:56 -0700222
223} // namespace art
224
225#endif // ART_RUNTIME_ELF_FILE_IMPL_H_