blob: 1b91aa64d2b51b9cbd6de361cdf784e01339eba9 [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
Andreas Gampea696c0a2014-12-10 20:51:45 -0800491 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
492 // us). This is usually the last in an oat file, and a good indicator of whether writing was
493 // successful (or the process crashed and left garbage).
494 if (program_header_only_) {
495 // It might not be mapped, but we can compare against the file size.
496 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
497 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
498 if (offset >= file_->GetLength()) {
499 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
500 file_->GetPath().c_str());
501 return false;
502 }
503 }
504
Andreas Gampedaab38c2014-09-12 18:38:24 -0700505 return true;
506}
507
Tong Shen62d1ca32014-09-03 17:24:56 -0700508template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
509 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
510 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
511bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
512 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
513 ::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700514 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800515 // MemMap::Open should have already set an error.
516 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800517 return false;
518 }
519 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700520 CHECK(map_.get() != nullptr) << file_->GetPath();
521 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800522
Tong Shen62d1ca32014-09-03 17:24:56 -0700523 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000524 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
525 || (ELFMAG1 != header_->e_ident[EI_MAG1])
526 || (ELFMAG2 != header_->e_ident[EI_MAG2])
527 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800528 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
529 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800530 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000531 header_->e_ident[EI_MAG0],
532 header_->e_ident[EI_MAG1],
533 header_->e_ident[EI_MAG2],
534 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800535 return false;
536 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700537 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
538 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800539 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700540 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800541 file_->GetPath().c_str(),
542 header_->e_ident[EI_CLASS]);
543 return false;
544 }
545 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
546 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
547 ELFDATA2LSB,
548 file_->GetPath().c_str(),
549 header_->e_ident[EI_CLASS]);
550 return false;
551 }
552 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
553 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
554 EV_CURRENT,
555 file_->GetPath().c_str(),
556 header_->e_ident[EI_CLASS]);
557 return false;
558 }
559 if (ET_DYN != header_->e_type) {
560 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
561 ET_DYN,
562 file_->GetPath().c_str(),
563 header_->e_type);
564 return false;
565 }
566 if (EV_CURRENT != header_->e_version) {
567 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
568 EV_CURRENT,
569 file_->GetPath().c_str(),
570 header_->e_version);
571 return false;
572 }
573 if (0 != header_->e_entry) {
574 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
575 0,
576 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700577 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800578 return false;
579 }
580 if (0 == header_->e_phoff) {
581 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
582 file_->GetPath().c_str());
583 return false;
584 }
585 if (0 == header_->e_shoff) {
586 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
587 file_->GetPath().c_str());
588 return false;
589 }
590 if (0 == header_->e_ehsize) {
591 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
592 file_->GetPath().c_str());
593 return false;
594 }
595 if (0 == header_->e_phentsize) {
596 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
597 file_->GetPath().c_str());
598 return false;
599 }
600 if (0 == header_->e_phnum) {
601 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
602 file_->GetPath().c_str());
603 return false;
604 }
605 if (0 == header_->e_shentsize) {
606 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
607 file_->GetPath().c_str());
608 return false;
609 }
610 if (0 == header_->e_shnum) {
611 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
612 file_->GetPath().c_str());
613 return false;
614 }
615 if (0 == header_->e_shstrndx) {
616 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
617 file_->GetPath().c_str());
618 return false;
619 }
620 if (header_->e_shstrndx >= header_->e_shnum) {
621 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
622 header_->e_shstrndx,
623 header_->e_shnum,
624 file_->GetPath().c_str());
625 return false;
626 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800627
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800628 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800629 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700630 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
631 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800632 Size(),
633 file_->GetPath().c_str());
634 return false;
635 }
636 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700637 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
638 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800639 Size(),
640 file_->GetPath().c_str());
641 return false;
642 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800643 }
644 return true;
645}
646
Tong Shen62d1ca32014-09-03 17:24:56 -0700647template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
648 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
649 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
650Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
651 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
652 ::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700653 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800654 return *header_;
655}
656
Tong Shen62d1ca32014-09-03 17:24:56 -0700657template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
658 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
659 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700660uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700661 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
662 ::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700663 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
664 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800665 return program_headers_start_;
666}
667
Tong Shen62d1ca32014-09-03 17:24:56 -0700668template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
669 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
670 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700671uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700672 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
673 ::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700674 CHECK(!program_header_only_); // Only used in "full" mode.
675 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800676 return section_headers_start_;
677}
678
Tong Shen62d1ca32014-09-03 17:24:56 -0700679template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
680 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
681 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
682Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
683 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
684 ::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700685 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800686 return *dynamic_program_header_;
687}
688
Tong Shen62d1ca32014-09-03 17:24:56 -0700689template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
690 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
691 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
692Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
693 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
694 ::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700695 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800696 return dynamic_section_start_;
697}
698
Tong Shen62d1ca32014-09-03 17:24:56 -0700699template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
700 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
701 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
702Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
703 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
704 ::GetSymbolSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800705 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800706 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000707 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700708 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800709 break;
710 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000711 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700712 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800713 break;
714 }
715 default: {
716 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700717 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800718 }
719 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800720}
721
Tong Shen62d1ca32014-09-03 17:24:56 -0700722template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
723 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
724 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
725const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
726 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
727 ::GetStringSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800728 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800729 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000730 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700731 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800732 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000733 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700734 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800735 }
736 default: {
737 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700738 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800739 }
740 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800741}
742
Tong Shen62d1ca32014-09-03 17:24:56 -0700743template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
744 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
745 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
746const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
747 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
748 ::GetString(Elf_Word section_type, Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800749 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
750 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700751 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800752 }
753 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700754 if (string_section_start == nullptr) {
755 return nullptr;
756 }
757 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758}
759
Andreas Gampedaab38c2014-09-12 18:38:24 -0700760// WARNING: The following methods do not check for an error condition (non-existent hash section).
761// It is the caller's job to do this.
762
Tong Shen62d1ca32014-09-03 17:24:56 -0700763template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
764 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
765 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
766Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
767 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
768 ::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800769 return hash_section_start_;
770}
771
Tong Shen62d1ca32014-09-03 17:24:56 -0700772template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
773 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
774 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
775Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
776 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
777 ::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800778 return GetHashSectionStart()[0];
779}
780
Tong Shen62d1ca32014-09-03 17:24:56 -0700781template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
782 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
783 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
784Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
785 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
786 ::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800787 return GetHashSectionStart()[1];
788}
789
Tong Shen62d1ca32014-09-03 17:24:56 -0700790template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
791 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
792 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
793Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
794 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
795 ::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700796 if (i >= GetHashBucketNum()) {
797 *ok = false;
798 return 0;
799 }
800 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800801 // 0 is nbucket, 1 is nchain
802 return GetHashSectionStart()[2 + i];
803}
804
Tong Shen62d1ca32014-09-03 17:24:56 -0700805template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
806 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
807 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
808Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
809 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
810 ::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600811 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700812 *ok = false;
813 return 0;
814 }
815 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800816 // 0 is nbucket, 1 is nchain, & chains are after buckets
817 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
818}
819
Tong Shen62d1ca32014-09-03 17:24:56 -0700820template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
821 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
822 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
823Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
824 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
825 ::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800826 return GetHeader().e_phnum;
827}
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 ::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700835 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700836 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700837 if (program_header >= End()) {
838 return nullptr; // Failure condition.
839 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700840 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800841}
842
Tong Shen62d1ca32014-09-03 17:24:56 -0700843template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
844 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
845 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
846Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
847 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
848 ::FindProgamHeaderByType(Elf_Word type) const {
849 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
850 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700851 if (program_header->p_type == type) {
852 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800853 }
854 }
Alex Light3470ab42014-06-18 10:35:45 -0700855 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800856}
857
Tong Shen62d1ca32014-09-03 17:24:56 -0700858template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
859 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
860 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
861Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
862 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
863 ::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800864 return GetHeader().e_shnum;
865}
866
Tong Shen62d1ca32014-09-03 17:24:56 -0700867template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
868 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
869 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
870Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
871 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
872 ::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800873 // Can only access arbitrary sections when we have the whole file, not just program header.
874 // Even if we Load(), it doesn't bring in all the sections.
875 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700876 if (i >= GetSectionHeaderNum()) {
877 return nullptr; // Failure condition.
878 }
Ian Rogers13735952014-10-08 12:43:28 -0700879 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700880 if (section_header >= End()) {
881 return nullptr; // Failure condition.
882 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700883 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884}
885
Tong Shen62d1ca32014-09-03 17:24:56 -0700886template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
887 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
888 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
889Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
890 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
891 ::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800892 // Can only access arbitrary sections when we have the whole file, not just program header.
893 // We could change this to switch on known types if they were detected during loading.
894 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700895 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
896 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700897 if (section_header->sh_type == type) {
898 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800899 }
900 }
Alex Light3470ab42014-06-18 10:35:45 -0700901 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800902}
903
904// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800905static unsigned elfhash(const char *_name) {
906 const unsigned char *name = (const unsigned char *) _name;
907 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800908
Brian Carlstromdf629502013-07-17 22:39:56 -0700909 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 h = (h << 4) + *name++;
911 g = h & 0xf0000000;
912 h ^= g;
913 h ^= g >> 24;
914 }
915 return h;
916}
917
Tong Shen62d1ca32014-09-03 17:24:56 -0700918template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
919 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
920 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
921Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
922 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
923 ::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800924 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800925}
926
Tong Shen62d1ca32014-09-03 17:24:56 -0700927template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
928 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
929 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700930const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700931 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
932 ::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700933 // Check that we have a hash section.
934 if (GetHashSectionStart() == nullptr) {
935 return nullptr; // Failure condition.
936 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700937 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700938 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700939 // TODO: we need to change this to calculate base_address_ in ::Open,
940 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700941 return base_address_ + sym->st_value;
942 } else {
943 return nullptr;
944 }
945}
946
Andreas Gampedaab38c2014-09-12 18:38:24 -0700947// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
Tong Shen62d1ca32014-09-03 17:24:56 -0700948template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
949 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
950 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
951const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
952 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
953 ::FindDynamicSymbol(const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700954 if (GetHashBucketNum() == 0) {
955 // No dynamic symbols at all.
956 return nullptr;
957 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700958 Elf_Word hash = elfhash(symbol_name.c_str());
959 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700960 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700961 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700962 if (!ok) {
963 return nullptr;
964 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800965 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700966 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700967 if (symbol == nullptr) {
968 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700970 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
971 if (symbol_name == name) {
972 return symbol;
973 }
974 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
975 if (!ok) {
976 return nullptr;
977 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800978 }
Alex Light3470ab42014-06-18 10:35:45 -0700979 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800980}
981
Tong Shen62d1ca32014-09-03 17:24:56 -0700982template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
983 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
984 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
985bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
986 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
987 ::IsSymbolSectionType(Elf_Word section_type) {
988 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
989}
990
991template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
992 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
993 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
994Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
995 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
996 ::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800997 CHECK(IsSymbolSectionType(section_header.sh_type))
998 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800999 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1000 return section_header.sh_size / section_header.sh_entsize;
1001}
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>
1006Elf_Sym* 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 ::GetSymbol(Elf_Word section_type,
1009 Elf_Word i) const {
1010 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001011 if (sym_start == nullptr) {
1012 return nullptr;
1013 }
1014 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001015}
1016
Tong Shen62d1ca32014-09-03 17:24:56 -07001017template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1018 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1019 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1020typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1021 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1022 ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1023 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1024 ::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001025 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
1026 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001027 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001028 return &symtab_symbol_table_;
1029 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001030 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001031 return &dynsym_symbol_table_;
1032 }
1033 default: {
1034 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -07001035 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001036 }
1037 }
1038}
1039
Tong Shen62d1ca32014-09-03 17:24:56 -07001040template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1041 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1042 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1043Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1044 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1045 ::FindSymbolByName(Elf_Word section_type,
1046 const std::string& symbol_name,
1047 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001048 CHECK(!program_header_only_) << file_->GetPath();
1049 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001050
1051 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -07001052 if (*symbol_table != nullptr || build_map) {
1053 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001054 DCHECK(build_map);
1055 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -07001056 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001057 if (symbol_section == nullptr) {
1058 return nullptr; // Failure condition.
1059 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001060 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001061 if (string_section == nullptr) {
1062 return nullptr; // Failure condition.
1063 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001064 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001065 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001066 if (symbol == nullptr) {
1067 return nullptr; // Failure condition.
1068 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001069 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
1070 ? ELF64_ST_TYPE(symbol->st_info)
1071 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001072 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001073 continue;
1074 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001075 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001076 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001077 continue;
1078 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001079 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -07001080 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -08001081 if (!result.second) {
1082 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001083 if ((symbol->st_value != result.first->second->st_value) ||
1084 (symbol->st_size != result.first->second->st_size) ||
1085 (symbol->st_info != result.first->second->st_info) ||
1086 (symbol->st_other != result.first->second->st_other) ||
1087 (symbol->st_shndx != result.first->second->st_shndx)) {
1088 return nullptr; // Failure condition.
1089 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001090 }
1091 }
1092 }
Alex Light3470ab42014-06-18 10:35:45 -07001093 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001094 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001095 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -07001096 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001097 }
1098 return it->second;
1099 }
1100
1101 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -07001102 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001103 if (symbol_section == nullptr) {
1104 return nullptr;
1105 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001106 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001107 if (string_section == nullptr) {
1108 return nullptr;
1109 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001111 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001112 if (symbol == nullptr) {
1113 return nullptr; // Failure condition.
1114 }
1115 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001116 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001117 continue;
1118 }
1119 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001120 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001121 }
1122 }
Alex Light3470ab42014-06-18 10:35:45 -07001123 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001124}
1125
Tong Shen62d1ca32014-09-03 17:24:56 -07001126template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1127 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1128 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1129Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1130 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1131 ::FindSymbolAddress(Elf_Word section_type,
1132 const std::string& symbol_name,
1133 bool build_map) {
1134 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001135 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001136 return 0;
1137 }
1138 return symbol->st_value;
1139}
1140
Tong Shen62d1ca32014-09-03 17:24:56 -07001141template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1142 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1143 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1144const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1145 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1146 ::GetString(Elf_Shdr& string_section, Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001147 CHECK(!program_header_only_) << file_->GetPath();
1148 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001149 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001150 return nullptr; // Failure condition.
1151 }
1152 if (i >= string_section.sh_size) {
1153 return nullptr;
1154 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001155 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001156 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001157 }
Ian Rogers13735952014-10-08 12:43:28 -07001158 uint8_t* strings = Begin() + string_section.sh_offset;
1159 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001160 if (string >= End()) {
1161 return nullptr;
1162 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001163 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001164}
1165
Tong Shen62d1ca32014-09-03 17:24:56 -07001166template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1167 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1168 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1169Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1170 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1171 ::GetDynamicNum() const {
1172 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001173}
1174
Tong Shen62d1ca32014-09-03 17:24:56 -07001175template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1176 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1177 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1178Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1179 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1180 ::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001181 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1182 return *(GetDynamicSectionStart() + i);
1183}
1184
Tong Shen62d1ca32014-09-03 17:24:56 -07001185template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1186 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1187 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1188Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1189 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1190 ::FindDynamicByType(Elf_Sword type) const {
1191 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1192 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001193 if (dyn->d_tag == type) {
1194 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001195 }
1196 }
Alex Light53cb16b2014-06-12 11:26:29 -07001197 return NULL;
1198}
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_Word 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 ::FindDynamicValueByType(Elf_Sword type) const {
1206 Elf_Dyn* dyn = FindDynamicByType(type);
Alex Light53cb16b2014-06-12 11:26:29 -07001207 if (dyn == NULL) {
1208 return 0;
1209 } else {
1210 return dyn->d_un.d_val;
1211 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001212}
1213
Tong Shen62d1ca32014-09-03 17:24:56 -07001214template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1215 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1216 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1217Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1218 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1219 ::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001220 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001221 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001222}
1223
Tong Shen62d1ca32014-09-03 17:24:56 -07001224template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1225 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1226 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1227Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1228 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1229 ::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001230 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001231 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1232 return section_header.sh_size / section_header.sh_entsize;
1233}
1234
Tong Shen62d1ca32014-09-03 17:24:56 -07001235template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1236 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1237 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1238Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1239 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1240 ::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001241 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001242 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1243 return *(GetRelSectionStart(section_header) + i);
1244}
1245
Tong Shen62d1ca32014-09-03 17:24:56 -07001246template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1247 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1248 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1249Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1250 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1251 ::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001252 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001253 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001254}
1255
Tong Shen62d1ca32014-09-03 17:24:56 -07001256template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1257 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1258 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1259Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1260 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1261 ::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001262 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001263 return section_header.sh_size / section_header.sh_entsize;
1264}
1265
Tong Shen62d1ca32014-09-03 17:24:56 -07001266template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1267 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1268 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1269Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1270 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1271 ::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001272 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001273 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1274 return *(GetRelaSectionStart(section_header) + i);
1275}
1276
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001277// Base on bionic phdr_table_get_load_size
Tong Shen62d1ca32014-09-03 17:24:56 -07001278template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1279 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1280 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1281size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1282 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1283 ::GetLoadedSize() const {
1284 Elf_Addr min_vaddr = 0xFFFFFFFFu;
1285 Elf_Addr max_vaddr = 0x00000000u;
1286 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1287 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001288 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001289 continue;
1290 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001291 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001292 if (begin_vaddr < min_vaddr) {
1293 min_vaddr = begin_vaddr;
1294 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001295 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001296 if (end_vaddr > max_vaddr) {
1297 max_vaddr = end_vaddr;
1298 }
1299 }
1300 min_vaddr = RoundDown(min_vaddr, kPageSize);
1301 max_vaddr = RoundUp(max_vaddr, kPageSize);
1302 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1303 size_t loaded_size = max_vaddr - min_vaddr;
1304 return loaded_size;
1305}
1306
Tong Shen62d1ca32014-09-03 17:24:56 -07001307template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1308 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1309 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1310bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1311 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1312 ::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001313 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001314
1315 if (executable) {
1316 InstructionSet elf_ISA = kNone;
1317 switch (GetHeader().e_machine) {
1318 case EM_ARM: {
1319 elf_ISA = kArm;
1320 break;
1321 }
1322 case EM_AARCH64: {
1323 elf_ISA = kArm64;
1324 break;
1325 }
1326 case EM_386: {
1327 elf_ISA = kX86;
1328 break;
1329 }
1330 case EM_X86_64: {
1331 elf_ISA = kX86_64;
1332 break;
1333 }
1334 case EM_MIPS: {
Andreas Gampec5a3ea72015-01-13 16:41:53 -08001335 if ((GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 ||
1336 (GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) {
1337 elf_ISA = kMips;
Andreas Gampe57b34292015-01-14 15:45:59 -08001338 } else if ((GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6) {
1339 elf_ISA = kMips64;
Andreas Gampec5a3ea72015-01-13 16:41:53 -08001340 }
Andreas Gampe91268c12014-04-03 17:50:24 -07001341 break;
1342 }
1343 }
1344
1345 if (elf_ISA != kRuntimeISA) {
1346 std::ostringstream oss;
1347 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1348 *error_msg = oss.str();
1349 return false;
1350 }
1351 }
1352
Jim_Guoa62a5882014-04-28 11:11:57 +08001353 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001354 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1355 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001356 if (program_header == nullptr) {
1357 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1358 i, file_->GetPath().c_str());
1359 return false;
1360 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001361
1362 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001363 if (program_header->p_type == PT_DYNAMIC) {
1364 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001365 continue;
1366 }
1367
1368 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001369 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001370 continue;
1371 }
1372
1373 // Found something to load.
1374
Jim_Guoa62a5882014-04-28 11:11:57 +08001375 // Before load the actual segments, reserve a contiguous chunk
1376 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001377 // permissions. We'll then carve that up with the proper
1378 // permissions as we load the actual segments. If p_vaddr is
1379 // non-zero, the segments require the specific address specified,
1380 // which either was specified in the file because we already set
1381 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001382 int64_t temp_file_length = file_->GetLength();
1383 if (temp_file_length < 0) {
1384 errno = -temp_file_length;
1385 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1386 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1387 return false;
1388 }
1389 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001390 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001391 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1392 uint8_t* reserve_base_override = reserve_base;
1393 // Override the base (e.g. when compiling with --compile-pic)
1394 if (requested_base_ != nullptr) {
1395 reserve_base_override = requested_base_;
1396 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001397 std::string reservation_name("ElfFile reservation for ");
1398 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001399 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001400 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001401 GetLoadedSize(), PROT_NONE, false,
1402 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001403 if (reserve.get() == nullptr) {
1404 *error_msg = StringPrintf("Failed to allocate %s: %s",
1405 reservation_name.c_str(), error_msg->c_str());
1406 return false;
1407 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001408 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001409
1410 // Base address is the difference of actual mapped location and the p_vaddr
1411 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1412 - reinterpret_cast<uintptr_t>(reserve_base));
1413 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1414 // dynamic memory address of where that object is actually mapped
1415 //
1416 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1417 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001418 segments_.push_back(reserve.release());
1419 }
1420 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001421 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001422 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001423 }
Ian Rogers13735952014-10-08 12:43:28 -07001424 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001425 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001426 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001427 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001428 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001429 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001430 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001431 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001432 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001433 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001434 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001435 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001436 if (writable_) {
1437 prot |= PROT_WRITE;
1438 flags |= MAP_SHARED;
1439 } else {
1440 flags |= MAP_PRIVATE;
1441 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001442 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001443 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001444 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1445 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001446 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001447 return false;
1448 }
Ian Rogers700a4022014-05-19 16:49:03 -07001449 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001450 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001451 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001452 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001453 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001454 file_->GetPath().c_str(),
1455 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001456 if (segment.get() == nullptr) {
1457 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1458 i, file_->GetPath().c_str(), error_msg->c_str());
1459 return false;
1460 }
1461 if (segment->Begin() != p_vaddr) {
1462 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1463 "instead mapped to %p",
1464 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1465 return false;
1466 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001467 segments_.push_back(segment.release());
1468 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001469
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001470 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001471 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001472 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1473 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1474 file_->GetPath().c_str());
1475 return false;
1476 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001477 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001478
Tong Shen62d1ca32014-09-03 17:24:56 -07001479 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1480 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001481 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001482 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001483 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001484 if (!ValidPointer(d_ptr)) {
1485 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1486 d_ptr, file_->GetPath().c_str());
1487 return false;
1488 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001489 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001490 break;
1491 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001492 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001493 if (!ValidPointer(d_ptr)) {
1494 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1495 d_ptr, file_->GetPath().c_str());
1496 return false;
1497 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001498 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1499 break;
1500 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001501 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001502 if (!ValidPointer(d_ptr)) {
1503 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1504 d_ptr, file_->GetPath().c_str());
1505 return false;
1506 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001507 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001508 break;
1509 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001510 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001511 if (GetDynamicNum() != i+1) {
1512 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1513 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1514 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1515 return false;
1516 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001517 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001518 }
1519 }
1520 }
1521
Andreas Gampedaab38c2014-09-12 18:38:24 -07001522 // Check for the existence of some sections.
1523 if (!CheckSectionsExist(error_msg)) {
1524 return false;
1525 }
1526
Mark Mendellae9fd932014-02-10 16:14:35 -08001527 // Use GDB JIT support to do stack backtrace, etc.
1528 if (executable) {
1529 GdbJITSupport();
1530 }
1531
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001532 return true;
1533}
1534
Tong Shen62d1ca32014-09-03 17:24:56 -07001535template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1536 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1537 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1538bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1539 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001540 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001541 for (size_t i = 0; i < segments_.size(); ++i) {
1542 const MemMap* segment = segments_[i];
1543 if (segment->Begin() <= start && start < segment->End()) {
1544 return true;
1545 }
1546 }
1547 return false;
1548}
1549
Alex Light3470ab42014-06-18 10:35:45 -07001550
Tong Shen62d1ca32014-09-03 17:24:56 -07001551template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1552 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1553 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1554Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1555 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1556 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001557 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001558 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001559 if (shstrtab_sec == nullptr) {
1560 return nullptr;
1561 }
Alex Light3470ab42014-06-18 10:35:45 -07001562 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001563 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001564 if (shdr == nullptr) {
1565 return nullptr;
1566 }
1567 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001568 if (sec_name == nullptr) {
1569 continue;
1570 }
1571 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001572 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001573 }
1574 }
1575 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001576}
1577
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001578struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001579 uint32_t raw_length_;
1580 uint32_t GetLength() {
1581 return raw_length_ + sizeof(raw_length_);
1582 }
1583 uint32_t CIE_pointer;
1584 uint32_t initial_location;
1585 uint32_t address_range;
1586 uint8_t instructions[0];
1587};
1588
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001589static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001590 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001591 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001592 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001593}
1594
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001595static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001596 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001597}
1598
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001599struct PACKED(1) FDE64 {
1600 uint32_t raw_length_;
1601 uint64_t extended_length_;
1602 uint64_t GetLength() {
1603 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1604 }
1605 uint64_t CIE_pointer;
1606 uint64_t initial_location;
1607 uint64_t address_range;
1608 uint8_t instructions[0];
1609};
1610
1611static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001612 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001613 fde_bytes += frame->GetLength();
1614 return reinterpret_cast<FDE64*>(fde_bytes);
1615}
1616
1617static bool IsFDE(FDE64* frame) {
1618 return frame->CIE_pointer != 0;
1619}
1620
1621static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001622 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001623 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1624 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1625 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1626 for (; frame < last_frame; frame = NextFDE(frame)) {
1627 if (!IsFDE(frame)) {
1628 return false;
1629 }
1630 frame->initial_location += base_address_delta;
1631 }
1632 return true;
1633 } else {
1634 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1635 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1636 for (; frame < last_frame; frame = NextFDE(frame)) {
1637 if (!IsFDE(frame)) {
1638 return false;
1639 }
1640 frame->initial_location += base_address_delta;
1641 }
1642 return true;
1643 }
1644}
1645
1646static uint8_t* NextLeb128(uint8_t* current) {
1647 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1648 return current;
1649}
1650
1651struct PACKED(1) DebugLineHeader {
1652 uint32_t unit_length_; // TODO 32-bit specific size
1653 uint16_t version_;
1654 uint32_t header_length_; // TODO 32-bit specific size
1655 uint8_t minimum_instruction_lenght_;
1656 uint8_t maximum_operations_per_instruction_;
1657 uint8_t default_is_stmt_;
1658 int8_t line_base_;
1659 uint8_t line_range_;
1660 uint8_t opcode_base_;
1661 uint8_t remaining_[0];
1662
1663 bool IsStandardOpcode(const uint8_t* op) const {
1664 return *op != 0 && *op < opcode_base_;
1665 }
1666
1667 bool IsExtendedOpcode(const uint8_t* op) const {
1668 return *op == 0;
1669 }
1670
1671 const uint8_t* GetStandardOpcodeLengths() const {
1672 return remaining_;
1673 }
1674
1675 uint8_t* GetNextOpcode(uint8_t* op) const {
1676 if (IsExtendedOpcode(op)) {
1677 uint8_t* length_field = op + 1;
1678 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1679 return length_field + length;
1680 } else if (!IsStandardOpcode(op)) {
1681 return op + 1;
1682 } else if (*op == DW_LNS_fixed_advance_pc) {
1683 return op + 1 + sizeof(uint16_t);
1684 } else {
1685 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1686 op += 1;
1687 for (int i = 0; i < num_args; i++) {
1688 op = NextLeb128(op);
1689 }
1690 return op;
1691 }
1692 }
1693
1694 uint8_t* GetDebugLineData() const {
1695 const uint8_t* hdr_start =
1696 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1697 return const_cast<uint8_t*>(hdr_start + header_length_);
1698 }
1699};
1700
Ian Rogersd4c4d952014-10-16 20:31:53 -07001701class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001702 public:
1703 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1704 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1705 new DebugLineInstructionIterator(header, section_size));
1706 if (line_iter.get() == nullptr) {
1707 return nullptr;
1708 } else {
1709 return line_iter.release();
1710 }
1711 }
1712
1713 ~DebugLineInstructionIterator() {}
1714
1715 bool Next() {
1716 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001717 return false;
1718 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001719 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1720 if (current_instruction_ >= last_instruction_) {
1721 current_instruction_ = nullptr;
1722 return false;
1723 } else {
1724 return true;
1725 }
1726 }
1727
Ian Rogersd4c4d952014-10-16 20:31:53 -07001728 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001729 return current_instruction_;
1730 }
1731
Ian Rogersd4c4d952014-10-16 20:31:53 -07001732 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001733 return header_->IsExtendedOpcode(current_instruction_);
1734 }
1735
1736 uint8_t GetOpcode() {
1737 if (!IsExtendedOpcode()) {
1738 return *current_instruction_;
1739 } else {
1740 uint8_t* len_ptr = current_instruction_ + 1;
1741 return *NextLeb128(len_ptr);
1742 }
1743 }
1744
1745 uint8_t* GetArguments() {
1746 if (!IsExtendedOpcode()) {
1747 return current_instruction_ + 1;
1748 } else {
1749 uint8_t* len_ptr = current_instruction_ + 1;
1750 return NextLeb128(len_ptr) + 1;
1751 }
1752 }
1753
1754 private:
1755 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1756 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1757 current_instruction_(header->GetDebugLineData()) {}
1758
Ian Rogersd4c4d952014-10-16 20:31:53 -07001759 DebugLineHeader* const header_;
1760 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001761 uint8_t* current_instruction_;
1762};
1763
1764static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1765 while (iter->Next()) {
1766 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1767 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1768 }
Alex Light3470ab42014-06-18 10:35:45 -07001769 }
1770 return true;
1771}
1772
1773struct PACKED(1) DebugInfoHeader {
1774 uint32_t unit_length; // TODO 32-bit specific size
1775 uint16_t version;
1776 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1777 uint8_t address_size;
1778};
1779
1780// Returns -1 if it is variable length, which we will just disallow for now.
1781static int32_t FormLength(uint32_t att) {
1782 switch (att) {
1783 case DW_FORM_data1:
1784 case DW_FORM_flag:
1785 case DW_FORM_flag_present:
1786 case DW_FORM_ref1:
1787 return 1;
1788
1789 case DW_FORM_data2:
1790 case DW_FORM_ref2:
1791 return 2;
1792
1793 case DW_FORM_addr: // TODO 32-bit only
1794 case DW_FORM_ref_addr: // TODO 32-bit only
1795 case DW_FORM_sec_offset: // TODO 32-bit only
1796 case DW_FORM_strp: // TODO 32-bit only
1797 case DW_FORM_data4:
1798 case DW_FORM_ref4:
1799 return 4;
1800
1801 case DW_FORM_data8:
1802 case DW_FORM_ref8:
1803 case DW_FORM_ref_sig8:
1804 return 8;
1805
1806 case DW_FORM_block:
1807 case DW_FORM_block1:
1808 case DW_FORM_block2:
1809 case DW_FORM_block4:
1810 case DW_FORM_exprloc:
1811 case DW_FORM_indirect:
1812 case DW_FORM_ref_udata:
1813 case DW_FORM_sdata:
1814 case DW_FORM_string:
1815 case DW_FORM_udata:
1816 default:
1817 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001818 }
1819}
1820
Ian Rogersd4c4d952014-10-16 20:31:53 -07001821class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001822 public:
Alex Light3470ab42014-06-18 10:35:45 -07001823 ~DebugTag() {}
1824 // Creates a new tag and moves data pointer up to the start of the next one.
1825 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001826 static DebugTag* Create(const uint8_t** data_pointer) {
1827 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001828 uint32_t index = DecodeUnsignedLeb128(&data);
1829 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1830 tag->size_ = static_cast<uint32_t>(
1831 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1832 // skip the abbrev
1833 tag->tag_ = DecodeUnsignedLeb128(&data);
1834 tag->has_child_ = (*data == 0);
1835 data++;
1836 while (true) {
1837 uint32_t attr = DecodeUnsignedLeb128(&data);
1838 uint32_t form = DecodeUnsignedLeb128(&data);
1839 if (attr == 0 && form == 0) {
1840 break;
1841 } else if (attr == 0 || form == 0) {
1842 // Bad abbrev.
1843 return nullptr;
1844 }
1845 int32_t size = FormLength(form);
1846 if (size == -1) {
1847 return nullptr;
1848 }
1849 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1850 }
1851 *data_pointer = data;
1852 return tag.release();
1853 }
1854
1855 uint32_t GetSize() const {
1856 return size_;
1857 }
1858
Ian Rogersd4c4d952014-10-16 20:31:53 -07001859 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001860 return has_child_;
1861 }
1862
Ian Rogersd4c4d952014-10-16 20:31:53 -07001863 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001864 return tag_;
1865 }
1866
Ian Rogersd4c4d952014-10-16 20:31:53 -07001867 uint32_t GetIndex() const {
1868 return index_;
1869 }
1870
Alex Light3470ab42014-06-18 10:35:45 -07001871 // Gets the offset of a particular attribute in this tag structure.
1872 // Interpretation of the data is left to the consumer. 0 is returned if the
1873 // tag does not contain the attribute.
1874 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1875 auto it = off_map_.find(dwarf_attribute);
1876 if (it == off_map_.end()) {
1877 return 0;
1878 } else {
1879 return it->second;
1880 }
1881 }
1882
1883 // Gets the size of attribute
1884 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1885 auto it = size_map_.find(dwarf_attribute);
1886 if (it == size_map_.end()) {
1887 return 0;
1888 } else {
1889 return it->second;
1890 }
1891 }
1892
1893 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001894 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001895 void AddAttribute(uint32_t type, uint32_t attr_size) {
1896 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1897 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1898 size_ += attr_size;
1899 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001900
1901 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001902 std::map<uint32_t, uint32_t> off_map_;
1903 std::map<uint32_t, uint32_t> size_map_;
1904 uint32_t size_;
1905 uint32_t tag_;
1906 bool has_child_;
1907};
1908
1909class DebugAbbrev {
1910 public:
1911 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001912 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001913 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1914 if (!abbrev->ReadAtOffset(0)) {
1915 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001916 }
1917 return abbrev.release();
1918 }
1919
Alex Lightd338ae02014-08-13 17:15:38 -07001920 bool ReadAtOffset(uint32_t abbrev_offset) {
1921 tags_.clear();
1922 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001923 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001924 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1925 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1926 if (tag.get() == nullptr) {
1927 return false;
1928 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001929 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001930 tag_list_.push_back(std::move(tag));
1931 }
1932 }
1933 return true;
1934 }
1935
Ian Rogers13735952014-10-08 12:43:28 -07001936 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001937 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1938 auto it = tags_.find(tag_num);
1939 if (it == tags_.end()) {
1940 return nullptr;
1941 } else {
1942 CHECK_GT(tag_list_.size(), it->second);
1943 return tag_list_.at(it->second).get();
1944 }
1945 }
1946
1947 private:
Ian Rogers13735952014-10-08 12:43:28 -07001948 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001949 const uint8_t* const begin_;
1950 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001951 std::map<uint32_t, uint32_t> tags_;
1952 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1953};
1954
1955class DebugInfoIterator {
1956 public:
1957 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1958 DebugAbbrev* abbrev) {
1959 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1960 if (iter->GetCurrentTag() == nullptr) {
1961 return nullptr;
1962 } else {
1963 return iter.release();
1964 }
1965 }
1966 ~DebugInfoIterator() {}
1967
1968 // Moves to the next DIE. Returns false if at last entry.
1969 // TODO Handle variable length attributes.
1970 bool next() {
1971 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1972 return false;
1973 }
Alex Lightd338ae02014-08-13 17:15:38 -07001974 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001975 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001976 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1977 current_cu_ = next_cu_;
1978 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001979 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001980 reread_abbrev = true;
1981 }
Alex Light3470ab42014-06-18 10:35:45 -07001982 if (current_entry_ >= last_entry_) {
1983 current_entry_ = nullptr;
1984 return false;
1985 }
Alex Lightd338ae02014-08-13 17:15:38 -07001986 if (reread_abbrev) {
1987 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1988 }
Alex Light3470ab42014-06-18 10:35:45 -07001989 current_tag_ = abbrev_->ReadTag(current_entry_);
1990 if (current_tag_ == nullptr) {
1991 current_entry_ = nullptr;
1992 return false;
1993 } else {
1994 return true;
1995 }
1996 }
1997
1998 const DebugTag* GetCurrentTag() {
1999 return const_cast<DebugTag*>(current_tag_);
2000 }
Ian Rogers13735952014-10-08 12:43:28 -07002001 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07002002 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
2003 return nullptr;
2004 }
2005 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
2006 if (off == 0) {
2007 // tag does not have that field.
2008 return nullptr;
2009 } else {
2010 DCHECK_LT(off, current_tag_->GetSize());
2011 return current_entry_ + off;
2012 }
2013 }
2014
2015 private:
Alex Lightd338ae02014-08-13 17:15:38 -07002016 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07002017 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07002018 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
2019 }
2020
Alex Light3470ab42014-06-18 10:35:45 -07002021 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
2022 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07002023 current_cu_(header),
2024 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07002025 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
2026 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07002027 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002028 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002029 DebugInfoHeader* current_cu_;
2030 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002031 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002032 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002033 DebugTag* current_tag_;
2034};
2035
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002036static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002037 do {
2038 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2039 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002040 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002041 return false;
2042 }
2043 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2044 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2045 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002046 *PC_low += base_address_delta;
2047 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002048 }
2049 } while (iter->next());
2050 return true;
2051}
2052
Tong Shen62d1ca32014-09-03 17:24:56 -07002053template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2054 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2055 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2056bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2057 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2058 ::FixupDebugSections(off_t base_address_delta) {
2059 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2060 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2061 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2062 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2063 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2064 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2065 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002066
2067 if (debug_info == nullptr || debug_abbrev == nullptr ||
2068 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2069 // Release version of ART does not generate debug info.
2070 return true;
2071 }
2072 if (base_address_delta == 0) {
2073 return true;
2074 }
2075 if (eh_frame != nullptr &&
2076 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2077 return false;
2078 }
2079
2080 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2081 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002082 if (abbrev.get() == nullptr) {
2083 return false;
2084 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002085 DebugInfoHeader* info_header =
2086 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2087 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2088 debug_info->sh_size,
2089 abbrev.get()));
2090 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002091 return false;
2092 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002093 if (debug_line != nullptr) {
2094 DebugLineHeader* line_header =
2095 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2096 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2097 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2098 if (line_iter.get() == nullptr) {
2099 return false;
2100 }
2101 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2102 return false;
2103 }
2104 }
2105 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002106}
Mark Mendellae9fd932014-02-10 16:14:35 -08002107
Tong Shen62d1ca32014-09-03 17:24:56 -07002108template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2109 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2110 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2111void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2112 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2113 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002114 // We only get here if we only are mapping the program header.
2115 DCHECK(program_header_only_);
2116
2117 // Well, we need the whole file to do this.
2118 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002119 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2120 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002121 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2122 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2123 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2124 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002125 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002126 return;
2127 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002128 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2129 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002130
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002131 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002132 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002133 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002134 return;
2135 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002136
2137 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002138 // We need to add in a strtab and symtab to the image.
2139 // all is MAP_PRIVATE so it can be written to freely.
2140 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002141 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002142 elf_hdr.e_entry = 0;
2143 elf_hdr.e_phoff = 0;
2144 elf_hdr.e_phnum = 0;
2145 elf_hdr.e_phentsize = 0;
2146 elf_hdr.e_type = ET_EXEC;
2147
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002148 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2149 // and the actual address stuff starts at in regular files this is good.
2150 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002151 LOG(ERROR) << "Failed to load GDB data";
2152 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002153 }
2154
Alex Light3470ab42014-06-18 10:35:45 -07002155 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2156 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002157}
2158
Tong Shen62d1ca32014-09-03 17:24:56 -07002159template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2160 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2161 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2162bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2163 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2164 ::Strip(std::string* error_msg) {
2165 // ELF files produced by MCLinker look roughly like this
2166 //
2167 // +------------+
2168 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2169 // +------------+
2170 // | Elf_Phdr | program headers
2171 // | Elf_Phdr |
2172 // | ... |
2173 // | Elf_Phdr |
2174 // +------------+
2175 // | section | mixture of needed and unneeded sections
2176 // +------------+
2177 // | section |
2178 // +------------+
2179 // | ... |
2180 // +------------+
2181 // | section |
2182 // +------------+
2183 // | Elf_Shdr | section headers
2184 // | Elf_Shdr |
2185 // | ... | contains offset to section start
2186 // | Elf_Shdr |
2187 // +------------+
2188 //
2189 // To strip:
2190 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2191 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2192 // - move the sections are keeping up to fill in gaps of sections we want to strip
2193 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2194 // - truncate rest of file
2195 //
2196
2197 std::vector<Elf_Shdr> section_headers;
2198 std::vector<Elf_Word> section_headers_original_indexes;
2199 section_headers.reserve(GetSectionHeaderNum());
2200
2201
2202 Elf_Shdr* string_section = GetSectionNameStringSection();
2203 CHECK(string_section != nullptr);
2204 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2205 Elf_Shdr* sh = GetSectionHeader(i);
2206 CHECK(sh != nullptr);
2207 const char* name = GetString(*string_section, sh->sh_name);
2208 if (name == nullptr) {
2209 CHECK_EQ(0U, i);
2210 section_headers.push_back(*sh);
2211 section_headers_original_indexes.push_back(0);
2212 continue;
2213 }
2214 if (StartsWith(name, ".debug")
2215 || (strcmp(name, ".strtab") == 0)
2216 || (strcmp(name, ".symtab") == 0)) {
2217 continue;
2218 }
2219 section_headers.push_back(*sh);
2220 section_headers_original_indexes.push_back(i);
2221 }
2222 CHECK_NE(0U, section_headers.size());
2223 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2224
2225 // section 0 is the NULL section, sections start at offset of first section
2226 CHECK(GetSectionHeader(1) != nullptr);
2227 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2228 for (size_t i = 1; i < section_headers.size(); i++) {
2229 Elf_Shdr& new_sh = section_headers[i];
2230 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2231 CHECK(old_sh != nullptr);
2232 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2233 if (old_sh->sh_addralign > 1) {
2234 offset = RoundUp(offset, old_sh->sh_addralign);
2235 }
2236 if (old_sh->sh_offset == offset) {
2237 // already in place
2238 offset += old_sh->sh_size;
2239 continue;
2240 }
2241 // shift section earlier
2242 memmove(Begin() + offset,
2243 Begin() + old_sh->sh_offset,
2244 old_sh->sh_size);
2245 new_sh.sh_offset = offset;
2246 offset += old_sh->sh_size;
2247 }
2248
2249 Elf_Off shoff = offset;
2250 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2251 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2252 offset += section_headers_size_in_bytes;
2253
2254 GetHeader().e_shnum = section_headers.size();
2255 GetHeader().e_shoff = shoff;
2256 int result = ftruncate(file_->Fd(), offset);
2257 if (result != 0) {
2258 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2259 file_->GetPath().c_str(), strerror(errno));
2260 return false;
2261 }
2262 return true;
2263}
2264
2265static const bool DEBUG_FIXUP = false;
2266
2267template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2268 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2269 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2270bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2271 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2272 ::Fixup(uintptr_t base_address) {
2273 if (!FixupDynamic(base_address)) {
2274 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2275 return false;
2276 }
2277 if (!FixupSectionHeaders(base_address)) {
2278 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2279 return false;
2280 }
2281 if (!FixupProgramHeaders(base_address)) {
2282 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2283 return false;
2284 }
2285 if (!FixupSymbols(base_address, true)) {
2286 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2287 return false;
2288 }
2289 if (!FixupSymbols(base_address, false)) {
2290 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2291 return false;
2292 }
2293 if (!FixupRelocations(base_address)) {
2294 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2295 return false;
2296 }
2297 if (!FixupDebugSections(base_address)) {
2298 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2299 return false;
2300 }
2301 return true;
2302}
2303
2304template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2305 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2306 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2307bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2308 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2309 ::FixupDynamic(uintptr_t base_address) {
2310 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2311 Elf_Dyn& elf_dyn = GetDynamic(i);
2312 Elf_Word d_tag = elf_dyn.d_tag;
2313 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2314 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2315 if (DEBUG_FIXUP) {
2316 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2317 GetFile().GetPath().c_str(), i,
2318 static_cast<uint64_t>(d_ptr),
2319 static_cast<uint64_t>(d_ptr + base_address));
2320 }
2321 d_ptr += base_address;
2322 elf_dyn.d_un.d_ptr = d_ptr;
2323 }
2324 }
2325 return true;
2326}
2327
2328template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2329 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2330 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2331bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2332 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2333 ::FixupSectionHeaders(uintptr_t base_address) {
2334 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2335 Elf_Shdr* sh = GetSectionHeader(i);
2336 CHECK(sh != nullptr);
2337 // 0 implies that the section will not exist in the memory of the process
2338 if (sh->sh_addr == 0) {
2339 continue;
2340 }
2341 if (DEBUG_FIXUP) {
2342 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2343 GetFile().GetPath().c_str(), i,
2344 static_cast<uint64_t>(sh->sh_addr),
2345 static_cast<uint64_t>(sh->sh_addr + base_address));
2346 }
2347 sh->sh_addr += base_address;
2348 }
2349 return true;
2350}
2351
2352template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2353 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2354 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2355bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2356 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2357 ::FixupProgramHeaders(uintptr_t base_address) {
2358 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2359 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2360 Elf_Phdr* ph = GetProgramHeader(i);
2361 CHECK(ph != nullptr);
2362 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2363 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2364 << GetFile().GetPath() << " i=" << i;
2365 if (DEBUG_FIXUP) {
2366 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2367 GetFile().GetPath().c_str(), i,
2368 static_cast<uint64_t>(ph->p_vaddr),
2369 static_cast<uint64_t>(ph->p_vaddr + base_address));
2370 }
2371 ph->p_vaddr += base_address;
2372 ph->p_paddr += base_address;
2373 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2374 << GetFile().GetPath() << " i=" << i;
2375 }
2376 return true;
2377}
2378
2379template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2380 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2381 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2382bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2383 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2384 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2385 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2386 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2387 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2388 if (symbol_section == nullptr) {
2389 // file is missing optional .symtab
2390 CHECK(!dynamic) << GetFile().GetPath();
2391 return true;
2392 }
2393 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2394 Elf_Sym* symbol = GetSymbol(section_type, i);
2395 CHECK(symbol != nullptr);
2396 if (symbol->st_value != 0) {
2397 if (DEBUG_FIXUP) {
2398 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2399 GetFile().GetPath().c_str(), i,
2400 static_cast<uint64_t>(symbol->st_value),
2401 static_cast<uint64_t>(symbol->st_value + base_address));
2402 }
2403 symbol->st_value += base_address;
2404 }
2405 }
2406 return true;
2407}
2408
2409template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2410 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2411 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2412bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2413 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2414 ::FixupRelocations(uintptr_t base_address) {
2415 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2416 Elf_Shdr* sh = GetSectionHeader(i);
2417 CHECK(sh != nullptr);
2418 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002419 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
2420 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002421 if (DEBUG_FIXUP) {
2422 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002423 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002424 static_cast<uint64_t>(rel.r_offset),
2425 static_cast<uint64_t>(rel.r_offset + base_address));
2426 }
2427 rel.r_offset += base_address;
2428 }
2429 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002430 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
2431 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002432 if (DEBUG_FIXUP) {
2433 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002434 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002435 static_cast<uint64_t>(rela.r_offset),
2436 static_cast<uint64_t>(rela.r_offset + base_address));
2437 }
2438 rela.r_offset += base_address;
2439 }
2440 }
2441 }
2442 return true;
2443}
2444
2445// Explicit instantiations
2446template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2447 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2448template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2449 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2450
Ian Rogersd4c4d952014-10-16 20:31:53 -07002451ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002452}
2453
Ian Rogersd4c4d952014-10-16 20:31:53 -07002454ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002455}
2456
2457ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002458 // Should never have 32 and 64-bit impls.
2459 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002460}
2461
Igor Murashkin46774762014-10-22 11:37:02 -07002462ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2463 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002464 if (file->GetLength() < EI_NIDENT) {
2465 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2466 file->GetPath().c_str());
2467 return nullptr;
2468 }
2469 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2470 file->GetPath().c_str(), error_msg));
2471 if (map == nullptr && map->Size() != EI_NIDENT) {
2472 return nullptr;
2473 }
Ian Rogers13735952014-10-08 12:43:28 -07002474 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002475 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002476 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2477 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002478 if (elf_file_impl == nullptr)
2479 return nullptr;
2480 return new ElfFile(elf_file_impl);
2481 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002482 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2483 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002484 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002485 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002486 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002487 return new ElfFile(elf_file_impl);
2488 } else {
2489 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2490 ELFCLASS32, ELFCLASS64,
2491 file->GetPath().c_str(),
2492 header[EI_CLASS]);
2493 return nullptr;
2494 }
2495}
2496
2497ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2498 if (file->GetLength() < EI_NIDENT) {
2499 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2500 file->GetPath().c_str());
2501 return nullptr;
2502 }
2503 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2504 file->GetPath().c_str(), error_msg));
2505 if (map == nullptr && map->Size() != EI_NIDENT) {
2506 return nullptr;
2507 }
Ian Rogers13735952014-10-08 12:43:28 -07002508 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002509 if (header[EI_CLASS] == ELFCLASS64) {
2510 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002511 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002512 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002513 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002514 return new ElfFile(elf_file_impl);
2515 } else if (header[EI_CLASS] == ELFCLASS32) {
2516 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002517 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002518 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002519 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002520 return new ElfFile(elf_file_impl);
2521 } else {
2522 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2523 ELFCLASS32, ELFCLASS64,
2524 file->GetPath().c_str(),
2525 header[EI_CLASS]);
2526 return nullptr;
2527 }
2528}
2529
2530#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002531 if (elf64_.get() != nullptr) { \
2532 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002533 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002534 DCHECK(elf32_.get() != nullptr); \
2535 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002536 }
2537
2538bool ElfFile::Load(bool executable, std::string* error_msg) {
2539 DELEGATE_TO_IMPL(Load, executable, error_msg);
2540}
2541
Ian Rogers13735952014-10-08 12:43:28 -07002542const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002543 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2544}
2545
2546size_t ElfFile::Size() const {
2547 DELEGATE_TO_IMPL(Size);
2548}
2549
Ian Rogers13735952014-10-08 12:43:28 -07002550uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002551 DELEGATE_TO_IMPL(Begin);
2552}
2553
Ian Rogers13735952014-10-08 12:43:28 -07002554uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002555 DELEGATE_TO_IMPL(End);
2556}
2557
2558const File& ElfFile::GetFile() const {
2559 DELEGATE_TO_IMPL(GetFile);
2560}
2561
2562bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002563 if (elf32_.get() == nullptr) {
2564 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002565
Ian Rogersd4c4d952014-10-16 20:31:53 -07002566 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2567 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002568 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002569 }
2570 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002571 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002572 }
2573 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002574 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002575 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002576 return true;
2577 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002578 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2579 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002580 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002581 }
2582 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002583 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002584 }
2585 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002586 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002587 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002588 return true;
2589 }
2590}
2591
2592uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2593 const std::string& symbol_name,
2594 bool build_map) {
2595 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2596}
2597
2598size_t ElfFile::GetLoadedSize() const {
2599 DELEGATE_TO_IMPL(GetLoadedSize);
2600}
2601
2602bool ElfFile::Strip(File* file, std::string* error_msg) {
2603 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2604 if (elf_file.get() == nullptr) {
2605 return false;
2606 }
2607
Ian Rogersd4c4d952014-10-16 20:31:53 -07002608 if (elf_file->elf64_.get() != nullptr)
2609 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002610 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002611 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002612}
2613
2614bool ElfFile::Fixup(uintptr_t base_address) {
2615 DELEGATE_TO_IMPL(Fixup, base_address);
2616}
2617
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002618} // namespace art