blob: b55b60f2dc6c7d652fba2a688405fa13ef06bbe3 [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"
David Sehr79e26072018-04-06 17:58:50 -070027#include "base/mem_map.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070028
29namespace art {
30
David Srbecky533c2072015-04-22 12:20:22 +010031template <typename ElfTypes>
Tong Shen62d1ca32014-09-03 17:24:56 -070032class ElfFileImpl {
33 public:
David Srbecky533c2072015-04-22 12:20:22 +010034 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
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080047 static ElfFileImpl* Open(File* file,
48 bool writable,
49 bool program_header_only,
50 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +010051 /*out*/std::string* error_msg);
Mathieu Chartierbcb6a722016-03-08 16:49:58 -080052 static ElfFileImpl* Open(File* file,
53 int mmap_prot,
54 int mmap_flags,
55 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +010056 /*out*/std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -070057 ~ElfFileImpl();
58
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -070059 const std::string& GetFilePath() const {
60 return file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -070061 }
62
Ian Rogers13735952014-10-08 12:43:28 -070063 uint8_t* Begin() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010064 return map_.Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -070065 }
66
Ian Rogers13735952014-10-08 12:43:28 -070067 uint8_t* End() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010068 return map_.End();
Tong Shen62d1ca32014-09-03 17:24:56 -070069 }
70
71 size_t Size() const {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010072 return map_.Size();
Tong Shen62d1ca32014-09-03 17:24:56 -070073 }
74
75 Elf_Ehdr& GetHeader() const;
76
77 Elf_Word GetProgramHeaderNum() const;
78 Elf_Phdr* GetProgramHeader(Elf_Word) const;
79
80 Elf_Word GetSectionHeaderNum() const;
81 Elf_Shdr* GetSectionHeader(Elf_Word) const;
82 Elf_Shdr* FindSectionByType(Elf_Word type) const;
83 Elf_Shdr* FindSectionByName(const std::string& name) const;
84
85 Elf_Shdr* GetSectionNameStringSection() const;
86
87 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Ian Rogers13735952014-10-08 12:43:28 -070088 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Tong Shen62d1ca32014-09-03 17:24:56 -070089
90 static bool IsSymbolSectionType(Elf_Word section_type);
91 Elf_Word GetSymbolNum(Elf_Shdr&) const;
92 Elf_Sym* GetSymbol(Elf_Word section_type, Elf_Word i) const;
93
94 // Find address of symbol in specified table, returning 0 if it is
95 // not found. See FindSymbolByName for an explanation of build_map.
96 Elf_Addr FindSymbolAddress(Elf_Word section_type,
97 const std::string& symbol_name,
98 bool build_map);
99
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700100 // Lookup a string given string section and offset. Returns null for special 0 offset.
Tong Shen62d1ca32014-09-03 17:24:56 -0700101 const char* GetString(Elf_Shdr&, Elf_Word) const;
102
103 Elf_Word GetDynamicNum() const;
104 Elf_Dyn& GetDynamic(Elf_Word) const;
105
106 Elf_Word GetRelNum(Elf_Shdr&) const;
107 Elf_Rel& GetRel(Elf_Shdr&, Elf_Word) const;
108
109 Elf_Word GetRelaNum(Elf_Shdr&) const;
110 Elf_Rela& GetRela(Elf_Shdr&, Elf_Word) const;
111
Vladimir Marko3fc99032015-05-13 19:06:30 +0100112 // Retrieves the expected size when the file is loaded at runtime. Returns true if successful.
113 bool GetLoadedSize(size_t* size, std::string* error_msg) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700114
115 // Load segments into memory based on PT_LOAD program headers.
116 // executable is true at run time, false at compile time.
Vladimir Markoc09cd052018-08-23 16:36:36 +0100117 bool Load(File* file,
118 bool executable,
119 bool low_4gb,
120 /*inout*/MemMap* reservation,
121 /*out*/std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700122
Andreas Gampe3c54b002015-04-07 16:09:30 -0700123 bool Fixup(Elf_Addr base_address);
124 bool FixupDynamic(Elf_Addr base_address);
125 bool FixupSectionHeaders(Elf_Addr base_address);
126 bool FixupProgramHeaders(Elf_Addr base_address);
127 bool FixupSymbols(Elf_Addr base_address, bool dynamic);
128 bool FixupRelocations(Elf_Addr base_address);
David Srbeckyf8980872015-05-22 17:04:47 +0100129 bool FixupDebugSections(Elf_Addr base_address_delta);
130 bool ApplyOatPatchesTo(const char* target_section_name, Elf_Addr base_address_delta);
131 static void ApplyOatPatches(const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100132 uint8_t* to_patch, const uint8_t* to_patch_end);
Tong Shen62d1ca32014-09-03 17:24:56 -0700133
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700134 bool Strip(File* file, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700135
136 private:
Vladimir Markoc09cd052018-08-23 16:36:36 +0100137 ElfFileImpl(File* file, bool writable, bool program_header_only);
138
139 bool GetLoadedAddressRange(/*out*/uint8_t** vaddr_begin,
140 /*out*/size_t* vaddr_size,
141 /*out*/std::string* error_msg) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700142
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700143 bool Setup(File* file, int prot, int flags, bool low_4gb, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700144
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100145 bool SetMap(File* file, MemMap&& map, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700146
Ian Rogers13735952014-10-08 12:43:28 -0700147 uint8_t* GetProgramHeadersStart() const;
148 uint8_t* GetSectionHeadersStart() const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700149 Elf_Phdr& GetDynamicProgramHeader() const;
150 Elf_Dyn* GetDynamicSectionStart() const;
151 Elf_Sym* GetSymbolSectionStart(Elf_Word section_type) const;
152 const char* GetStringSectionStart(Elf_Word section_type) const;
153 Elf_Rel* GetRelSectionStart(Elf_Shdr&) const;
154 Elf_Rela* GetRelaSectionStart(Elf_Shdr&) const;
155 Elf_Word* GetHashSectionStart() const;
156 Elf_Word GetHashBucketNum() const;
157 Elf_Word GetHashChainNum() const;
158 Elf_Word GetHashBucket(size_t i, bool* ok) const;
159 Elf_Word GetHashChain(size_t i, bool* ok) const;
160
161 typedef std::map<std::string, Elf_Sym*> SymbolTable;
162 SymbolTable** GetSymbolTable(Elf_Word section_type);
163
Ian Rogers13735952014-10-08 12:43:28 -0700164 bool ValidPointer(const uint8_t* start) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700165
166 const Elf_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
167
168 // Check that certain sections and their dependencies exist.
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700169 bool CheckSectionsExist(File* file, std::string* error_msg) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700170
171 // Check that the link of the first section links to the second section.
Ian Rogers13735952014-10-08 12:43:28 -0700172 bool CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const;
Tong Shen62d1ca32014-09-03 17:24:56 -0700173
174 // Check whether the offset is in range, and set to target to Begin() + offset if OK.
Ian Rogers13735952014-10-08 12:43:28 -0700175 bool CheckAndSet(Elf32_Off offset, const char* label, uint8_t** target, std::string* error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -0700176
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700177 // Find symbol in specified table, returning null if it is not found.
Tong Shen62d1ca32014-09-03 17:24:56 -0700178 //
179 // If build_map is true, builds a map to speed repeated access. The
180 // map does not included untyped symbol values (aka STT_NOTYPE)
181 // since they can contain duplicates. If build_map is false, the map
182 // will be used if it was already created. Typically build_map
183 // should be set unless only a small number of symbols will be
184 // looked up.
185 Elf_Sym* FindSymbolByName(Elf_Word section_type,
186 const std::string& symbol_name,
187 bool build_map);
188
189 Elf_Phdr* FindProgamHeaderByType(Elf_Word type) const;
190
191 Elf_Dyn* FindDynamicByType(Elf_Sword type) const;
192 Elf_Word FindDynamicValueByType(Elf_Sword type) const;
193
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700194 // Lookup a string by section type. Returns null for special 0 offset.
Tong Shen62d1ca32014-09-03 17:24:56 -0700195 const char* GetString(Elf_Word section_type, Elf_Word) const;
196
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -0700197 const std::string file_path_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700198 const bool writable_;
199 const bool program_header_only_;
200
201 // ELF header mapping. If program_header_only_ is false, will
202 // actually point to the entire elf file.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100203 MemMap map_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700204 Elf_Ehdr* header_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100205 std::vector<MemMap> segments_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700206
207 // Pointer to start of first PT_LOAD program segment after Load()
208 // when program_header_only_ is true.
Ian Rogers13735952014-10-08 12:43:28 -0700209 uint8_t* base_address_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700210
211 // The program header should always available but use GetProgramHeadersStart() to be sure.
Ian Rogers13735952014-10-08 12:43:28 -0700212 uint8_t* program_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700213
214 // Conditionally available values. Use accessors to ensure they exist if they are required.
Ian Rogers13735952014-10-08 12:43:28 -0700215 uint8_t* section_headers_start_;
Tong Shen62d1ca32014-09-03 17:24:56 -0700216 Elf_Phdr* dynamic_program_header_;
217 Elf_Dyn* dynamic_section_start_;
218 Elf_Sym* symtab_section_start_;
219 Elf_Sym* dynsym_section_start_;
220 char* strtab_section_start_;
221 char* dynstr_section_start_;
222 Elf_Word* hash_section_start_;
223
224 SymbolTable* symtab_symbol_table_;
225 SymbolTable* dynsym_symbol_table_;
226
Ian Rogersd4c4d952014-10-16 20:31:53 -0700227 DISALLOW_COPY_AND_ASSIGN(ElfFileImpl);
228};
Tong Shen62d1ca32014-09-03 17:24:56 -0700229
230} // namespace art
231
232#endif // ART_RUNTIME_ELF_FILE_IMPL_H_