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