blob: 37c5f9c975a179bfecf300c19ecb7c5a834b4e68 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
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#include "elf_file.h"
18
Tong Shen62d1ca32014-09-03 17:24:56 -070019#include <inttypes.h>
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000020#include <sys/types.h>
21#include <unistd.h>
22
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070027#include "base/unix_file/fd_file.h"
Alex Light3470ab42014-06-18 10:35:45 -070028#include "dwarf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070029#include "elf_file_impl.h"
30#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070031#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032#include "utils.h"
33
34namespace art {
35
Mark Mendellae9fd932014-02-10 16:14:35 -080036// -------------------------------------------------------------------
37// Binary GDB JIT Interface as described in
38// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
39extern "C" {
40 typedef enum {
41 JIT_NOACTION = 0,
42 JIT_REGISTER_FN,
43 JIT_UNREGISTER_FN
44 } JITAction;
45
46 struct JITCodeEntry {
47 JITCodeEntry* next_;
48 JITCodeEntry* prev_;
Ian Rogers13735952014-10-08 12:43:28 -070049 const uint8_t *symfile_addr_;
Mark Mendellae9fd932014-02-10 16:14:35 -080050 uint64_t symfile_size_;
51 };
52
53 struct JITDescriptor {
54 uint32_t version_;
55 uint32_t action_flag_;
56 JITCodeEntry* relevant_entry_;
57 JITCodeEntry* first_entry_;
58 };
59
60 // GDB will place breakpoint into this function.
61 // To prevent GCC from inlining or removing it we place noinline attribute
62 // and inline assembler statement inside.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080063 void __attribute__((noinline)) __jit_debug_register_code();
Mark Mendellae9fd932014-02-10 16:14:35 -080064 void __attribute__((noinline)) __jit_debug_register_code() {
65 __asm__("");
66 }
67
68 // GDB will inspect contents of this descriptor.
69 // Static initialization is necessary to prevent GDB from seeing
70 // uninitialized descriptor.
71 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
72}
73
74
Ian Rogers13735952014-10-08 12:43:28 -070075static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr,
Mark Mendellae9fd932014-02-10 16:14:35 -080076 uintptr_t symfile_size) {
77 JITCodeEntry* entry = new JITCodeEntry;
78 entry->symfile_addr_ = symfile_addr;
79 entry->symfile_size_ = symfile_size;
80 entry->prev_ = nullptr;
81
82 // TODO: Do we need a lock here?
83 entry->next_ = __jit_debug_descriptor.first_entry_;
84 if (entry->next_ != nullptr) {
85 entry->next_->prev_ = entry;
86 }
87 __jit_debug_descriptor.first_entry_ = entry;
88 __jit_debug_descriptor.relevant_entry_ = entry;
89
90 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
91 __jit_debug_register_code();
92 return entry;
93}
94
95
96static void UnregisterCodeEntry(JITCodeEntry* entry) {
97 // TODO: Do we need a lock here?
98 if (entry->prev_ != nullptr) {
99 entry->prev_->next_ = entry->next_;
100 } else {
101 __jit_debug_descriptor.first_entry_ = entry->next_;
102 }
103
104 if (entry->next_ != nullptr) {
105 entry->next_->prev_ = entry->prev_;
106 }
107
108 __jit_debug_descriptor.relevant_entry_ = entry;
109 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
110 __jit_debug_register_code();
111 delete entry;
112}
113
Tong Shen62d1ca32014-09-03 17:24:56 -0700114template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
115 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
116 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
117ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
118 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700119 ::ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base)
Brian Carlstromc1409452014-02-26 14:06:23 -0800120 : file_(file),
121 writable_(writable),
122 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700123 header_(nullptr),
124 base_address_(nullptr),
125 program_headers_start_(nullptr),
126 section_headers_start_(nullptr),
127 dynamic_program_header_(nullptr),
128 dynamic_section_start_(nullptr),
129 symtab_section_start_(nullptr),
130 dynsym_section_start_(nullptr),
131 strtab_section_start_(nullptr),
132 dynstr_section_start_(nullptr),
133 hash_section_start_(nullptr),
134 symtab_symbol_table_(nullptr),
135 dynsym_symbol_table_(nullptr),
136 jit_elf_image_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -0700137 jit_gdb_entry_(nullptr),
138 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -0700139 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800140}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800141
Tong Shen62d1ca32014-09-03 17:24:56 -0700142template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
143 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
144 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
145ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
146 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
147 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
148 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
149 ::Open(File* file, bool writable, bool program_header_only,
Igor Murashkin46774762014-10-22 11:37:02 -0700150 std::string* error_msg, uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700151 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
152 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
153 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
154 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700155 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 int prot;
157 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700158 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 prot = PROT_READ | PROT_WRITE;
160 flags = MAP_SHARED;
161 } else {
162 prot = PROT_READ;
163 flags = MAP_PRIVATE;
164 }
Alex Light3470ab42014-06-18 10:35:45 -0700165 if (!elf_file->Setup(prot, flags, error_msg)) {
166 return nullptr;
167 }
168 return elf_file.release();
169}
170
Tong Shen62d1ca32014-09-03 17:24:56 -0700171template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
172 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
173 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
174ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
175 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
176 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
177 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
178 ::Open(File* file, int prot, int flags, std::string* error_msg) {
179 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
180 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
181 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
182 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700183 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
184 /*requested_base*/nullptr));
Alex Light3470ab42014-06-18 10:35:45 -0700185 if (!elf_file->Setup(prot, flags, error_msg)) {
186 return nullptr;
187 }
188 return elf_file.release();
189}
190
Tong Shen62d1ca32014-09-03 17:24:56 -0700191template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
192 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
193 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
194bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
195 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
196 ::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800197 int64_t temp_file_length = file_->GetLength();
198 if (temp_file_length < 0) {
199 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
201 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800202 return false;
203 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800204 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700205 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800206 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700207 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 return false;
210 }
211
Brian Carlstromc1409452014-02-26 14:06:23 -0800212 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700214 size_t elf_header_size = sizeof(Elf_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800216 file_->GetPath().c_str(), error_msg),
217 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 return false;
219 }
220 // then remap to cover program header
221 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700222 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800223 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700224 "header of %zd bytes: '%s'", file_length,
Tong Shen62d1ca32014-09-03 17:24:56 -0700225 sizeof(Elf_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700226 return false;
227 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800229 file_->GetPath().c_str(), error_msg),
230 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232 return false;
233 }
234 } else {
235 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800237 file_->GetPath().c_str(), error_msg),
238 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800240 return false;
241 }
242 }
243
Andreas Gampedaab38c2014-09-12 18:38:24 -0700244 if (program_header_only_) {
245 program_headers_start_ = Begin() + GetHeader().e_phoff;
246 } else {
247 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
248 return false;
249 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700252 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
253 return false;
254 }
255
256 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700257 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700258 if (shstrtab_section_header == nullptr) {
259 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
260 file_->GetPath().c_str());
261 return false;
262 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263
264 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000265 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700266 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
268 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269 return false;
270 }
271
Andreas Gampedaab38c2014-09-12 18:38:24 -0700272 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700273 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700274 return false;
275 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276
277 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700278 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
279 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700280 if (section_header == nullptr) {
281 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
282 i, file_->GetPath().c_str());
283 return false;
284 }
285 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000286 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700287 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700288 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700289 return false;
290 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800291 break;
292 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000293 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700294 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700295 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700296 return false;
297 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800298 break;
299 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000300 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800301 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700302 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
303 // Check that this is named ".dynstr" and ignore otherwise.
304 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
305 if (strncmp(".dynstr", header_name, 8) == 0) {
306 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700307 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700308 return false;
309 }
310 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800311 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700312 // Check that this is named ".strtab" and ignore otherwise.
313 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
314 if (strncmp(".strtab", header_name, 8) == 0) {
315 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700316 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700317 return false;
318 }
319 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800320 }
321 break;
322 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000323 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700324 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700325 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800326 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800327 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800328 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700329 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800330 return false;
331 }
332 break;
333 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000334 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700335 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700336 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700337 return false;
338 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 break;
340 }
341 }
342 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700343
344 // Check for the existence of some sections.
345 if (!CheckSectionsExist(error_msg)) {
346 return false;
347 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800348 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700349
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800350 return true;
351}
352
Tong Shen62d1ca32014-09-03 17:24:56 -0700353template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
354 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
355 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
356ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
357 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
358 ::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800359 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800360 delete symtab_symbol_table_;
361 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800362 delete jit_elf_image_;
363 if (jit_gdb_entry_) {
364 UnregisterCodeEntry(jit_gdb_entry_);
365 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800366}
367
Tong Shen62d1ca32014-09-03 17:24:56 -0700368template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
369 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
370 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
371bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
372 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
373 ::CheckAndSet(Elf32_Off offset, const char* label,
Ian Rogers13735952014-10-08 12:43:28 -0700374 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700375 if (Begin() + offset >= End()) {
376 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
377 file_->GetPath().c_str());
378 return false;
379 }
380 *target = Begin() + offset;
381 return true;
382}
383
Tong Shen62d1ca32014-09-03 17:24:56 -0700384template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
385 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
386 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
387bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
388 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700389 ::CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700390 // Only works in whole-program mode, as we need to iterate over the sections.
391 // Note that we normally can't search by type, as duplicates are allowed for most section types.
392 if (program_header_only_) {
393 return true;
394 }
395
Tong Shen62d1ca32014-09-03 17:24:56 -0700396 Elf_Shdr* source_section = nullptr;
397 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700398 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700399 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
400 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700401
402 if (Begin() + section_header->sh_offset == source) {
403 // Found the source.
404 source_section = section_header;
405 if (target_index) {
406 break;
407 }
408 } else if (Begin() + section_header->sh_offset == target) {
409 target_index = i;
410 target_found = true;
411 if (source_section != nullptr) {
412 break;
413 }
414 }
415 }
416
417 return target_found && source_section != nullptr && source_section->sh_link == target_index;
418}
419
Tong Shen62d1ca32014-09-03 17:24:56 -0700420template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
421 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
422 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
423bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
424 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
425 ::CheckSectionsExist(std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700426 if (!program_header_only_) {
427 // If in full mode, need section headers.
428 if (section_headers_start_ == nullptr) {
429 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
430 return false;
431 }
432 }
433
434 // This is redundant, but defensive.
435 if (dynamic_program_header_ == nullptr) {
436 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
437 file_->GetPath().c_str());
438 return false;
439 }
440
441 // Need a dynamic section. This is redundant, but defensive.
442 if (dynamic_section_start_ == nullptr) {
443 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
444 file_->GetPath().c_str());
445 return false;
446 }
447
448 // Symtab validation. These is not really a hard failure, as we are currently not using the
449 // symtab internally, but it's nice to be defensive.
450 if (symtab_section_start_ != nullptr) {
451 // When there's a symtab, there should be a strtab.
452 if (strtab_section_start_ == nullptr) {
453 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
454 return false;
455 }
456
457 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700458 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
459 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700460 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
461 file_->GetPath().c_str());
462 return false;
463 }
464 }
465
466 // We always need a dynstr & dynsym.
467 if (dynstr_section_start_ == nullptr) {
468 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
469 return false;
470 }
471 if (dynsym_section_start_ == nullptr) {
472 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
473 return false;
474 }
475
476 // Need a hash section for dynamic symbol lookup.
477 if (hash_section_start_ == nullptr) {
478 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
479 file_->GetPath().c_str());
480 return false;
481 }
482
483 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700484 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
485 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700486 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
487 file_->GetPath().c_str());
488 return false;
489 }
490
491 return true;
492}
493
Tong Shen62d1ca32014-09-03 17:24:56 -0700494template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
495 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
496 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
497bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
498 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
499 ::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700500 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800501 // MemMap::Open should have already set an error.
502 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800503 return false;
504 }
505 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700506 CHECK(map_.get() != nullptr) << file_->GetPath();
507 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800508
Tong Shen62d1ca32014-09-03 17:24:56 -0700509 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000510 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
511 || (ELFMAG1 != header_->e_ident[EI_MAG1])
512 || (ELFMAG2 != header_->e_ident[EI_MAG2])
513 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800514 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
515 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800516 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000517 header_->e_ident[EI_MAG0],
518 header_->e_ident[EI_MAG1],
519 header_->e_ident[EI_MAG2],
520 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800521 return false;
522 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700523 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
524 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800525 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700526 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800527 file_->GetPath().c_str(),
528 header_->e_ident[EI_CLASS]);
529 return false;
530 }
531 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
532 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
533 ELFDATA2LSB,
534 file_->GetPath().c_str(),
535 header_->e_ident[EI_CLASS]);
536 return false;
537 }
538 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
539 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
540 EV_CURRENT,
541 file_->GetPath().c_str(),
542 header_->e_ident[EI_CLASS]);
543 return false;
544 }
545 if (ET_DYN != header_->e_type) {
546 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
547 ET_DYN,
548 file_->GetPath().c_str(),
549 header_->e_type);
550 return false;
551 }
552 if (EV_CURRENT != header_->e_version) {
553 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
554 EV_CURRENT,
555 file_->GetPath().c_str(),
556 header_->e_version);
557 return false;
558 }
559 if (0 != header_->e_entry) {
560 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
561 0,
562 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700563 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800564 return false;
565 }
566 if (0 == header_->e_phoff) {
567 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
568 file_->GetPath().c_str());
569 return false;
570 }
571 if (0 == header_->e_shoff) {
572 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
573 file_->GetPath().c_str());
574 return false;
575 }
576 if (0 == header_->e_ehsize) {
577 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
578 file_->GetPath().c_str());
579 return false;
580 }
581 if (0 == header_->e_phentsize) {
582 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
583 file_->GetPath().c_str());
584 return false;
585 }
586 if (0 == header_->e_phnum) {
587 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
588 file_->GetPath().c_str());
589 return false;
590 }
591 if (0 == header_->e_shentsize) {
592 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
593 file_->GetPath().c_str());
594 return false;
595 }
596 if (0 == header_->e_shnum) {
597 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
598 file_->GetPath().c_str());
599 return false;
600 }
601 if (0 == header_->e_shstrndx) {
602 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
603 file_->GetPath().c_str());
604 return false;
605 }
606 if (header_->e_shstrndx >= header_->e_shnum) {
607 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
608 header_->e_shstrndx,
609 header_->e_shnum,
610 file_->GetPath().c_str());
611 return false;
612 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800613
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800614 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800615 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700616 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
617 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800618 Size(),
619 file_->GetPath().c_str());
620 return false;
621 }
622 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700623 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
624 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800625 Size(),
626 file_->GetPath().c_str());
627 return false;
628 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800629 }
630 return true;
631}
632
Tong Shen62d1ca32014-09-03 17:24:56 -0700633template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
634 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
635 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
636Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
637 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
638 ::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700639 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800640 return *header_;
641}
642
Tong Shen62d1ca32014-09-03 17:24:56 -0700643template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
644 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
645 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700646uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700647 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
648 ::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700649 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
650 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800651 return program_headers_start_;
652}
653
Tong Shen62d1ca32014-09-03 17:24:56 -0700654template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
655 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
656 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700657uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700658 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
659 ::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700660 CHECK(!program_header_only_); // Only used in "full" mode.
661 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800662 return section_headers_start_;
663}
664
Tong Shen62d1ca32014-09-03 17:24:56 -0700665template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
666 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
667 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
668Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
669 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
670 ::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700671 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800672 return *dynamic_program_header_;
673}
674
Tong Shen62d1ca32014-09-03 17:24:56 -0700675template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
676 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
677 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
678Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
679 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
680 ::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700681 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800682 return dynamic_section_start_;
683}
684
Tong Shen62d1ca32014-09-03 17:24:56 -0700685template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
686 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
687 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
688Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
689 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
690 ::GetSymbolSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800691 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000693 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700694 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800695 break;
696 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000697 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700698 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699 break;
700 }
701 default: {
702 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700703 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800704 }
705 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800706}
707
Tong Shen62d1ca32014-09-03 17:24:56 -0700708template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
709 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
710 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
711const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
712 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
713 ::GetStringSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800714 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800715 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000716 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700717 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800718 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000719 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700720 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800721 }
722 default: {
723 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700724 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800725 }
726 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800727}
728
Tong Shen62d1ca32014-09-03 17:24:56 -0700729template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
730 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
731 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
732const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
733 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
734 ::GetString(Elf_Word section_type, Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800735 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
736 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700737 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800738 }
739 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700740 if (string_section_start == nullptr) {
741 return nullptr;
742 }
743 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800744}
745
Andreas Gampedaab38c2014-09-12 18:38:24 -0700746// WARNING: The following methods do not check for an error condition (non-existent hash section).
747// It is the caller's job to do this.
748
Tong Shen62d1ca32014-09-03 17:24:56 -0700749template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
750 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
751 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
752Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
753 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
754 ::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800755 return hash_section_start_;
756}
757
Tong Shen62d1ca32014-09-03 17:24:56 -0700758template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
759 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
760 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
761Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
762 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
763 ::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800764 return GetHashSectionStart()[0];
765}
766
Tong Shen62d1ca32014-09-03 17:24:56 -0700767template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
768 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
769 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
770Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
771 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
772 ::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800773 return GetHashSectionStart()[1];
774}
775
Tong Shen62d1ca32014-09-03 17:24:56 -0700776template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
777 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
778 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
779Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
780 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
781 ::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700782 if (i >= GetHashBucketNum()) {
783 *ok = false;
784 return 0;
785 }
786 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800787 // 0 is nbucket, 1 is nchain
788 return GetHashSectionStart()[2 + i];
789}
790
Tong Shen62d1ca32014-09-03 17:24:56 -0700791template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
792 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
793 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
794Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
795 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
796 ::GetHashChain(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700797 if (i >= GetHashBucketNum()) {
798 *ok = false;
799 return 0;
800 }
801 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800802 // 0 is nbucket, 1 is nchain, & chains are after buckets
803 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
804}
805
Tong Shen62d1ca32014-09-03 17:24:56 -0700806template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
807 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
808 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
809Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
810 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
811 ::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800812 return GetHeader().e_phnum;
813}
814
Tong Shen62d1ca32014-09-03 17:24:56 -0700815template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
816 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
817 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
818Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
819 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
820 ::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700821 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700822 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700823 if (program_header >= End()) {
824 return nullptr; // Failure condition.
825 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700826 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800827}
828
Tong Shen62d1ca32014-09-03 17:24:56 -0700829template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
830 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
831 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
832Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
833 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
834 ::FindProgamHeaderByType(Elf_Word type) const {
835 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
836 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700837 if (program_header->p_type == type) {
838 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800839 }
840 }
Alex Light3470ab42014-06-18 10:35:45 -0700841 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800842}
843
Tong Shen62d1ca32014-09-03 17:24:56 -0700844template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
845 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
846 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
847Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
848 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
849 ::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800850 return GetHeader().e_shnum;
851}
852
Tong Shen62d1ca32014-09-03 17:24:56 -0700853template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
854 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
855 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
856Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
857 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
858 ::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800859 // Can only access arbitrary sections when we have the whole file, not just program header.
860 // Even if we Load(), it doesn't bring in all the sections.
861 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700862 if (i >= GetSectionHeaderNum()) {
863 return nullptr; // Failure condition.
864 }
Ian Rogers13735952014-10-08 12:43:28 -0700865 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700866 if (section_header >= End()) {
867 return nullptr; // Failure condition.
868 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700869 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800870}
871
Tong Shen62d1ca32014-09-03 17:24:56 -0700872template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
873 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
874 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
875Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
876 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
877 ::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800878 // Can only access arbitrary sections when we have the whole file, not just program header.
879 // We could change this to switch on known types if they were detected during loading.
880 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700881 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
882 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700883 if (section_header->sh_type == type) {
884 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800885 }
886 }
Alex Light3470ab42014-06-18 10:35:45 -0700887 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800888}
889
890// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800891static unsigned elfhash(const char *_name) {
892 const unsigned char *name = (const unsigned char *) _name;
893 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800894
Brian Carlstromdf629502013-07-17 22:39:56 -0700895 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800896 h = (h << 4) + *name++;
897 g = h & 0xf0000000;
898 h ^= g;
899 h ^= g >> 24;
900 }
901 return h;
902}
903
Tong Shen62d1ca32014-09-03 17:24:56 -0700904template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
905 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
906 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
907Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
908 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
909 ::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800911}
912
Tong Shen62d1ca32014-09-03 17:24:56 -0700913template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
914 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
915 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700916const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700917 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
918 ::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700919 // Check that we have a hash section.
920 if (GetHashSectionStart() == nullptr) {
921 return nullptr; // Failure condition.
922 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700923 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700924 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700925 // TODO: we need to change this to calculate base_address_ in ::Open,
926 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700927 return base_address_ + sym->st_value;
928 } else {
929 return nullptr;
930 }
931}
932
Andreas Gampedaab38c2014-09-12 18:38:24 -0700933// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
Tong Shen62d1ca32014-09-03 17:24:56 -0700934template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
935 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
936 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
937const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
938 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
939 ::FindDynamicSymbol(const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700940 if (GetHashBucketNum() == 0) {
941 // No dynamic symbols at all.
942 return nullptr;
943 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700944 Elf_Word hash = elfhash(symbol_name.c_str());
945 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700946 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700947 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700948 if (!ok) {
949 return nullptr;
950 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800951 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700952 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700953 if (symbol == nullptr) {
954 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800955 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700956 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
957 if (symbol_name == name) {
958 return symbol;
959 }
960 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
961 if (!ok) {
962 return nullptr;
963 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800964 }
Alex Light3470ab42014-06-18 10:35:45 -0700965 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800966}
967
Tong Shen62d1ca32014-09-03 17:24:56 -0700968template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
969 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
970 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
971bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
972 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
973 ::IsSymbolSectionType(Elf_Word section_type) {
974 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
975}
976
977template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
978 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
979 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
980Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
981 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
982 ::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800983 CHECK(IsSymbolSectionType(section_header.sh_type))
984 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800985 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
986 return section_header.sh_size / section_header.sh_entsize;
987}
988
Tong Shen62d1ca32014-09-03 17:24:56 -0700989template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
990 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
991 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
992Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
993 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
994 ::GetSymbol(Elf_Word section_type,
995 Elf_Word i) const {
996 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700997 if (sym_start == nullptr) {
998 return nullptr;
999 }
1000 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001001}
1002
Tong Shen62d1ca32014-09-03 17:24:56 -07001003template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1004 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1005 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1006typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1007 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1008 ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1009 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1010 ::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001011 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
1012 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001013 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001014 return &symtab_symbol_table_;
1015 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001016 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001017 return &dynsym_symbol_table_;
1018 }
1019 default: {
1020 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -07001021 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001022 }
1023 }
1024}
1025
Tong Shen62d1ca32014-09-03 17:24:56 -07001026template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1027 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1028 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1029Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1030 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1031 ::FindSymbolByName(Elf_Word section_type,
1032 const std::string& symbol_name,
1033 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001034 CHECK(!program_header_only_) << file_->GetPath();
1035 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001036
1037 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -07001038 if (*symbol_table != nullptr || build_map) {
1039 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001040 DCHECK(build_map);
1041 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -07001042 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001043 if (symbol_section == nullptr) {
1044 return nullptr; // Failure condition.
1045 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001046 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001047 if (string_section == nullptr) {
1048 return nullptr; // Failure condition.
1049 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001050 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001051 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001052 if (symbol == nullptr) {
1053 return nullptr; // Failure condition.
1054 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001055 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
1056 ? ELF64_ST_TYPE(symbol->st_info)
1057 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001058 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001059 continue;
1060 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001061 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001062 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001063 continue;
1064 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001065 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -07001066 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -08001067 if (!result.second) {
1068 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001069 if ((symbol->st_value != result.first->second->st_value) ||
1070 (symbol->st_size != result.first->second->st_size) ||
1071 (symbol->st_info != result.first->second->st_info) ||
1072 (symbol->st_other != result.first->second->st_other) ||
1073 (symbol->st_shndx != result.first->second->st_shndx)) {
1074 return nullptr; // Failure condition.
1075 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001076 }
1077 }
1078 }
Alex Light3470ab42014-06-18 10:35:45 -07001079 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001080 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001081 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -07001082 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001083 }
1084 return it->second;
1085 }
1086
1087 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -07001088 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001089 if (symbol_section == nullptr) {
1090 return nullptr;
1091 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001092 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001093 if (string_section == nullptr) {
1094 return nullptr;
1095 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001096 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001097 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001098 if (symbol == nullptr) {
1099 return nullptr; // Failure condition.
1100 }
1101 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001102 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001103 continue;
1104 }
1105 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001106 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001107 }
1108 }
Alex Light3470ab42014-06-18 10:35:45 -07001109 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110}
1111
Tong Shen62d1ca32014-09-03 17:24:56 -07001112template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1113 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1114 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1115Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1116 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1117 ::FindSymbolAddress(Elf_Word section_type,
1118 const std::string& symbol_name,
1119 bool build_map) {
1120 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001121 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001122 return 0;
1123 }
1124 return symbol->st_value;
1125}
1126
Tong Shen62d1ca32014-09-03 17:24:56 -07001127template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1128 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1129 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1130const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1131 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1132 ::GetString(Elf_Shdr& string_section, Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001133 CHECK(!program_header_only_) << file_->GetPath();
1134 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001135 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001136 return nullptr; // Failure condition.
1137 }
1138 if (i >= string_section.sh_size) {
1139 return nullptr;
1140 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001141 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001142 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001143 }
Ian Rogers13735952014-10-08 12:43:28 -07001144 uint8_t* strings = Begin() + string_section.sh_offset;
1145 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001146 if (string >= End()) {
1147 return nullptr;
1148 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001149 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001150}
1151
Tong Shen62d1ca32014-09-03 17:24:56 -07001152template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1153 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1154 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1155Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1156 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1157 ::GetDynamicNum() const {
1158 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001159}
1160
Tong Shen62d1ca32014-09-03 17:24:56 -07001161template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1162 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1163 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1164Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1165 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1166 ::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001167 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1168 return *(GetDynamicSectionStart() + i);
1169}
1170
Tong Shen62d1ca32014-09-03 17:24:56 -07001171template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1172 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1173 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1174Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1175 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1176 ::FindDynamicByType(Elf_Sword type) const {
1177 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1178 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001179 if (dyn->d_tag == type) {
1180 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001181 }
1182 }
Alex Light53cb16b2014-06-12 11:26:29 -07001183 return NULL;
1184}
1185
Tong Shen62d1ca32014-09-03 17:24:56 -07001186template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1187 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1188 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1189Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1190 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1191 ::FindDynamicValueByType(Elf_Sword type) const {
1192 Elf_Dyn* dyn = FindDynamicByType(type);
Alex Light53cb16b2014-06-12 11:26:29 -07001193 if (dyn == NULL) {
1194 return 0;
1195 } else {
1196 return dyn->d_un.d_val;
1197 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001198}
1199
Tong Shen62d1ca32014-09-03 17:24:56 -07001200template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1201 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1202 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1203Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1204 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1205 ::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001206 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001207 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001208}
1209
Tong Shen62d1ca32014-09-03 17:24:56 -07001210template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1211 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1212 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1213Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1214 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1215 ::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001216 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001217 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1218 return section_header.sh_size / section_header.sh_entsize;
1219}
1220
Tong Shen62d1ca32014-09-03 17:24:56 -07001221template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1222 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1223 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1224Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1225 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1226 ::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001227 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001228 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1229 return *(GetRelSectionStart(section_header) + i);
1230}
1231
Tong Shen62d1ca32014-09-03 17:24:56 -07001232template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1233 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1234 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1235Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1236 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1237 ::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001238 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001239 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001240}
1241
Tong Shen62d1ca32014-09-03 17:24:56 -07001242template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1243 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1244 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1245Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1246 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1247 ::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001248 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001249 return section_header.sh_size / section_header.sh_entsize;
1250}
1251
Tong Shen62d1ca32014-09-03 17:24:56 -07001252template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1253 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1254 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1255Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1256 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1257 ::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001258 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001259 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1260 return *(GetRelaSectionStart(section_header) + i);
1261}
1262
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001263// Base on bionic phdr_table_get_load_size
Tong Shen62d1ca32014-09-03 17:24:56 -07001264template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1265 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1266 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1267size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1268 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1269 ::GetLoadedSize() const {
1270 Elf_Addr min_vaddr = 0xFFFFFFFFu;
1271 Elf_Addr max_vaddr = 0x00000000u;
1272 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1273 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001274 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001275 continue;
1276 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001277 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001278 if (begin_vaddr < min_vaddr) {
1279 min_vaddr = begin_vaddr;
1280 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001281 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001282 if (end_vaddr > max_vaddr) {
1283 max_vaddr = end_vaddr;
1284 }
1285 }
1286 min_vaddr = RoundDown(min_vaddr, kPageSize);
1287 max_vaddr = RoundUp(max_vaddr, kPageSize);
1288 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1289 size_t loaded_size = max_vaddr - min_vaddr;
1290 return loaded_size;
1291}
1292
Tong Shen62d1ca32014-09-03 17:24:56 -07001293template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1294 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1295 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1296bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1297 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1298 ::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001299 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001300
1301 if (executable) {
1302 InstructionSet elf_ISA = kNone;
1303 switch (GetHeader().e_machine) {
1304 case EM_ARM: {
1305 elf_ISA = kArm;
1306 break;
1307 }
1308 case EM_AARCH64: {
1309 elf_ISA = kArm64;
1310 break;
1311 }
1312 case EM_386: {
1313 elf_ISA = kX86;
1314 break;
1315 }
1316 case EM_X86_64: {
1317 elf_ISA = kX86_64;
1318 break;
1319 }
1320 case EM_MIPS: {
1321 elf_ISA = kMips;
1322 break;
1323 }
1324 }
1325
1326 if (elf_ISA != kRuntimeISA) {
1327 std::ostringstream oss;
1328 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1329 *error_msg = oss.str();
1330 return false;
1331 }
1332 }
1333
Jim_Guoa62a5882014-04-28 11:11:57 +08001334 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001335 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1336 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001337 if (program_header == nullptr) {
1338 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1339 i, file_->GetPath().c_str());
1340 return false;
1341 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001342
1343 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001344 if (program_header->p_type == PT_DYNAMIC) {
1345 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001346 continue;
1347 }
1348
1349 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001350 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001351 continue;
1352 }
1353
1354 // Found something to load.
1355
Jim_Guoa62a5882014-04-28 11:11:57 +08001356 // Before load the actual segments, reserve a contiguous chunk
1357 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001358 // permissions. We'll then carve that up with the proper
1359 // permissions as we load the actual segments. If p_vaddr is
1360 // non-zero, the segments require the specific address specified,
1361 // which either was specified in the file because we already set
1362 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001363 int64_t temp_file_length = file_->GetLength();
1364 if (temp_file_length < 0) {
1365 errno = -temp_file_length;
1366 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1367 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1368 return false;
1369 }
1370 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001371 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001372 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1373 uint8_t* reserve_base_override = reserve_base;
1374 // Override the base (e.g. when compiling with --compile-pic)
1375 if (requested_base_ != nullptr) {
1376 reserve_base_override = requested_base_;
1377 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001378 std::string reservation_name("ElfFile reservation for ");
1379 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001380 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001381 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001382 GetLoadedSize(), PROT_NONE, false,
1383 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001384 if (reserve.get() == nullptr) {
1385 *error_msg = StringPrintf("Failed to allocate %s: %s",
1386 reservation_name.c_str(), error_msg->c_str());
1387 return false;
1388 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001389 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001390
1391 // Base address is the difference of actual mapped location and the p_vaddr
1392 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1393 - reinterpret_cast<uintptr_t>(reserve_base));
1394 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1395 // dynamic memory address of where that object is actually mapped
1396 //
1397 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1398 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001399 segments_.push_back(reserve.release());
1400 }
1401 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001402 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001403 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001404 }
Ian Rogers13735952014-10-08 12:43:28 -07001405 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001406 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001407 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001408 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001409 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001410 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001411 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001412 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001413 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001414 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001415 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001416 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001417 if (writable_) {
1418 prot |= PROT_WRITE;
1419 flags |= MAP_SHARED;
1420 } else {
1421 flags |= MAP_PRIVATE;
1422 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001423 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001424 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001425 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1426 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001427 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001428 return false;
1429 }
Ian Rogers700a4022014-05-19 16:49:03 -07001430 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001431 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001432 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001433 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001434 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001435 file_->GetPath().c_str(),
1436 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001437 if (segment.get() == nullptr) {
1438 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1439 i, file_->GetPath().c_str(), error_msg->c_str());
1440 return false;
1441 }
1442 if (segment->Begin() != p_vaddr) {
1443 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1444 "instead mapped to %p",
1445 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1446 return false;
1447 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001448 segments_.push_back(segment.release());
1449 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001450
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001451 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001452 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001453 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1454 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1455 file_->GetPath().c_str());
1456 return false;
1457 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001458 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001459
Tong Shen62d1ca32014-09-03 17:24:56 -07001460 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1461 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001462 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001463 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001464 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001465 if (!ValidPointer(d_ptr)) {
1466 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1467 d_ptr, file_->GetPath().c_str());
1468 return false;
1469 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001470 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001471 break;
1472 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001473 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001474 if (!ValidPointer(d_ptr)) {
1475 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1476 d_ptr, file_->GetPath().c_str());
1477 return false;
1478 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001479 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1480 break;
1481 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001482 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001483 if (!ValidPointer(d_ptr)) {
1484 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1485 d_ptr, file_->GetPath().c_str());
1486 return false;
1487 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001488 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001489 break;
1490 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001491 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001492 if (GetDynamicNum() != i+1) {
1493 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1494 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1495 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1496 return false;
1497 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001498 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001499 }
1500 }
1501 }
1502
Andreas Gampedaab38c2014-09-12 18:38:24 -07001503 // Check for the existence of some sections.
1504 if (!CheckSectionsExist(error_msg)) {
1505 return false;
1506 }
1507
Mark Mendellae9fd932014-02-10 16:14:35 -08001508 // Use GDB JIT support to do stack backtrace, etc.
1509 if (executable) {
1510 GdbJITSupport();
1511 }
1512
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001513 return true;
1514}
1515
Tong Shen62d1ca32014-09-03 17:24:56 -07001516template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1517 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1518 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1519bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1520 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001521 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001522 for (size_t i = 0; i < segments_.size(); ++i) {
1523 const MemMap* segment = segments_[i];
1524 if (segment->Begin() <= start && start < segment->End()) {
1525 return true;
1526 }
1527 }
1528 return false;
1529}
1530
Alex Light3470ab42014-06-18 10:35:45 -07001531
Tong Shen62d1ca32014-09-03 17:24:56 -07001532template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1533 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1534 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1535Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1536 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1537 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001538 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001539 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001540 if (shstrtab_sec == nullptr) {
1541 return nullptr;
1542 }
Alex Light3470ab42014-06-18 10:35:45 -07001543 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001544 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001545 if (shdr == nullptr) {
1546 return nullptr;
1547 }
1548 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001549 if (sec_name == nullptr) {
1550 continue;
1551 }
1552 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001553 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001554 }
1555 }
1556 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001557}
1558
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001559struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001560 uint32_t raw_length_;
1561 uint32_t GetLength() {
1562 return raw_length_ + sizeof(raw_length_);
1563 }
1564 uint32_t CIE_pointer;
1565 uint32_t initial_location;
1566 uint32_t address_range;
1567 uint8_t instructions[0];
1568};
1569
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001570static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001571 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001572 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001573 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001574}
1575
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001576static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001577 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001578}
1579
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001580struct PACKED(1) FDE64 {
1581 uint32_t raw_length_;
1582 uint64_t extended_length_;
1583 uint64_t GetLength() {
1584 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1585 }
1586 uint64_t CIE_pointer;
1587 uint64_t initial_location;
1588 uint64_t address_range;
1589 uint8_t instructions[0];
1590};
1591
1592static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001593 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001594 fde_bytes += frame->GetLength();
1595 return reinterpret_cast<FDE64*>(fde_bytes);
1596}
1597
1598static bool IsFDE(FDE64* frame) {
1599 return frame->CIE_pointer != 0;
1600}
1601
1602static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001603 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001604 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1605 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1606 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1607 for (; frame < last_frame; frame = NextFDE(frame)) {
1608 if (!IsFDE(frame)) {
1609 return false;
1610 }
1611 frame->initial_location += base_address_delta;
1612 }
1613 return true;
1614 } else {
1615 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1616 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1617 for (; frame < last_frame; frame = NextFDE(frame)) {
1618 if (!IsFDE(frame)) {
1619 return false;
1620 }
1621 frame->initial_location += base_address_delta;
1622 }
1623 return true;
1624 }
1625}
1626
1627static uint8_t* NextLeb128(uint8_t* current) {
1628 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1629 return current;
1630}
1631
1632struct PACKED(1) DebugLineHeader {
1633 uint32_t unit_length_; // TODO 32-bit specific size
1634 uint16_t version_;
1635 uint32_t header_length_; // TODO 32-bit specific size
1636 uint8_t minimum_instruction_lenght_;
1637 uint8_t maximum_operations_per_instruction_;
1638 uint8_t default_is_stmt_;
1639 int8_t line_base_;
1640 uint8_t line_range_;
1641 uint8_t opcode_base_;
1642 uint8_t remaining_[0];
1643
1644 bool IsStandardOpcode(const uint8_t* op) const {
1645 return *op != 0 && *op < opcode_base_;
1646 }
1647
1648 bool IsExtendedOpcode(const uint8_t* op) const {
1649 return *op == 0;
1650 }
1651
1652 const uint8_t* GetStandardOpcodeLengths() const {
1653 return remaining_;
1654 }
1655
1656 uint8_t* GetNextOpcode(uint8_t* op) const {
1657 if (IsExtendedOpcode(op)) {
1658 uint8_t* length_field = op + 1;
1659 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1660 return length_field + length;
1661 } else if (!IsStandardOpcode(op)) {
1662 return op + 1;
1663 } else if (*op == DW_LNS_fixed_advance_pc) {
1664 return op + 1 + sizeof(uint16_t);
1665 } else {
1666 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1667 op += 1;
1668 for (int i = 0; i < num_args; i++) {
1669 op = NextLeb128(op);
1670 }
1671 return op;
1672 }
1673 }
1674
1675 uint8_t* GetDebugLineData() const {
1676 const uint8_t* hdr_start =
1677 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1678 return const_cast<uint8_t*>(hdr_start + header_length_);
1679 }
1680};
1681
Ian Rogersd4c4d952014-10-16 20:31:53 -07001682class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001683 public:
1684 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1685 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1686 new DebugLineInstructionIterator(header, section_size));
1687 if (line_iter.get() == nullptr) {
1688 return nullptr;
1689 } else {
1690 return line_iter.release();
1691 }
1692 }
1693
1694 ~DebugLineInstructionIterator() {}
1695
1696 bool Next() {
1697 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001698 return false;
1699 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001700 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1701 if (current_instruction_ >= last_instruction_) {
1702 current_instruction_ = nullptr;
1703 return false;
1704 } else {
1705 return true;
1706 }
1707 }
1708
Ian Rogersd4c4d952014-10-16 20:31:53 -07001709 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001710 return current_instruction_;
1711 }
1712
Ian Rogersd4c4d952014-10-16 20:31:53 -07001713 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001714 return header_->IsExtendedOpcode(current_instruction_);
1715 }
1716
1717 uint8_t GetOpcode() {
1718 if (!IsExtendedOpcode()) {
1719 return *current_instruction_;
1720 } else {
1721 uint8_t* len_ptr = current_instruction_ + 1;
1722 return *NextLeb128(len_ptr);
1723 }
1724 }
1725
1726 uint8_t* GetArguments() {
1727 if (!IsExtendedOpcode()) {
1728 return current_instruction_ + 1;
1729 } else {
1730 uint8_t* len_ptr = current_instruction_ + 1;
1731 return NextLeb128(len_ptr) + 1;
1732 }
1733 }
1734
1735 private:
1736 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1737 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1738 current_instruction_(header->GetDebugLineData()) {}
1739
Ian Rogersd4c4d952014-10-16 20:31:53 -07001740 DebugLineHeader* const header_;
1741 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001742 uint8_t* current_instruction_;
1743};
1744
1745static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1746 while (iter->Next()) {
1747 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1748 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1749 }
Alex Light3470ab42014-06-18 10:35:45 -07001750 }
1751 return true;
1752}
1753
1754struct PACKED(1) DebugInfoHeader {
1755 uint32_t unit_length; // TODO 32-bit specific size
1756 uint16_t version;
1757 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1758 uint8_t address_size;
1759};
1760
1761// Returns -1 if it is variable length, which we will just disallow for now.
1762static int32_t FormLength(uint32_t att) {
1763 switch (att) {
1764 case DW_FORM_data1:
1765 case DW_FORM_flag:
1766 case DW_FORM_flag_present:
1767 case DW_FORM_ref1:
1768 return 1;
1769
1770 case DW_FORM_data2:
1771 case DW_FORM_ref2:
1772 return 2;
1773
1774 case DW_FORM_addr: // TODO 32-bit only
1775 case DW_FORM_ref_addr: // TODO 32-bit only
1776 case DW_FORM_sec_offset: // TODO 32-bit only
1777 case DW_FORM_strp: // TODO 32-bit only
1778 case DW_FORM_data4:
1779 case DW_FORM_ref4:
1780 return 4;
1781
1782 case DW_FORM_data8:
1783 case DW_FORM_ref8:
1784 case DW_FORM_ref_sig8:
1785 return 8;
1786
1787 case DW_FORM_block:
1788 case DW_FORM_block1:
1789 case DW_FORM_block2:
1790 case DW_FORM_block4:
1791 case DW_FORM_exprloc:
1792 case DW_FORM_indirect:
1793 case DW_FORM_ref_udata:
1794 case DW_FORM_sdata:
1795 case DW_FORM_string:
1796 case DW_FORM_udata:
1797 default:
1798 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001799 }
1800}
1801
Ian Rogersd4c4d952014-10-16 20:31:53 -07001802class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001803 public:
Alex Light3470ab42014-06-18 10:35:45 -07001804 ~DebugTag() {}
1805 // Creates a new tag and moves data pointer up to the start of the next one.
1806 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001807 static DebugTag* Create(const uint8_t** data_pointer) {
1808 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001809 uint32_t index = DecodeUnsignedLeb128(&data);
1810 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1811 tag->size_ = static_cast<uint32_t>(
1812 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1813 // skip the abbrev
1814 tag->tag_ = DecodeUnsignedLeb128(&data);
1815 tag->has_child_ = (*data == 0);
1816 data++;
1817 while (true) {
1818 uint32_t attr = DecodeUnsignedLeb128(&data);
1819 uint32_t form = DecodeUnsignedLeb128(&data);
1820 if (attr == 0 && form == 0) {
1821 break;
1822 } else if (attr == 0 || form == 0) {
1823 // Bad abbrev.
1824 return nullptr;
1825 }
1826 int32_t size = FormLength(form);
1827 if (size == -1) {
1828 return nullptr;
1829 }
1830 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1831 }
1832 *data_pointer = data;
1833 return tag.release();
1834 }
1835
1836 uint32_t GetSize() const {
1837 return size_;
1838 }
1839
Ian Rogersd4c4d952014-10-16 20:31:53 -07001840 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001841 return has_child_;
1842 }
1843
Ian Rogersd4c4d952014-10-16 20:31:53 -07001844 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001845 return tag_;
1846 }
1847
Ian Rogersd4c4d952014-10-16 20:31:53 -07001848 uint32_t GetIndex() const {
1849 return index_;
1850 }
1851
Alex Light3470ab42014-06-18 10:35:45 -07001852 // Gets the offset of a particular attribute in this tag structure.
1853 // Interpretation of the data is left to the consumer. 0 is returned if the
1854 // tag does not contain the attribute.
1855 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1856 auto it = off_map_.find(dwarf_attribute);
1857 if (it == off_map_.end()) {
1858 return 0;
1859 } else {
1860 return it->second;
1861 }
1862 }
1863
1864 // Gets the size of attribute
1865 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1866 auto it = size_map_.find(dwarf_attribute);
1867 if (it == size_map_.end()) {
1868 return 0;
1869 } else {
1870 return it->second;
1871 }
1872 }
1873
1874 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001875 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001876 void AddAttribute(uint32_t type, uint32_t attr_size) {
1877 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1878 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1879 size_ += attr_size;
1880 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001881
1882 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001883 std::map<uint32_t, uint32_t> off_map_;
1884 std::map<uint32_t, uint32_t> size_map_;
1885 uint32_t size_;
1886 uint32_t tag_;
1887 bool has_child_;
1888};
1889
1890class DebugAbbrev {
1891 public:
1892 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001893 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001894 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1895 if (!abbrev->ReadAtOffset(0)) {
1896 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001897 }
1898 return abbrev.release();
1899 }
1900
Alex Lightd338ae02014-08-13 17:15:38 -07001901 bool ReadAtOffset(uint32_t abbrev_offset) {
1902 tags_.clear();
1903 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001904 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001905 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1906 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1907 if (tag.get() == nullptr) {
1908 return false;
1909 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001910 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001911 tag_list_.push_back(std::move(tag));
1912 }
1913 }
1914 return true;
1915 }
1916
Ian Rogers13735952014-10-08 12:43:28 -07001917 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001918 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1919 auto it = tags_.find(tag_num);
1920 if (it == tags_.end()) {
1921 return nullptr;
1922 } else {
1923 CHECK_GT(tag_list_.size(), it->second);
1924 return tag_list_.at(it->second).get();
1925 }
1926 }
1927
1928 private:
Ian Rogers13735952014-10-08 12:43:28 -07001929 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001930 const uint8_t* const begin_;
1931 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001932 std::map<uint32_t, uint32_t> tags_;
1933 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1934};
1935
1936class DebugInfoIterator {
1937 public:
1938 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1939 DebugAbbrev* abbrev) {
1940 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1941 if (iter->GetCurrentTag() == nullptr) {
1942 return nullptr;
1943 } else {
1944 return iter.release();
1945 }
1946 }
1947 ~DebugInfoIterator() {}
1948
1949 // Moves to the next DIE. Returns false if at last entry.
1950 // TODO Handle variable length attributes.
1951 bool next() {
1952 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1953 return false;
1954 }
Alex Lightd338ae02014-08-13 17:15:38 -07001955 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001956 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001957 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1958 current_cu_ = next_cu_;
1959 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001960 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001961 reread_abbrev = true;
1962 }
Alex Light3470ab42014-06-18 10:35:45 -07001963 if (current_entry_ >= last_entry_) {
1964 current_entry_ = nullptr;
1965 return false;
1966 }
Alex Lightd338ae02014-08-13 17:15:38 -07001967 if (reread_abbrev) {
1968 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1969 }
Alex Light3470ab42014-06-18 10:35:45 -07001970 current_tag_ = abbrev_->ReadTag(current_entry_);
1971 if (current_tag_ == nullptr) {
1972 current_entry_ = nullptr;
1973 return false;
1974 } else {
1975 return true;
1976 }
1977 }
1978
1979 const DebugTag* GetCurrentTag() {
1980 return const_cast<DebugTag*>(current_tag_);
1981 }
Ian Rogers13735952014-10-08 12:43:28 -07001982 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07001983 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1984 return nullptr;
1985 }
1986 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1987 if (off == 0) {
1988 // tag does not have that field.
1989 return nullptr;
1990 } else {
1991 DCHECK_LT(off, current_tag_->GetSize());
1992 return current_entry_ + off;
1993 }
1994 }
1995
1996 private:
Alex Lightd338ae02014-08-13 17:15:38 -07001997 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07001998 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07001999 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
2000 }
2001
Alex Light3470ab42014-06-18 10:35:45 -07002002 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
2003 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07002004 current_cu_(header),
2005 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07002006 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
2007 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07002008 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002009 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002010 DebugInfoHeader* current_cu_;
2011 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002012 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002013 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002014 DebugTag* current_tag_;
2015};
2016
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002017static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002018 do {
2019 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2020 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002021 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002022 return false;
2023 }
2024 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2025 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2026 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002027 *PC_low += base_address_delta;
2028 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002029 }
2030 } while (iter->next());
2031 return true;
2032}
2033
Tong Shen62d1ca32014-09-03 17:24:56 -07002034template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2035 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2036 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2037bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2038 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2039 ::FixupDebugSections(off_t base_address_delta) {
2040 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2041 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2042 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2043 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2044 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2045 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2046 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002047
2048 if (debug_info == nullptr || debug_abbrev == nullptr ||
2049 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2050 // Release version of ART does not generate debug info.
2051 return true;
2052 }
2053 if (base_address_delta == 0) {
2054 return true;
2055 }
2056 if (eh_frame != nullptr &&
2057 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2058 return false;
2059 }
2060
2061 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2062 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002063 if (abbrev.get() == nullptr) {
2064 return false;
2065 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002066 DebugInfoHeader* info_header =
2067 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2068 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2069 debug_info->sh_size,
2070 abbrev.get()));
2071 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002072 return false;
2073 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002074 if (debug_line != nullptr) {
2075 DebugLineHeader* line_header =
2076 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2077 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2078 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2079 if (line_iter.get() == nullptr) {
2080 return false;
2081 }
2082 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2083 return false;
2084 }
2085 }
2086 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002087}
Mark Mendellae9fd932014-02-10 16:14:35 -08002088
Tong Shen62d1ca32014-09-03 17:24:56 -07002089template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2090 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2091 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2092void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2093 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2094 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002095 // We only get here if we only are mapping the program header.
2096 DCHECK(program_header_only_);
2097
2098 // Well, we need the whole file to do this.
2099 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002100 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2101 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002102 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2103 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2104 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2105 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002106 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002107 return;
2108 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002109 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2110 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002111
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002112 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002113 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002114 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002115 return;
2116 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002117
2118 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002119 // We need to add in a strtab and symtab to the image.
2120 // all is MAP_PRIVATE so it can be written to freely.
2121 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002122 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002123 elf_hdr.e_entry = 0;
2124 elf_hdr.e_phoff = 0;
2125 elf_hdr.e_phnum = 0;
2126 elf_hdr.e_phentsize = 0;
2127 elf_hdr.e_type = ET_EXEC;
2128
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002129 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2130 // and the actual address stuff starts at in regular files this is good.
2131 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002132 LOG(ERROR) << "Failed to load GDB data";
2133 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002134 }
2135
Alex Light3470ab42014-06-18 10:35:45 -07002136 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2137 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002138}
2139
Tong Shen62d1ca32014-09-03 17:24:56 -07002140template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2141 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2142 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2143bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2144 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2145 ::Strip(std::string* error_msg) {
2146 // ELF files produced by MCLinker look roughly like this
2147 //
2148 // +------------+
2149 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2150 // +------------+
2151 // | Elf_Phdr | program headers
2152 // | Elf_Phdr |
2153 // | ... |
2154 // | Elf_Phdr |
2155 // +------------+
2156 // | section | mixture of needed and unneeded sections
2157 // +------------+
2158 // | section |
2159 // +------------+
2160 // | ... |
2161 // +------------+
2162 // | section |
2163 // +------------+
2164 // | Elf_Shdr | section headers
2165 // | Elf_Shdr |
2166 // | ... | contains offset to section start
2167 // | Elf_Shdr |
2168 // +------------+
2169 //
2170 // To strip:
2171 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2172 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2173 // - move the sections are keeping up to fill in gaps of sections we want to strip
2174 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2175 // - truncate rest of file
2176 //
2177
2178 std::vector<Elf_Shdr> section_headers;
2179 std::vector<Elf_Word> section_headers_original_indexes;
2180 section_headers.reserve(GetSectionHeaderNum());
2181
2182
2183 Elf_Shdr* string_section = GetSectionNameStringSection();
2184 CHECK(string_section != nullptr);
2185 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2186 Elf_Shdr* sh = GetSectionHeader(i);
2187 CHECK(sh != nullptr);
2188 const char* name = GetString(*string_section, sh->sh_name);
2189 if (name == nullptr) {
2190 CHECK_EQ(0U, i);
2191 section_headers.push_back(*sh);
2192 section_headers_original_indexes.push_back(0);
2193 continue;
2194 }
2195 if (StartsWith(name, ".debug")
2196 || (strcmp(name, ".strtab") == 0)
2197 || (strcmp(name, ".symtab") == 0)) {
2198 continue;
2199 }
2200 section_headers.push_back(*sh);
2201 section_headers_original_indexes.push_back(i);
2202 }
2203 CHECK_NE(0U, section_headers.size());
2204 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2205
2206 // section 0 is the NULL section, sections start at offset of first section
2207 CHECK(GetSectionHeader(1) != nullptr);
2208 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2209 for (size_t i = 1; i < section_headers.size(); i++) {
2210 Elf_Shdr& new_sh = section_headers[i];
2211 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2212 CHECK(old_sh != nullptr);
2213 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2214 if (old_sh->sh_addralign > 1) {
2215 offset = RoundUp(offset, old_sh->sh_addralign);
2216 }
2217 if (old_sh->sh_offset == offset) {
2218 // already in place
2219 offset += old_sh->sh_size;
2220 continue;
2221 }
2222 // shift section earlier
2223 memmove(Begin() + offset,
2224 Begin() + old_sh->sh_offset,
2225 old_sh->sh_size);
2226 new_sh.sh_offset = offset;
2227 offset += old_sh->sh_size;
2228 }
2229
2230 Elf_Off shoff = offset;
2231 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2232 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2233 offset += section_headers_size_in_bytes;
2234
2235 GetHeader().e_shnum = section_headers.size();
2236 GetHeader().e_shoff = shoff;
2237 int result = ftruncate(file_->Fd(), offset);
2238 if (result != 0) {
2239 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2240 file_->GetPath().c_str(), strerror(errno));
2241 return false;
2242 }
2243 return true;
2244}
2245
2246static const bool DEBUG_FIXUP = false;
2247
2248template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2249 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2250 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2251bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2252 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2253 ::Fixup(uintptr_t base_address) {
2254 if (!FixupDynamic(base_address)) {
2255 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2256 return false;
2257 }
2258 if (!FixupSectionHeaders(base_address)) {
2259 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2260 return false;
2261 }
2262 if (!FixupProgramHeaders(base_address)) {
2263 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2264 return false;
2265 }
2266 if (!FixupSymbols(base_address, true)) {
2267 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2268 return false;
2269 }
2270 if (!FixupSymbols(base_address, false)) {
2271 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2272 return false;
2273 }
2274 if (!FixupRelocations(base_address)) {
2275 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2276 return false;
2277 }
2278 if (!FixupDebugSections(base_address)) {
2279 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2280 return false;
2281 }
2282 return true;
2283}
2284
2285template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2286 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2287 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2288bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2289 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2290 ::FixupDynamic(uintptr_t base_address) {
2291 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2292 Elf_Dyn& elf_dyn = GetDynamic(i);
2293 Elf_Word d_tag = elf_dyn.d_tag;
2294 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2295 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2296 if (DEBUG_FIXUP) {
2297 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2298 GetFile().GetPath().c_str(), i,
2299 static_cast<uint64_t>(d_ptr),
2300 static_cast<uint64_t>(d_ptr + base_address));
2301 }
2302 d_ptr += base_address;
2303 elf_dyn.d_un.d_ptr = d_ptr;
2304 }
2305 }
2306 return true;
2307}
2308
2309template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2310 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2311 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2312bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2313 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2314 ::FixupSectionHeaders(uintptr_t base_address) {
2315 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2316 Elf_Shdr* sh = GetSectionHeader(i);
2317 CHECK(sh != nullptr);
2318 // 0 implies that the section will not exist in the memory of the process
2319 if (sh->sh_addr == 0) {
2320 continue;
2321 }
2322 if (DEBUG_FIXUP) {
2323 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2324 GetFile().GetPath().c_str(), i,
2325 static_cast<uint64_t>(sh->sh_addr),
2326 static_cast<uint64_t>(sh->sh_addr + base_address));
2327 }
2328 sh->sh_addr += base_address;
2329 }
2330 return true;
2331}
2332
2333template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2334 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2335 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2336bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2337 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2338 ::FixupProgramHeaders(uintptr_t base_address) {
2339 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2340 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2341 Elf_Phdr* ph = GetProgramHeader(i);
2342 CHECK(ph != nullptr);
2343 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2344 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2345 << GetFile().GetPath() << " i=" << i;
2346 if (DEBUG_FIXUP) {
2347 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2348 GetFile().GetPath().c_str(), i,
2349 static_cast<uint64_t>(ph->p_vaddr),
2350 static_cast<uint64_t>(ph->p_vaddr + base_address));
2351 }
2352 ph->p_vaddr += base_address;
2353 ph->p_paddr += base_address;
2354 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2355 << GetFile().GetPath() << " i=" << i;
2356 }
2357 return true;
2358}
2359
2360template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2361 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2362 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2363bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2364 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2365 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2366 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2367 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2368 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2369 if (symbol_section == nullptr) {
2370 // file is missing optional .symtab
2371 CHECK(!dynamic) << GetFile().GetPath();
2372 return true;
2373 }
2374 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2375 Elf_Sym* symbol = GetSymbol(section_type, i);
2376 CHECK(symbol != nullptr);
2377 if (symbol->st_value != 0) {
2378 if (DEBUG_FIXUP) {
2379 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2380 GetFile().GetPath().c_str(), i,
2381 static_cast<uint64_t>(symbol->st_value),
2382 static_cast<uint64_t>(symbol->st_value + base_address));
2383 }
2384 symbol->st_value += base_address;
2385 }
2386 }
2387 return true;
2388}
2389
2390template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2391 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2392 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2393bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2394 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2395 ::FixupRelocations(uintptr_t base_address) {
2396 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2397 Elf_Shdr* sh = GetSectionHeader(i);
2398 CHECK(sh != nullptr);
2399 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002400 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
2401 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002402 if (DEBUG_FIXUP) {
2403 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002404 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002405 static_cast<uint64_t>(rel.r_offset),
2406 static_cast<uint64_t>(rel.r_offset + base_address));
2407 }
2408 rel.r_offset += base_address;
2409 }
2410 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002411 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
2412 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002413 if (DEBUG_FIXUP) {
2414 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002415 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002416 static_cast<uint64_t>(rela.r_offset),
2417 static_cast<uint64_t>(rela.r_offset + base_address));
2418 }
2419 rela.r_offset += base_address;
2420 }
2421 }
2422 }
2423 return true;
2424}
2425
2426// Explicit instantiations
2427template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2428 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2429template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2430 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2431
Ian Rogersd4c4d952014-10-16 20:31:53 -07002432ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002433}
2434
Ian Rogersd4c4d952014-10-16 20:31:53 -07002435ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002436}
2437
2438ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002439 // Should never have 32 and 64-bit impls.
2440 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002441}
2442
Igor Murashkin46774762014-10-22 11:37:02 -07002443ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2444 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002445 if (file->GetLength() < EI_NIDENT) {
2446 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2447 file->GetPath().c_str());
2448 return nullptr;
2449 }
2450 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2451 file->GetPath().c_str(), error_msg));
2452 if (map == nullptr && map->Size() != EI_NIDENT) {
2453 return nullptr;
2454 }
Ian Rogers13735952014-10-08 12:43:28 -07002455 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002456 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002457 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2458 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002459 if (elf_file_impl == nullptr)
2460 return nullptr;
2461 return new ElfFile(elf_file_impl);
2462 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002463 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2464 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002465 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002466 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002467 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002468 return new ElfFile(elf_file_impl);
2469 } else {
2470 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2471 ELFCLASS32, ELFCLASS64,
2472 file->GetPath().c_str(),
2473 header[EI_CLASS]);
2474 return nullptr;
2475 }
2476}
2477
2478ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2479 if (file->GetLength() < EI_NIDENT) {
2480 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2481 file->GetPath().c_str());
2482 return nullptr;
2483 }
2484 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2485 file->GetPath().c_str(), error_msg));
2486 if (map == nullptr && map->Size() != EI_NIDENT) {
2487 return nullptr;
2488 }
Ian Rogers13735952014-10-08 12:43:28 -07002489 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002490 if (header[EI_CLASS] == ELFCLASS64) {
2491 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002492 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002493 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002494 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002495 return new ElfFile(elf_file_impl);
2496 } else if (header[EI_CLASS] == ELFCLASS32) {
2497 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002498 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002499 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002500 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002501 return new ElfFile(elf_file_impl);
2502 } else {
2503 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2504 ELFCLASS32, ELFCLASS64,
2505 file->GetPath().c_str(),
2506 header[EI_CLASS]);
2507 return nullptr;
2508 }
2509}
2510
2511#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002512 if (elf64_.get() != nullptr) { \
2513 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002514 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002515 DCHECK(elf32_.get() != nullptr); \
2516 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002517 }
2518
2519bool ElfFile::Load(bool executable, std::string* error_msg) {
2520 DELEGATE_TO_IMPL(Load, executable, error_msg);
2521}
2522
Ian Rogers13735952014-10-08 12:43:28 -07002523const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002524 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2525}
2526
2527size_t ElfFile::Size() const {
2528 DELEGATE_TO_IMPL(Size);
2529}
2530
Ian Rogers13735952014-10-08 12:43:28 -07002531uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002532 DELEGATE_TO_IMPL(Begin);
2533}
2534
Ian Rogers13735952014-10-08 12:43:28 -07002535uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002536 DELEGATE_TO_IMPL(End);
2537}
2538
2539const File& ElfFile::GetFile() const {
2540 DELEGATE_TO_IMPL(GetFile);
2541}
2542
2543bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002544 if (elf32_.get() == nullptr) {
2545 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002546
Ian Rogersd4c4d952014-10-16 20:31:53 -07002547 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2548 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002549 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002550 }
2551 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002552 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002553 }
2554 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002555 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002556 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002557 return true;
2558 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002559 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2560 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002561 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002562 }
2563 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002564 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002565 }
2566 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002567 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002568 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002569 return true;
2570 }
2571}
2572
2573uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2574 const std::string& symbol_name,
2575 bool build_map) {
2576 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2577}
2578
2579size_t ElfFile::GetLoadedSize() const {
2580 DELEGATE_TO_IMPL(GetLoadedSize);
2581}
2582
2583bool ElfFile::Strip(File* file, std::string* error_msg) {
2584 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2585 if (elf_file.get() == nullptr) {
2586 return false;
2587 }
2588
Ian Rogersd4c4d952014-10-16 20:31:53 -07002589 if (elf_file->elf64_.get() != nullptr)
2590 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002591 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002592 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002593}
2594
2595bool ElfFile::Fixup(uintptr_t base_address) {
2596 DELEGATE_TO_IMPL(Fixup, base_address);
2597}
2598
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002599} // namespace art