blob: a22e2741ceb8ca3ba849fa48c1f09fa748e04d7c [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) {
Andreas Gampe6f611412015-01-21 22:25:24 -08001316 InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
Andreas Gampe91268c12014-04-03 17:50:24 -07001317 if (elf_ISA != kRuntimeISA) {
1318 std::ostringstream oss;
1319 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1320 *error_msg = oss.str();
1321 return false;
1322 }
1323 }
1324
Jim_Guoa62a5882014-04-28 11:11:57 +08001325 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001326 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1327 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001328 if (program_header == nullptr) {
1329 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1330 i, file_->GetPath().c_str());
1331 return false;
1332 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001333
1334 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001335 if (program_header->p_type == PT_DYNAMIC) {
1336 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001337 continue;
1338 }
1339
1340 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001341 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001342 continue;
1343 }
1344
1345 // Found something to load.
1346
Jim_Guoa62a5882014-04-28 11:11:57 +08001347 // Before load the actual segments, reserve a contiguous chunk
1348 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001349 // permissions. We'll then carve that up with the proper
1350 // permissions as we load the actual segments. If p_vaddr is
1351 // non-zero, the segments require the specific address specified,
1352 // which either was specified in the file because we already set
1353 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001354 int64_t temp_file_length = file_->GetLength();
1355 if (temp_file_length < 0) {
1356 errno = -temp_file_length;
1357 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1358 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1359 return false;
1360 }
1361 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001362 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001363 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1364 uint8_t* reserve_base_override = reserve_base;
1365 // Override the base (e.g. when compiling with --compile-pic)
1366 if (requested_base_ != nullptr) {
1367 reserve_base_override = requested_base_;
1368 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001369 std::string reservation_name("ElfFile reservation for ");
1370 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001371 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001372 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001373 GetLoadedSize(), PROT_NONE, false,
1374 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001375 if (reserve.get() == nullptr) {
1376 *error_msg = StringPrintf("Failed to allocate %s: %s",
1377 reservation_name.c_str(), error_msg->c_str());
1378 return false;
1379 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001380 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001381
1382 // Base address is the difference of actual mapped location and the p_vaddr
1383 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1384 - reinterpret_cast<uintptr_t>(reserve_base));
1385 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1386 // dynamic memory address of where that object is actually mapped
1387 //
1388 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1389 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001390 segments_.push_back(reserve.release());
1391 }
1392 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001393 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001394 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001395 }
Ian Rogers13735952014-10-08 12:43:28 -07001396 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001397 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001398 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001399 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001400 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001401 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001402 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001403 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001404 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001405 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001406 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001407 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001408 if (writable_) {
1409 prot |= PROT_WRITE;
1410 flags |= MAP_SHARED;
1411 } else {
1412 flags |= MAP_PRIVATE;
1413 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001414 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001415 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001416 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1417 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001418 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001419 return false;
1420 }
Ian Rogers700a4022014-05-19 16:49:03 -07001421 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001422 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001423 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001424 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001425 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001426 file_->GetPath().c_str(),
1427 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001428 if (segment.get() == nullptr) {
1429 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1430 i, file_->GetPath().c_str(), error_msg->c_str());
1431 return false;
1432 }
1433 if (segment->Begin() != p_vaddr) {
1434 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1435 "instead mapped to %p",
1436 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1437 return false;
1438 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001439 segments_.push_back(segment.release());
1440 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001441
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001442 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001443 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001444 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1445 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1446 file_->GetPath().c_str());
1447 return false;
1448 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001449 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001450
Tong Shen62d1ca32014-09-03 17:24:56 -07001451 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1452 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001453 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001454 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001455 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001456 if (!ValidPointer(d_ptr)) {
1457 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1458 d_ptr, file_->GetPath().c_str());
1459 return false;
1460 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001461 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001462 break;
1463 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001464 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001465 if (!ValidPointer(d_ptr)) {
1466 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1467 d_ptr, file_->GetPath().c_str());
1468 return false;
1469 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001470 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1471 break;
1472 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001473 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001474 if (!ValidPointer(d_ptr)) {
1475 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1476 d_ptr, file_->GetPath().c_str());
1477 return false;
1478 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001479 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001480 break;
1481 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001482 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001483 if (GetDynamicNum() != i+1) {
1484 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1485 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1486 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1487 return false;
1488 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001489 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001490 }
1491 }
1492 }
1493
Andreas Gampedaab38c2014-09-12 18:38:24 -07001494 // Check for the existence of some sections.
1495 if (!CheckSectionsExist(error_msg)) {
1496 return false;
1497 }
1498
Mark Mendellae9fd932014-02-10 16:14:35 -08001499 // Use GDB JIT support to do stack backtrace, etc.
1500 if (executable) {
1501 GdbJITSupport();
1502 }
1503
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001504 return true;
1505}
1506
Tong Shen62d1ca32014-09-03 17:24:56 -07001507template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1508 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1509 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1510bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1511 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001512 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001513 for (size_t i = 0; i < segments_.size(); ++i) {
1514 const MemMap* segment = segments_[i];
1515 if (segment->Begin() <= start && start < segment->End()) {
1516 return true;
1517 }
1518 }
1519 return false;
1520}
1521
Alex Light3470ab42014-06-18 10:35:45 -07001522
Tong Shen62d1ca32014-09-03 17:24:56 -07001523template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1524 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1525 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1526Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1527 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1528 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001529 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001530 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001531 if (shstrtab_sec == nullptr) {
1532 return nullptr;
1533 }
Alex Light3470ab42014-06-18 10:35:45 -07001534 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001535 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001536 if (shdr == nullptr) {
1537 return nullptr;
1538 }
1539 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001540 if (sec_name == nullptr) {
1541 continue;
1542 }
1543 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001544 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001545 }
1546 }
1547 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001548}
1549
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001550struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001551 uint32_t raw_length_;
1552 uint32_t GetLength() {
1553 return raw_length_ + sizeof(raw_length_);
1554 }
1555 uint32_t CIE_pointer;
1556 uint32_t initial_location;
1557 uint32_t address_range;
1558 uint8_t instructions[0];
1559};
1560
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001561static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001562 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001563 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001564 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001565}
1566
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001567static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001568 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001569}
1570
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001571struct PACKED(1) FDE64 {
1572 uint32_t raw_length_;
1573 uint64_t extended_length_;
1574 uint64_t GetLength() {
1575 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1576 }
1577 uint64_t CIE_pointer;
1578 uint64_t initial_location;
1579 uint64_t address_range;
1580 uint8_t instructions[0];
1581};
1582
1583static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001584 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001585 fde_bytes += frame->GetLength();
1586 return reinterpret_cast<FDE64*>(fde_bytes);
1587}
1588
1589static bool IsFDE(FDE64* frame) {
1590 return frame->CIE_pointer != 0;
1591}
1592
1593static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001594 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001595 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1596 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1597 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1598 for (; frame < last_frame; frame = NextFDE(frame)) {
1599 if (!IsFDE(frame)) {
1600 return false;
1601 }
1602 frame->initial_location += base_address_delta;
1603 }
1604 return true;
1605 } else {
1606 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1607 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1608 for (; frame < last_frame; frame = NextFDE(frame)) {
1609 if (!IsFDE(frame)) {
1610 return false;
1611 }
1612 frame->initial_location += base_address_delta;
1613 }
1614 return true;
1615 }
1616}
1617
1618static uint8_t* NextLeb128(uint8_t* current) {
1619 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1620 return current;
1621}
1622
1623struct PACKED(1) DebugLineHeader {
1624 uint32_t unit_length_; // TODO 32-bit specific size
1625 uint16_t version_;
1626 uint32_t header_length_; // TODO 32-bit specific size
1627 uint8_t minimum_instruction_lenght_;
1628 uint8_t maximum_operations_per_instruction_;
1629 uint8_t default_is_stmt_;
1630 int8_t line_base_;
1631 uint8_t line_range_;
1632 uint8_t opcode_base_;
1633 uint8_t remaining_[0];
1634
1635 bool IsStandardOpcode(const uint8_t* op) const {
1636 return *op != 0 && *op < opcode_base_;
1637 }
1638
1639 bool IsExtendedOpcode(const uint8_t* op) const {
1640 return *op == 0;
1641 }
1642
1643 const uint8_t* GetStandardOpcodeLengths() const {
1644 return remaining_;
1645 }
1646
1647 uint8_t* GetNextOpcode(uint8_t* op) const {
1648 if (IsExtendedOpcode(op)) {
1649 uint8_t* length_field = op + 1;
1650 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1651 return length_field + length;
1652 } else if (!IsStandardOpcode(op)) {
1653 return op + 1;
1654 } else if (*op == DW_LNS_fixed_advance_pc) {
1655 return op + 1 + sizeof(uint16_t);
1656 } else {
1657 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1658 op += 1;
1659 for (int i = 0; i < num_args; i++) {
1660 op = NextLeb128(op);
1661 }
1662 return op;
1663 }
1664 }
1665
1666 uint8_t* GetDebugLineData() const {
1667 const uint8_t* hdr_start =
1668 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1669 return const_cast<uint8_t*>(hdr_start + header_length_);
1670 }
1671};
1672
Ian Rogersd4c4d952014-10-16 20:31:53 -07001673class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001674 public:
1675 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1676 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1677 new DebugLineInstructionIterator(header, section_size));
1678 if (line_iter.get() == nullptr) {
1679 return nullptr;
1680 } else {
1681 return line_iter.release();
1682 }
1683 }
1684
1685 ~DebugLineInstructionIterator() {}
1686
1687 bool Next() {
1688 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001689 return false;
1690 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001691 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1692 if (current_instruction_ >= last_instruction_) {
1693 current_instruction_ = nullptr;
1694 return false;
1695 } else {
1696 return true;
1697 }
1698 }
1699
Ian Rogersd4c4d952014-10-16 20:31:53 -07001700 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001701 return current_instruction_;
1702 }
1703
Ian Rogersd4c4d952014-10-16 20:31:53 -07001704 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001705 return header_->IsExtendedOpcode(current_instruction_);
1706 }
1707
1708 uint8_t GetOpcode() {
1709 if (!IsExtendedOpcode()) {
1710 return *current_instruction_;
1711 } else {
1712 uint8_t* len_ptr = current_instruction_ + 1;
1713 return *NextLeb128(len_ptr);
1714 }
1715 }
1716
1717 uint8_t* GetArguments() {
1718 if (!IsExtendedOpcode()) {
1719 return current_instruction_ + 1;
1720 } else {
1721 uint8_t* len_ptr = current_instruction_ + 1;
1722 return NextLeb128(len_ptr) + 1;
1723 }
1724 }
1725
1726 private:
1727 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1728 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1729 current_instruction_(header->GetDebugLineData()) {}
1730
Ian Rogersd4c4d952014-10-16 20:31:53 -07001731 DebugLineHeader* const header_;
1732 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001733 uint8_t* current_instruction_;
1734};
1735
1736static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1737 while (iter->Next()) {
1738 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1739 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1740 }
Alex Light3470ab42014-06-18 10:35:45 -07001741 }
1742 return true;
1743}
1744
1745struct PACKED(1) DebugInfoHeader {
1746 uint32_t unit_length; // TODO 32-bit specific size
1747 uint16_t version;
1748 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1749 uint8_t address_size;
1750};
1751
1752// Returns -1 if it is variable length, which we will just disallow for now.
1753static int32_t FormLength(uint32_t att) {
1754 switch (att) {
1755 case DW_FORM_data1:
1756 case DW_FORM_flag:
1757 case DW_FORM_flag_present:
1758 case DW_FORM_ref1:
1759 return 1;
1760
1761 case DW_FORM_data2:
1762 case DW_FORM_ref2:
1763 return 2;
1764
1765 case DW_FORM_addr: // TODO 32-bit only
1766 case DW_FORM_ref_addr: // TODO 32-bit only
1767 case DW_FORM_sec_offset: // TODO 32-bit only
1768 case DW_FORM_strp: // TODO 32-bit only
1769 case DW_FORM_data4:
1770 case DW_FORM_ref4:
1771 return 4;
1772
1773 case DW_FORM_data8:
1774 case DW_FORM_ref8:
1775 case DW_FORM_ref_sig8:
1776 return 8;
1777
1778 case DW_FORM_block:
1779 case DW_FORM_block1:
1780 case DW_FORM_block2:
1781 case DW_FORM_block4:
1782 case DW_FORM_exprloc:
1783 case DW_FORM_indirect:
1784 case DW_FORM_ref_udata:
1785 case DW_FORM_sdata:
1786 case DW_FORM_string:
1787 case DW_FORM_udata:
1788 default:
1789 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001790 }
1791}
1792
Ian Rogersd4c4d952014-10-16 20:31:53 -07001793class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001794 public:
Alex Light3470ab42014-06-18 10:35:45 -07001795 ~DebugTag() {}
1796 // Creates a new tag and moves data pointer up to the start of the next one.
1797 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001798 static DebugTag* Create(const uint8_t** data_pointer) {
1799 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001800 uint32_t index = DecodeUnsignedLeb128(&data);
1801 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1802 tag->size_ = static_cast<uint32_t>(
1803 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1804 // skip the abbrev
1805 tag->tag_ = DecodeUnsignedLeb128(&data);
1806 tag->has_child_ = (*data == 0);
1807 data++;
1808 while (true) {
1809 uint32_t attr = DecodeUnsignedLeb128(&data);
1810 uint32_t form = DecodeUnsignedLeb128(&data);
1811 if (attr == 0 && form == 0) {
1812 break;
1813 } else if (attr == 0 || form == 0) {
1814 // Bad abbrev.
1815 return nullptr;
1816 }
1817 int32_t size = FormLength(form);
1818 if (size == -1) {
1819 return nullptr;
1820 }
1821 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1822 }
1823 *data_pointer = data;
1824 return tag.release();
1825 }
1826
1827 uint32_t GetSize() const {
1828 return size_;
1829 }
1830
Ian Rogersd4c4d952014-10-16 20:31:53 -07001831 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001832 return has_child_;
1833 }
1834
Ian Rogersd4c4d952014-10-16 20:31:53 -07001835 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001836 return tag_;
1837 }
1838
Ian Rogersd4c4d952014-10-16 20:31:53 -07001839 uint32_t GetIndex() const {
1840 return index_;
1841 }
1842
Alex Light3470ab42014-06-18 10:35:45 -07001843 // Gets the offset of a particular attribute in this tag structure.
1844 // Interpretation of the data is left to the consumer. 0 is returned if the
1845 // tag does not contain the attribute.
1846 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1847 auto it = off_map_.find(dwarf_attribute);
1848 if (it == off_map_.end()) {
1849 return 0;
1850 } else {
1851 return it->second;
1852 }
1853 }
1854
1855 // Gets the size of attribute
1856 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1857 auto it = size_map_.find(dwarf_attribute);
1858 if (it == size_map_.end()) {
1859 return 0;
1860 } else {
1861 return it->second;
1862 }
1863 }
1864
1865 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001866 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001867 void AddAttribute(uint32_t type, uint32_t attr_size) {
1868 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1869 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1870 size_ += attr_size;
1871 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001872
1873 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001874 std::map<uint32_t, uint32_t> off_map_;
1875 std::map<uint32_t, uint32_t> size_map_;
1876 uint32_t size_;
1877 uint32_t tag_;
1878 bool has_child_;
1879};
1880
1881class DebugAbbrev {
1882 public:
1883 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001884 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001885 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1886 if (!abbrev->ReadAtOffset(0)) {
1887 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001888 }
1889 return abbrev.release();
1890 }
1891
Alex Lightd338ae02014-08-13 17:15:38 -07001892 bool ReadAtOffset(uint32_t abbrev_offset) {
1893 tags_.clear();
1894 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001895 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001896 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1897 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1898 if (tag.get() == nullptr) {
1899 return false;
1900 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001901 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001902 tag_list_.push_back(std::move(tag));
1903 }
1904 }
1905 return true;
1906 }
1907
Ian Rogers13735952014-10-08 12:43:28 -07001908 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001909 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1910 auto it = tags_.find(tag_num);
1911 if (it == tags_.end()) {
1912 return nullptr;
1913 } else {
1914 CHECK_GT(tag_list_.size(), it->second);
1915 return tag_list_.at(it->second).get();
1916 }
1917 }
1918
1919 private:
Ian Rogers13735952014-10-08 12:43:28 -07001920 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001921 const uint8_t* const begin_;
1922 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001923 std::map<uint32_t, uint32_t> tags_;
1924 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1925};
1926
1927class DebugInfoIterator {
1928 public:
1929 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1930 DebugAbbrev* abbrev) {
1931 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1932 if (iter->GetCurrentTag() == nullptr) {
1933 return nullptr;
1934 } else {
1935 return iter.release();
1936 }
1937 }
1938 ~DebugInfoIterator() {}
1939
1940 // Moves to the next DIE. Returns false if at last entry.
1941 // TODO Handle variable length attributes.
1942 bool next() {
1943 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1944 return false;
1945 }
Alex Lightd338ae02014-08-13 17:15:38 -07001946 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001947 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001948 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1949 current_cu_ = next_cu_;
1950 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001951 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001952 reread_abbrev = true;
1953 }
Alex Light3470ab42014-06-18 10:35:45 -07001954 if (current_entry_ >= last_entry_) {
1955 current_entry_ = nullptr;
1956 return false;
1957 }
Alex Lightd338ae02014-08-13 17:15:38 -07001958 if (reread_abbrev) {
1959 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1960 }
Alex Light3470ab42014-06-18 10:35:45 -07001961 current_tag_ = abbrev_->ReadTag(current_entry_);
1962 if (current_tag_ == nullptr) {
1963 current_entry_ = nullptr;
1964 return false;
1965 } else {
1966 return true;
1967 }
1968 }
1969
1970 const DebugTag* GetCurrentTag() {
1971 return const_cast<DebugTag*>(current_tag_);
1972 }
Ian Rogers13735952014-10-08 12:43:28 -07001973 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07001974 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1975 return nullptr;
1976 }
1977 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1978 if (off == 0) {
1979 // tag does not have that field.
1980 return nullptr;
1981 } else {
1982 DCHECK_LT(off, current_tag_->GetSize());
1983 return current_entry_ + off;
1984 }
1985 }
1986
1987 private:
Alex Lightd338ae02014-08-13 17:15:38 -07001988 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07001989 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07001990 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
1991 }
1992
Alex Light3470ab42014-06-18 10:35:45 -07001993 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
1994 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07001995 current_cu_(header),
1996 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07001997 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
1998 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07001999 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002000 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002001 DebugInfoHeader* current_cu_;
2002 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002003 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002004 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002005 DebugTag* current_tag_;
2006};
2007
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002008static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002009 do {
2010 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2011 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002012 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002013 return false;
2014 }
2015 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2016 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2017 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002018 *PC_low += base_address_delta;
2019 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002020 }
2021 } while (iter->next());
2022 return true;
2023}
2024
Tong Shen62d1ca32014-09-03 17:24:56 -07002025template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2026 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2027 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2028bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2029 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2030 ::FixupDebugSections(off_t base_address_delta) {
2031 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2032 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2033 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2034 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2035 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2036 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2037 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002038
2039 if (debug_info == nullptr || debug_abbrev == nullptr ||
2040 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2041 // Release version of ART does not generate debug info.
2042 return true;
2043 }
2044 if (base_address_delta == 0) {
2045 return true;
2046 }
2047 if (eh_frame != nullptr &&
2048 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2049 return false;
2050 }
2051
2052 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2053 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002054 if (abbrev.get() == nullptr) {
2055 return false;
2056 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002057 DebugInfoHeader* info_header =
2058 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2059 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2060 debug_info->sh_size,
2061 abbrev.get()));
2062 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002063 return false;
2064 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002065 if (debug_line != nullptr) {
2066 DebugLineHeader* line_header =
2067 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2068 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2069 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2070 if (line_iter.get() == nullptr) {
2071 return false;
2072 }
2073 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2074 return false;
2075 }
2076 }
2077 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002078}
Mark Mendellae9fd932014-02-10 16:14:35 -08002079
Tong Shen62d1ca32014-09-03 17:24:56 -07002080template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2081 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2082 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2083void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2084 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2085 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002086 // We only get here if we only are mapping the program header.
2087 DCHECK(program_header_only_);
2088
2089 // Well, we need the whole file to do this.
2090 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002091 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2092 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002093 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2094 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2095 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2096 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002097 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002098 return;
2099 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002100 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2101 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002102
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002103 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002104 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002105 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002106 return;
2107 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002108
2109 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002110 // We need to add in a strtab and symtab to the image.
2111 // all is MAP_PRIVATE so it can be written to freely.
2112 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002113 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002114 elf_hdr.e_entry = 0;
2115 elf_hdr.e_phoff = 0;
2116 elf_hdr.e_phnum = 0;
2117 elf_hdr.e_phentsize = 0;
2118 elf_hdr.e_type = ET_EXEC;
2119
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002120 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2121 // and the actual address stuff starts at in regular files this is good.
2122 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002123 LOG(ERROR) << "Failed to load GDB data";
2124 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002125 }
2126
Alex Light3470ab42014-06-18 10:35:45 -07002127 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2128 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002129}
2130
Tong Shen62d1ca32014-09-03 17:24:56 -07002131template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2132 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2133 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2134bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2135 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2136 ::Strip(std::string* error_msg) {
2137 // ELF files produced by MCLinker look roughly like this
2138 //
2139 // +------------+
2140 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2141 // +------------+
2142 // | Elf_Phdr | program headers
2143 // | Elf_Phdr |
2144 // | ... |
2145 // | Elf_Phdr |
2146 // +------------+
2147 // | section | mixture of needed and unneeded sections
2148 // +------------+
2149 // | section |
2150 // +------------+
2151 // | ... |
2152 // +------------+
2153 // | section |
2154 // +------------+
2155 // | Elf_Shdr | section headers
2156 // | Elf_Shdr |
2157 // | ... | contains offset to section start
2158 // | Elf_Shdr |
2159 // +------------+
2160 //
2161 // To strip:
2162 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2163 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2164 // - move the sections are keeping up to fill in gaps of sections we want to strip
2165 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2166 // - truncate rest of file
2167 //
2168
2169 std::vector<Elf_Shdr> section_headers;
2170 std::vector<Elf_Word> section_headers_original_indexes;
2171 section_headers.reserve(GetSectionHeaderNum());
2172
2173
2174 Elf_Shdr* string_section = GetSectionNameStringSection();
2175 CHECK(string_section != nullptr);
2176 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2177 Elf_Shdr* sh = GetSectionHeader(i);
2178 CHECK(sh != nullptr);
2179 const char* name = GetString(*string_section, sh->sh_name);
2180 if (name == nullptr) {
2181 CHECK_EQ(0U, i);
2182 section_headers.push_back(*sh);
2183 section_headers_original_indexes.push_back(0);
2184 continue;
2185 }
2186 if (StartsWith(name, ".debug")
2187 || (strcmp(name, ".strtab") == 0)
2188 || (strcmp(name, ".symtab") == 0)) {
2189 continue;
2190 }
2191 section_headers.push_back(*sh);
2192 section_headers_original_indexes.push_back(i);
2193 }
2194 CHECK_NE(0U, section_headers.size());
2195 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2196
2197 // section 0 is the NULL section, sections start at offset of first section
2198 CHECK(GetSectionHeader(1) != nullptr);
2199 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2200 for (size_t i = 1; i < section_headers.size(); i++) {
2201 Elf_Shdr& new_sh = section_headers[i];
2202 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2203 CHECK(old_sh != nullptr);
2204 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2205 if (old_sh->sh_addralign > 1) {
2206 offset = RoundUp(offset, old_sh->sh_addralign);
2207 }
2208 if (old_sh->sh_offset == offset) {
2209 // already in place
2210 offset += old_sh->sh_size;
2211 continue;
2212 }
2213 // shift section earlier
2214 memmove(Begin() + offset,
2215 Begin() + old_sh->sh_offset,
2216 old_sh->sh_size);
2217 new_sh.sh_offset = offset;
2218 offset += old_sh->sh_size;
2219 }
2220
2221 Elf_Off shoff = offset;
2222 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2223 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2224 offset += section_headers_size_in_bytes;
2225
2226 GetHeader().e_shnum = section_headers.size();
2227 GetHeader().e_shoff = shoff;
2228 int result = ftruncate(file_->Fd(), offset);
2229 if (result != 0) {
2230 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2231 file_->GetPath().c_str(), strerror(errno));
2232 return false;
2233 }
2234 return true;
2235}
2236
2237static const bool DEBUG_FIXUP = false;
2238
2239template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2240 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2241 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2242bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2243 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2244 ::Fixup(uintptr_t base_address) {
2245 if (!FixupDynamic(base_address)) {
2246 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2247 return false;
2248 }
2249 if (!FixupSectionHeaders(base_address)) {
2250 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2251 return false;
2252 }
2253 if (!FixupProgramHeaders(base_address)) {
2254 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2255 return false;
2256 }
2257 if (!FixupSymbols(base_address, true)) {
2258 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2259 return false;
2260 }
2261 if (!FixupSymbols(base_address, false)) {
2262 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2263 return false;
2264 }
2265 if (!FixupRelocations(base_address)) {
2266 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2267 return false;
2268 }
2269 if (!FixupDebugSections(base_address)) {
2270 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2271 return false;
2272 }
2273 return true;
2274}
2275
2276template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2277 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2278 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2279bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2280 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2281 ::FixupDynamic(uintptr_t base_address) {
2282 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2283 Elf_Dyn& elf_dyn = GetDynamic(i);
2284 Elf_Word d_tag = elf_dyn.d_tag;
2285 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2286 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2287 if (DEBUG_FIXUP) {
2288 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2289 GetFile().GetPath().c_str(), i,
2290 static_cast<uint64_t>(d_ptr),
2291 static_cast<uint64_t>(d_ptr + base_address));
2292 }
2293 d_ptr += base_address;
2294 elf_dyn.d_un.d_ptr = d_ptr;
2295 }
2296 }
2297 return true;
2298}
2299
2300template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2301 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2302 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2303bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2304 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2305 ::FixupSectionHeaders(uintptr_t base_address) {
2306 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2307 Elf_Shdr* sh = GetSectionHeader(i);
2308 CHECK(sh != nullptr);
2309 // 0 implies that the section will not exist in the memory of the process
2310 if (sh->sh_addr == 0) {
2311 continue;
2312 }
2313 if (DEBUG_FIXUP) {
2314 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2315 GetFile().GetPath().c_str(), i,
2316 static_cast<uint64_t>(sh->sh_addr),
2317 static_cast<uint64_t>(sh->sh_addr + base_address));
2318 }
2319 sh->sh_addr += base_address;
2320 }
2321 return true;
2322}
2323
2324template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2325 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2326 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2327bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2328 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2329 ::FixupProgramHeaders(uintptr_t base_address) {
2330 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2331 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2332 Elf_Phdr* ph = GetProgramHeader(i);
2333 CHECK(ph != nullptr);
2334 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2335 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2336 << GetFile().GetPath() << " i=" << i;
2337 if (DEBUG_FIXUP) {
2338 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2339 GetFile().GetPath().c_str(), i,
2340 static_cast<uint64_t>(ph->p_vaddr),
2341 static_cast<uint64_t>(ph->p_vaddr + base_address));
2342 }
2343 ph->p_vaddr += base_address;
2344 ph->p_paddr += base_address;
2345 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2346 << GetFile().GetPath() << " i=" << i;
2347 }
2348 return true;
2349}
2350
2351template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2352 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2353 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2354bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2355 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2356 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2357 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2358 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2359 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2360 if (symbol_section == nullptr) {
2361 // file is missing optional .symtab
2362 CHECK(!dynamic) << GetFile().GetPath();
2363 return true;
2364 }
2365 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2366 Elf_Sym* symbol = GetSymbol(section_type, i);
2367 CHECK(symbol != nullptr);
2368 if (symbol->st_value != 0) {
2369 if (DEBUG_FIXUP) {
2370 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2371 GetFile().GetPath().c_str(), i,
2372 static_cast<uint64_t>(symbol->st_value),
2373 static_cast<uint64_t>(symbol->st_value + base_address));
2374 }
2375 symbol->st_value += base_address;
2376 }
2377 }
2378 return true;
2379}
2380
2381template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2382 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2383 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2384bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2385 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2386 ::FixupRelocations(uintptr_t base_address) {
2387 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2388 Elf_Shdr* sh = GetSectionHeader(i);
2389 CHECK(sh != nullptr);
2390 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002391 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
2392 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002393 if (DEBUG_FIXUP) {
2394 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002395 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002396 static_cast<uint64_t>(rel.r_offset),
2397 static_cast<uint64_t>(rel.r_offset + base_address));
2398 }
2399 rel.r_offset += base_address;
2400 }
2401 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002402 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
2403 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002404 if (DEBUG_FIXUP) {
2405 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002406 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002407 static_cast<uint64_t>(rela.r_offset),
2408 static_cast<uint64_t>(rela.r_offset + base_address));
2409 }
2410 rela.r_offset += base_address;
2411 }
2412 }
2413 }
2414 return true;
2415}
2416
2417// Explicit instantiations
2418template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2419 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2420template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2421 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2422
Ian Rogersd4c4d952014-10-16 20:31:53 -07002423ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002424}
2425
Ian Rogersd4c4d952014-10-16 20:31:53 -07002426ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002427}
2428
2429ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002430 // Should never have 32 and 64-bit impls.
2431 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002432}
2433
Igor Murashkin46774762014-10-22 11:37:02 -07002434ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2435 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002436 if (file->GetLength() < EI_NIDENT) {
2437 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2438 file->GetPath().c_str());
2439 return nullptr;
2440 }
2441 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2442 file->GetPath().c_str(), error_msg));
2443 if (map == nullptr && map->Size() != EI_NIDENT) {
2444 return nullptr;
2445 }
Ian Rogers13735952014-10-08 12:43:28 -07002446 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002447 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002448 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2449 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002450 if (elf_file_impl == nullptr)
2451 return nullptr;
2452 return new ElfFile(elf_file_impl);
2453 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002454 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2455 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002456 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002457 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002458 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002459 return new ElfFile(elf_file_impl);
2460 } else {
2461 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2462 ELFCLASS32, ELFCLASS64,
2463 file->GetPath().c_str(),
2464 header[EI_CLASS]);
2465 return nullptr;
2466 }
2467}
2468
2469ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2470 if (file->GetLength() < EI_NIDENT) {
2471 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2472 file->GetPath().c_str());
2473 return nullptr;
2474 }
2475 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2476 file->GetPath().c_str(), error_msg));
2477 if (map == nullptr && map->Size() != EI_NIDENT) {
2478 return nullptr;
2479 }
Ian Rogers13735952014-10-08 12:43:28 -07002480 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002481 if (header[EI_CLASS] == ELFCLASS64) {
2482 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002483 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002484 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002485 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002486 return new ElfFile(elf_file_impl);
2487 } else if (header[EI_CLASS] == ELFCLASS32) {
2488 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002489 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002490 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002491 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002492 return new ElfFile(elf_file_impl);
2493 } else {
2494 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2495 ELFCLASS32, ELFCLASS64,
2496 file->GetPath().c_str(),
2497 header[EI_CLASS]);
2498 return nullptr;
2499 }
2500}
2501
2502#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002503 if (elf64_.get() != nullptr) { \
2504 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002505 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002506 DCHECK(elf32_.get() != nullptr); \
2507 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002508 }
2509
2510bool ElfFile::Load(bool executable, std::string* error_msg) {
2511 DELEGATE_TO_IMPL(Load, executable, error_msg);
2512}
2513
Ian Rogers13735952014-10-08 12:43:28 -07002514const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002515 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2516}
2517
2518size_t ElfFile::Size() const {
2519 DELEGATE_TO_IMPL(Size);
2520}
2521
Ian Rogers13735952014-10-08 12:43:28 -07002522uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002523 DELEGATE_TO_IMPL(Begin);
2524}
2525
Ian Rogers13735952014-10-08 12:43:28 -07002526uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002527 DELEGATE_TO_IMPL(End);
2528}
2529
2530const File& ElfFile::GetFile() const {
2531 DELEGATE_TO_IMPL(GetFile);
2532}
2533
2534bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002535 if (elf32_.get() == nullptr) {
2536 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002537
Ian Rogersd4c4d952014-10-16 20:31:53 -07002538 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2539 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002540 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002541 }
2542 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002543 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002544 }
2545 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002546 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002547 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002548 return true;
2549 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002550 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2551 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002552 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002553 }
2554 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002555 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002556 }
2557 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002558 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002559 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002560 return true;
2561 }
2562}
2563
2564uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2565 const std::string& symbol_name,
2566 bool build_map) {
2567 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2568}
2569
2570size_t ElfFile::GetLoadedSize() const {
2571 DELEGATE_TO_IMPL(GetLoadedSize);
2572}
2573
2574bool ElfFile::Strip(File* file, std::string* error_msg) {
2575 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2576 if (elf_file.get() == nullptr) {
2577 return false;
2578 }
2579
Ian Rogersd4c4d952014-10-16 20:31:53 -07002580 if (elf_file->elf64_.get() != nullptr)
2581 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002582 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002583 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002584}
2585
2586bool ElfFile::Fixup(uintptr_t base_address) {
2587 DELEGATE_TO_IMPL(Fixup, base_address);
2588}
2589
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002590} // namespace art