blob: 4198905e23e7297f5f7fd385b492c85425e84914 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "elf_file.h"
18
Tong Shen62d1ca32014-09-03 17:24:56 -070019#include <inttypes.h>
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000020#include <sys/types.h>
21#include <unistd.h>
22
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "base/stl_util.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070027#include "base/unix_file/fd_file.h"
Alex Light3470ab42014-06-18 10:35:45 -070028#include "dwarf.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070029#include "elf_file_impl.h"
30#include "elf_utils.h"
Alex Light3470ab42014-06-18 10:35:45 -070031#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032#include "utils.h"
33
34namespace art {
35
Mark Mendellae9fd932014-02-10 16:14:35 -080036// -------------------------------------------------------------------
37// Binary GDB JIT Interface as described in
38// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
39extern "C" {
40 typedef enum {
41 JIT_NOACTION = 0,
42 JIT_REGISTER_FN,
43 JIT_UNREGISTER_FN
44 } JITAction;
45
46 struct JITCodeEntry {
47 JITCodeEntry* next_;
48 JITCodeEntry* prev_;
Ian Rogers13735952014-10-08 12:43:28 -070049 const uint8_t *symfile_addr_;
Mark Mendellae9fd932014-02-10 16:14:35 -080050 uint64_t symfile_size_;
51 };
52
53 struct JITDescriptor {
54 uint32_t version_;
55 uint32_t action_flag_;
56 JITCodeEntry* relevant_entry_;
57 JITCodeEntry* first_entry_;
58 };
59
60 // GDB will place breakpoint into this function.
61 // To prevent GCC from inlining or removing it we place noinline attribute
62 // and inline assembler statement inside.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080063 void __attribute__((noinline)) __jit_debug_register_code();
Mark Mendellae9fd932014-02-10 16:14:35 -080064 void __attribute__((noinline)) __jit_debug_register_code() {
65 __asm__("");
66 }
67
68 // GDB will inspect contents of this descriptor.
69 // Static initialization is necessary to prevent GDB from seeing
70 // uninitialized descriptor.
71 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
72}
73
74
Ian Rogers13735952014-10-08 12:43:28 -070075static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr,
Mark Mendellae9fd932014-02-10 16:14:35 -080076 uintptr_t symfile_size) {
77 JITCodeEntry* entry = new JITCodeEntry;
78 entry->symfile_addr_ = symfile_addr;
79 entry->symfile_size_ = symfile_size;
80 entry->prev_ = nullptr;
81
82 // TODO: Do we need a lock here?
83 entry->next_ = __jit_debug_descriptor.first_entry_;
84 if (entry->next_ != nullptr) {
85 entry->next_->prev_ = entry;
86 }
87 __jit_debug_descriptor.first_entry_ = entry;
88 __jit_debug_descriptor.relevant_entry_ = entry;
89
90 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
91 __jit_debug_register_code();
92 return entry;
93}
94
95
96static void UnregisterCodeEntry(JITCodeEntry* entry) {
97 // TODO: Do we need a lock here?
98 if (entry->prev_ != nullptr) {
99 entry->prev_->next_ = entry->next_;
100 } else {
101 __jit_debug_descriptor.first_entry_ = entry->next_;
102 }
103
104 if (entry->next_ != nullptr) {
105 entry->next_->prev_ = entry->prev_;
106 }
107
108 __jit_debug_descriptor.relevant_entry_ = entry;
109 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
110 __jit_debug_register_code();
111 delete entry;
112}
113
Tong Shen62d1ca32014-09-03 17:24:56 -0700114template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
115 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
116 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
117ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
118 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700119 ::ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base)
Brian Carlstromc1409452014-02-26 14:06:23 -0800120 : file_(file),
121 writable_(writable),
122 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700123 header_(nullptr),
124 base_address_(nullptr),
125 program_headers_start_(nullptr),
126 section_headers_start_(nullptr),
127 dynamic_program_header_(nullptr),
128 dynamic_section_start_(nullptr),
129 symtab_section_start_(nullptr),
130 dynsym_section_start_(nullptr),
131 strtab_section_start_(nullptr),
132 dynstr_section_start_(nullptr),
133 hash_section_start_(nullptr),
134 symtab_symbol_table_(nullptr),
135 dynsym_symbol_table_(nullptr),
136 jit_elf_image_(nullptr),
Igor Murashkin46774762014-10-22 11:37:02 -0700137 jit_gdb_entry_(nullptr),
138 requested_base_(requested_base) {
Alex Light3470ab42014-06-18 10:35:45 -0700139 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800140}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800141
Tong Shen62d1ca32014-09-03 17:24:56 -0700142template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
143 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
144 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
145ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
146 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
147 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
148 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
149 ::Open(File* file, bool writable, bool program_header_only,
Igor Murashkin46774762014-10-22 11:37:02 -0700150 std::string* error_msg, uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700151 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
152 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
153 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
154 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700155 (file, writable, program_header_only, requested_base));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156 int prot;
157 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700158 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 prot = PROT_READ | PROT_WRITE;
160 flags = MAP_SHARED;
161 } else {
162 prot = PROT_READ;
163 flags = MAP_PRIVATE;
164 }
Alex Light3470ab42014-06-18 10:35:45 -0700165 if (!elf_file->Setup(prot, flags, error_msg)) {
166 return nullptr;
167 }
168 return elf_file.release();
169}
170
Tong Shen62d1ca32014-09-03 17:24:56 -0700171template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
172 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
173 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
174ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
175 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>*
176 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
177 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
178 ::Open(File* file, int prot, int flags, std::string* error_msg) {
179 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
180 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
181 elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
182 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Igor Murashkin46774762014-10-22 11:37:02 -0700183 (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
184 /*requested_base*/nullptr));
Alex Light3470ab42014-06-18 10:35:45 -0700185 if (!elf_file->Setup(prot, flags, error_msg)) {
186 return nullptr;
187 }
188 return elf_file.release();
189}
190
Tong Shen62d1ca32014-09-03 17:24:56 -0700191template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
192 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
193 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
194bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
195 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
196 ::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800197 int64_t temp_file_length = file_->GetLength();
198 if (temp_file_length < 0) {
199 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
201 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800202 return false;
203 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800204 size_t file_length = static_cast<size_t>(temp_file_length);
Tong Shen62d1ca32014-09-03 17:24:56 -0700205 if (file_length < sizeof(Elf_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800206 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Tong Shen62d1ca32014-09-03 17:24:56 -0700207 "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700208 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 return false;
210 }
211
Brian Carlstromc1409452014-02-26 14:06:23 -0800212 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800213 // first just map ELF header to get program header size information
Tong Shen62d1ca32014-09-03 17:24:56 -0700214 size_t elf_header_size = sizeof(Elf_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800216 file_->GetPath().c_str(), error_msg),
217 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800218 return false;
219 }
220 // then remap to cover program header
221 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700222 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800223 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700224 "header of %zd bytes: '%s'", file_length,
Tong Shen62d1ca32014-09-03 17:24:56 -0700225 sizeof(Elf_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700226 return false;
227 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700228 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800229 file_->GetPath().c_str(), error_msg),
230 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700231 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232 return false;
233 }
234 } else {
235 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700236 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800237 file_->GetPath().c_str(), error_msg),
238 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700239 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800240 return false;
241 }
242 }
243
Andreas Gampedaab38c2014-09-12 18:38:24 -0700244 if (program_header_only_) {
245 program_headers_start_ = Begin() + GetHeader().e_phoff;
246 } else {
247 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
248 return false;
249 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800250
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800251 // Setup section headers.
Andreas Gampedaab38c2014-09-12 18:38:24 -0700252 if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
253 return false;
254 }
255
256 // Find shstrtab.
Tong Shen62d1ca32014-09-03 17:24:56 -0700257 Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700258 if (shstrtab_section_header == nullptr) {
259 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
260 file_->GetPath().c_str());
261 return false;
262 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800263
264 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000265 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700266 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700267 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
268 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800269 return false;
270 }
271
Andreas Gampedaab38c2014-09-12 18:38:24 -0700272 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
Ian Rogers13735952014-10-08 12:43:28 -0700273 reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700274 return false;
275 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800276
277 // Find other sections from section headers
Tong Shen62d1ca32014-09-03 17:24:56 -0700278 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
279 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700280 if (section_header == nullptr) {
281 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
282 i, file_->GetPath().c_str());
283 return false;
284 }
285 switch (section_header->sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000286 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700287 if (!CheckAndSet(section_header->sh_offset, "symtab",
Ian Rogers13735952014-10-08 12:43:28 -0700288 reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700289 return false;
290 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800291 break;
292 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000293 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700294 if (!CheckAndSet(section_header->sh_offset, "dynsym",
Ian Rogers13735952014-10-08 12:43:28 -0700295 reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700296 return false;
297 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800298 break;
299 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000300 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800301 // TODO: base these off of sh_link from .symtab and .dynsym above
Andreas Gampedaab38c2014-09-12 18:38:24 -0700302 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
303 // Check that this is named ".dynstr" and ignore otherwise.
304 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
305 if (strncmp(".dynstr", header_name, 8) == 0) {
306 if (!CheckAndSet(section_header->sh_offset, "dynstr",
Ian Rogers13735952014-10-08 12:43:28 -0700307 reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700308 return false;
309 }
310 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800311 } else {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700312 // Check that this is named ".strtab" and ignore otherwise.
313 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
314 if (strncmp(".strtab", header_name, 8) == 0) {
315 if (!CheckAndSet(section_header->sh_offset, "strtab",
Ian Rogers13735952014-10-08 12:43:28 -0700316 reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700317 return false;
318 }
319 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800320 }
321 break;
322 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000323 case SHT_DYNAMIC: {
Ian Rogers13735952014-10-08 12:43:28 -0700324 if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
Andreas Gampedaab38c2014-09-12 18:38:24 -0700325 Begin() + section_header->sh_offset) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800326 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800327 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800328 << reinterpret_cast<void*>(dynamic_section_start_)
Andreas Gampedaab38c2014-09-12 18:38:24 -0700329 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800330 return false;
331 }
332 break;
333 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000334 case SHT_HASH: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700335 if (!CheckAndSet(section_header->sh_offset, "hash section",
Ian Rogers13735952014-10-08 12:43:28 -0700336 reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700337 return false;
338 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800339 break;
340 }
341 }
342 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700343
344 // Check for the existence of some sections.
345 if (!CheckSectionsExist(error_msg)) {
346 return false;
347 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800348 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700349
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800350 return true;
351}
352
Tong Shen62d1ca32014-09-03 17:24:56 -0700353template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
354 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
355 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
356ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
357 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
358 ::~ElfFileImpl() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800359 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800360 delete symtab_symbol_table_;
361 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800362 delete jit_elf_image_;
363 if (jit_gdb_entry_) {
364 UnregisterCodeEntry(jit_gdb_entry_);
365 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800366}
367
Tong Shen62d1ca32014-09-03 17:24:56 -0700368template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
369 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
370 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
371bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
372 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
373 ::CheckAndSet(Elf32_Off offset, const char* label,
Ian Rogers13735952014-10-08 12:43:28 -0700374 uint8_t** target, std::string* error_msg) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700375 if (Begin() + offset >= End()) {
376 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
377 file_->GetPath().c_str());
378 return false;
379 }
380 *target = Begin() + offset;
381 return true;
382}
383
Tong Shen62d1ca32014-09-03 17:24:56 -0700384template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
385 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
386 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
387bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
388 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700389 ::CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700390 // Only works in whole-program mode, as we need to iterate over the sections.
391 // Note that we normally can't search by type, as duplicates are allowed for most section types.
392 if (program_header_only_) {
393 return true;
394 }
395
Tong Shen62d1ca32014-09-03 17:24:56 -0700396 Elf_Shdr* source_section = nullptr;
397 Elf_Word target_index = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700398 bool target_found = false;
Tong Shen62d1ca32014-09-03 17:24:56 -0700399 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
400 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700401
402 if (Begin() + section_header->sh_offset == source) {
403 // Found the source.
404 source_section = section_header;
405 if (target_index) {
406 break;
407 }
408 } else if (Begin() + section_header->sh_offset == target) {
409 target_index = i;
410 target_found = true;
411 if (source_section != nullptr) {
412 break;
413 }
414 }
415 }
416
417 return target_found && source_section != nullptr && source_section->sh_link == target_index;
418}
419
Tong Shen62d1ca32014-09-03 17:24:56 -0700420template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
421 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
422 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
423bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
424 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
425 ::CheckSectionsExist(std::string* error_msg) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700426 if (!program_header_only_) {
427 // If in full mode, need section headers.
428 if (section_headers_start_ == nullptr) {
429 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
430 return false;
431 }
432 }
433
434 // This is redundant, but defensive.
435 if (dynamic_program_header_ == nullptr) {
436 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
437 file_->GetPath().c_str());
438 return false;
439 }
440
441 // Need a dynamic section. This is redundant, but defensive.
442 if (dynamic_section_start_ == nullptr) {
443 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
444 file_->GetPath().c_str());
445 return false;
446 }
447
448 // Symtab validation. These is not really a hard failure, as we are currently not using the
449 // symtab internally, but it's nice to be defensive.
450 if (symtab_section_start_ != nullptr) {
451 // When there's a symtab, there should be a strtab.
452 if (strtab_section_start_ == nullptr) {
453 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
454 return false;
455 }
456
457 // The symtab should link to the strtab.
Ian Rogers13735952014-10-08 12:43:28 -0700458 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
459 reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700460 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
461 file_->GetPath().c_str());
462 return false;
463 }
464 }
465
466 // We always need a dynstr & dynsym.
467 if (dynstr_section_start_ == nullptr) {
468 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
469 return false;
470 }
471 if (dynsym_section_start_ == nullptr) {
472 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
473 return false;
474 }
475
476 // Need a hash section for dynamic symbol lookup.
477 if (hash_section_start_ == nullptr) {
478 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
479 file_->GetPath().c_str());
480 return false;
481 }
482
483 // And the hash section should be linking to the dynsym.
Ian Rogers13735952014-10-08 12:43:28 -0700484 if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
485 reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700486 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
487 file_->GetPath().c_str());
488 return false;
489 }
490
Andreas Gampea696c0a2014-12-10 20:51:45 -0800491 // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
492 // us). This is usually the last in an oat file, and a good indicator of whether writing was
493 // successful (or the process crashed and left garbage).
494 if (program_header_only_) {
495 // It might not be mapped, but we can compare against the file size.
496 int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
497 (GetHeader().e_shstrndx * GetHeader().e_shentsize));
498 if (offset >= file_->GetLength()) {
499 *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
500 file_->GetPath().c_str());
501 return false;
502 }
503 }
504
Andreas Gampedaab38c2014-09-12 18:38:24 -0700505 return true;
506}
507
Tong Shen62d1ca32014-09-03 17:24:56 -0700508template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
509 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
510 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
511bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
512 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
513 ::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700514 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800515 // MemMap::Open should have already set an error.
516 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800517 return false;
518 }
519 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700520 CHECK(map_.get() != nullptr) << file_->GetPath();
521 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800522
Tong Shen62d1ca32014-09-03 17:24:56 -0700523 header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000524 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
525 || (ELFMAG1 != header_->e_ident[EI_MAG1])
526 || (ELFMAG2 != header_->e_ident[EI_MAG2])
527 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800528 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
529 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800530 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000531 header_->e_ident[EI_MAG0],
532 header_->e_ident[EI_MAG1],
533 header_->e_ident[EI_MAG2],
534 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800535 return false;
536 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700537 uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
538 if (elf_class != header_->e_ident[EI_CLASS]) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800539 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
Tong Shen62d1ca32014-09-03 17:24:56 -0700540 elf_class,
Brian Carlstromc1409452014-02-26 14:06:23 -0800541 file_->GetPath().c_str(),
542 header_->e_ident[EI_CLASS]);
543 return false;
544 }
545 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
546 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
547 ELFDATA2LSB,
548 file_->GetPath().c_str(),
549 header_->e_ident[EI_CLASS]);
550 return false;
551 }
552 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
553 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
554 EV_CURRENT,
555 file_->GetPath().c_str(),
556 header_->e_ident[EI_CLASS]);
557 return false;
558 }
559 if (ET_DYN != header_->e_type) {
560 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
561 ET_DYN,
562 file_->GetPath().c_str(),
563 header_->e_type);
564 return false;
565 }
566 if (EV_CURRENT != header_->e_version) {
567 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
568 EV_CURRENT,
569 file_->GetPath().c_str(),
570 header_->e_version);
571 return false;
572 }
573 if (0 != header_->e_entry) {
574 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
575 0,
576 file_->GetPath().c_str(),
Tong Shen62d1ca32014-09-03 17:24:56 -0700577 static_cast<int32_t>(header_->e_entry));
Brian Carlstromc1409452014-02-26 14:06:23 -0800578 return false;
579 }
580 if (0 == header_->e_phoff) {
581 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
582 file_->GetPath().c_str());
583 return false;
584 }
585 if (0 == header_->e_shoff) {
586 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
587 file_->GetPath().c_str());
588 return false;
589 }
590 if (0 == header_->e_ehsize) {
591 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
592 file_->GetPath().c_str());
593 return false;
594 }
595 if (0 == header_->e_phentsize) {
596 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
597 file_->GetPath().c_str());
598 return false;
599 }
600 if (0 == header_->e_phnum) {
601 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
602 file_->GetPath().c_str());
603 return false;
604 }
605 if (0 == header_->e_shentsize) {
606 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
607 file_->GetPath().c_str());
608 return false;
609 }
610 if (0 == header_->e_shnum) {
611 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
612 file_->GetPath().c_str());
613 return false;
614 }
615 if (0 == header_->e_shstrndx) {
616 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
617 file_->GetPath().c_str());
618 return false;
619 }
620 if (header_->e_shstrndx >= header_->e_shnum) {
621 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
622 header_->e_shstrndx,
623 header_->e_shnum,
624 file_->GetPath().c_str());
625 return false;
626 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800627
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800628 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800629 if (header_->e_phoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700630 *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
631 static_cast<uint64_t>(header_->e_phoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800632 Size(),
633 file_->GetPath().c_str());
634 return false;
635 }
636 if (header_->e_shoff >= Size()) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700637 *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
638 static_cast<uint64_t>(header_->e_shoff),
Brian Carlstromc1409452014-02-26 14:06:23 -0800639 Size(),
640 file_->GetPath().c_str());
641 return false;
642 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800643 }
644 return true;
645}
646
Tong Shen62d1ca32014-09-03 17:24:56 -0700647template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
648 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
649 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
650Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
651 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
652 ::GetHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700653 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800654 return *header_;
655}
656
Tong Shen62d1ca32014-09-03 17:24:56 -0700657template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
658 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
659 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700660uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700661 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
662 ::GetProgramHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700663 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
664 // check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800665 return program_headers_start_;
666}
667
Tong Shen62d1ca32014-09-03 17:24:56 -0700668template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
669 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
670 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700671uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700672 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
673 ::GetSectionHeadersStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700674 CHECK(!program_header_only_); // Only used in "full" mode.
675 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800676 return section_headers_start_;
677}
678
Tong Shen62d1ca32014-09-03 17:24:56 -0700679template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
680 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
681 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
682Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
683 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
684 ::GetDynamicProgramHeader() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700685 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800686 return *dynamic_program_header_;
687}
688
Tong Shen62d1ca32014-09-03 17:24:56 -0700689template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
690 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
691 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
692Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
693 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
694 ::GetDynamicSectionStart() const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700695 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800696 return dynamic_section_start_;
697}
698
Tong Shen62d1ca32014-09-03 17:24:56 -0700699template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
700 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
701 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
702Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
703 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
704 ::GetSymbolSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800705 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800706 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000707 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700708 return symtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800709 break;
710 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000711 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700712 return dynsym_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800713 break;
714 }
715 default: {
716 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700717 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800718 }
719 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800720}
721
Tong Shen62d1ca32014-09-03 17:24:56 -0700722template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
723 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
724 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
725const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
726 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
727 ::GetStringSectionStart(Elf_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800728 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800729 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000730 case SHT_SYMTAB: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700731 return strtab_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800732 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000733 case SHT_DYNSYM: {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700734 return dynstr_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800735 }
736 default: {
737 LOG(FATAL) << section_type;
Andreas Gampedaab38c2014-09-12 18:38:24 -0700738 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800739 }
740 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800741}
742
Tong Shen62d1ca32014-09-03 17:24:56 -0700743template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
744 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
745 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
746const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
747 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
748 ::GetString(Elf_Word section_type, Elf_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800749 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
750 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700751 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800752 }
753 const char* string_section_start = GetStringSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700754 if (string_section_start == nullptr) {
755 return nullptr;
756 }
757 return string_section_start + i;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800758}
759
Andreas Gampedaab38c2014-09-12 18:38:24 -0700760// WARNING: The following methods do not check for an error condition (non-existent hash section).
761// It is the caller's job to do this.
762
Tong Shen62d1ca32014-09-03 17:24:56 -0700763template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
764 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
765 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
766Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
767 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
768 ::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800769 return hash_section_start_;
770}
771
Tong Shen62d1ca32014-09-03 17:24:56 -0700772template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
773 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
774 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
775Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
776 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
777 ::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800778 return GetHashSectionStart()[0];
779}
780
Tong Shen62d1ca32014-09-03 17:24:56 -0700781template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
782 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
783 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
784Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
785 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
786 ::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800787 return GetHashSectionStart()[1];
788}
789
Tong Shen62d1ca32014-09-03 17:24:56 -0700790template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
791 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
792 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
793Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
794 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
795 ::GetHashBucket(size_t i, bool* ok) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700796 if (i >= GetHashBucketNum()) {
797 *ok = false;
798 return 0;
799 }
800 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800801 // 0 is nbucket, 1 is nchain
802 return GetHashSectionStart()[2 + i];
803}
804
Tong Shen62d1ca32014-09-03 17:24:56 -0700805template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
806 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
807 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
808Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
809 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
810 ::GetHashChain(size_t i, bool* ok) const {
Yevgeny Roubanacb01382014-11-24 13:40:56 +0600811 if (i >= GetHashChainNum()) {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700812 *ok = false;
813 return 0;
814 }
815 *ok = true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800816 // 0 is nbucket, 1 is nchain, & chains are after buckets
817 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
818}
819
Tong Shen62d1ca32014-09-03 17:24:56 -0700820template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
821 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
822 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
823Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
824 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
825 ::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800826 return GetHeader().e_phnum;
827}
828
Tong Shen62d1ca32014-09-03 17:24:56 -0700829template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
830 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
831 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
832Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
833 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
834 ::GetProgramHeader(Elf_Word i) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700835 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
Ian Rogers13735952014-10-08 12:43:28 -0700836 uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700837 if (program_header >= End()) {
838 return nullptr; // Failure condition.
839 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700840 return reinterpret_cast<Elf_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800841}
842
Tong Shen62d1ca32014-09-03 17:24:56 -0700843template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
844 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
845 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
846Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
847 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
848 ::FindProgamHeaderByType(Elf_Word type) const {
849 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
850 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700851 if (program_header->p_type == type) {
852 return program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800853 }
854 }
Alex Light3470ab42014-06-18 10:35:45 -0700855 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800856}
857
Tong Shen62d1ca32014-09-03 17:24:56 -0700858template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
859 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
860 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
861Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
862 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
863 ::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800864 return GetHeader().e_shnum;
865}
866
Tong Shen62d1ca32014-09-03 17:24:56 -0700867template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
868 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
869 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
870Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
871 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
872 ::GetSectionHeader(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800873 // Can only access arbitrary sections when we have the whole file, not just program header.
874 // Even if we Load(), it doesn't bring in all the sections.
875 CHECK(!program_header_only_) << file_->GetPath();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700876 if (i >= GetSectionHeaderNum()) {
877 return nullptr; // Failure condition.
878 }
Ian Rogers13735952014-10-08 12:43:28 -0700879 uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700880 if (section_header >= End()) {
881 return nullptr; // Failure condition.
882 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700883 return reinterpret_cast<Elf_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884}
885
Tong Shen62d1ca32014-09-03 17:24:56 -0700886template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
887 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
888 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
889Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
890 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
891 ::FindSectionByType(Elf_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800892 // Can only access arbitrary sections when we have the whole file, not just program header.
893 // We could change this to switch on known types if they were detected during loading.
894 CHECK(!program_header_only_) << file_->GetPath();
Tong Shen62d1ca32014-09-03 17:24:56 -0700895 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
896 Elf_Shdr* section_header = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700897 if (section_header->sh_type == type) {
898 return section_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800899 }
900 }
Alex Light3470ab42014-06-18 10:35:45 -0700901 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800902}
903
904// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800905static unsigned elfhash(const char *_name) {
906 const unsigned char *name = (const unsigned char *) _name;
907 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800908
Brian Carlstromdf629502013-07-17 22:39:56 -0700909 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800910 h = (h << 4) + *name++;
911 g = h & 0xf0000000;
912 h ^= g;
913 h ^= g >> 24;
914 }
915 return h;
916}
917
Tong Shen62d1ca32014-09-03 17:24:56 -0700918template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
919 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
920 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
921Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
922 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
923 ::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800924 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800925}
926
Tong Shen62d1ca32014-09-03 17:24:56 -0700927template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
928 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
929 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -0700930const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
Tong Shen62d1ca32014-09-03 17:24:56 -0700931 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
932 ::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Andreas Gampedaab38c2014-09-12 18:38:24 -0700933 // Check that we have a hash section.
934 if (GetHashSectionStart() == nullptr) {
935 return nullptr; // Failure condition.
936 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700937 const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
Alex Light3470ab42014-06-18 10:35:45 -0700938 if (sym != nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -0700939 // TODO: we need to change this to calculate base_address_ in ::Open,
940 // otherwise it will be wrongly 0 if ::Load has not yet been called.
Alex Light3470ab42014-06-18 10:35:45 -0700941 return base_address_ + sym->st_value;
942 } else {
943 return nullptr;
944 }
945}
946
Andreas Gampedaab38c2014-09-12 18:38:24 -0700947// WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
Tong Shen62d1ca32014-09-03 17:24:56 -0700948template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
949 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
950 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
951const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
952 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
953 ::FindDynamicSymbol(const std::string& symbol_name) const {
Andreas Gampec48b2062014-09-08 23:39:45 -0700954 if (GetHashBucketNum() == 0) {
955 // No dynamic symbols at all.
956 return nullptr;
957 }
Tong Shen62d1ca32014-09-03 17:24:56 -0700958 Elf_Word hash = elfhash(symbol_name.c_str());
959 Elf_Word bucket_index = hash % GetHashBucketNum();
Andreas Gampedaab38c2014-09-12 18:38:24 -0700960 bool ok;
Tong Shen62d1ca32014-09-03 17:24:56 -0700961 Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700962 if (!ok) {
963 return nullptr;
964 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800965 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Tong Shen62d1ca32014-09-03 17:24:56 -0700966 Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
Andreas Gampedaab38c2014-09-12 18:38:24 -0700967 if (symbol == nullptr) {
968 return nullptr; // Failure condition.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 }
Andreas Gampedaab38c2014-09-12 18:38:24 -0700970 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
971 if (symbol_name == name) {
972 return symbol;
973 }
974 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
975 if (!ok) {
976 return nullptr;
977 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800978 }
Alex Light3470ab42014-06-18 10:35:45 -0700979 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800980}
981
Tong Shen62d1ca32014-09-03 17:24:56 -0700982template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
983 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
984 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
985bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
986 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
987 ::IsSymbolSectionType(Elf_Word section_type) {
988 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
989}
990
991template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
992 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
993 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
994Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
995 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
996 ::GetSymbolNum(Elf_Shdr& section_header) const {
Brian Carlstromc1409452014-02-26 14:06:23 -0800997 CHECK(IsSymbolSectionType(section_header.sh_type))
998 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800999 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1000 return section_header.sh_size / section_header.sh_entsize;
1001}
1002
Tong Shen62d1ca32014-09-03 17:24:56 -07001003template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1004 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1005 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1006Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1007 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1008 ::GetSymbol(Elf_Word section_type,
1009 Elf_Word i) const {
1010 Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001011 if (sym_start == nullptr) {
1012 return nullptr;
1013 }
1014 return sym_start + i;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001015}
1016
Tong Shen62d1ca32014-09-03 17:24:56 -07001017template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1018 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1019 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1020typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1021 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1022 ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1023 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1024 ::GetSymbolTable(Elf_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001025 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
1026 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001027 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001028 return &symtab_symbol_table_;
1029 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001030 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001031 return &dynsym_symbol_table_;
1032 }
1033 default: {
1034 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -07001035 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001036 }
1037 }
1038}
1039
Tong Shen62d1ca32014-09-03 17:24:56 -07001040template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1041 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1042 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1043Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1044 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1045 ::FindSymbolByName(Elf_Word section_type,
1046 const std::string& symbol_name,
1047 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001048 CHECK(!program_header_only_) << file_->GetPath();
1049 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001050
1051 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -07001052 if (*symbol_table != nullptr || build_map) {
1053 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001054 DCHECK(build_map);
1055 *symbol_table = new SymbolTable;
Tong Shen62d1ca32014-09-03 17:24:56 -07001056 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001057 if (symbol_section == nullptr) {
1058 return nullptr; // Failure condition.
1059 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001060 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001061 if (string_section == nullptr) {
1062 return nullptr; // Failure condition.
1063 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001064 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001065 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001066 if (symbol == nullptr) {
1067 return nullptr; // Failure condition.
1068 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001069 unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
1070 ? ELF64_ST_TYPE(symbol->st_info)
1071 : ELF32_ST_TYPE(symbol->st_info);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001072 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001073 continue;
1074 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001075 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001076 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -08001077 continue;
1078 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001079 std::pair<typename SymbolTable::iterator, bool> result =
Andreas Gampedaab38c2014-09-12 18:38:24 -07001080 (*symbol_table)->insert(std::make_pair(name, symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -08001081 if (!result.second) {
1082 // If a duplicate, make sure it has the same logical value. Seen on x86.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001083 if ((symbol->st_value != result.first->second->st_value) ||
1084 (symbol->st_size != result.first->second->st_size) ||
1085 (symbol->st_info != result.first->second->st_info) ||
1086 (symbol->st_other != result.first->second->st_other) ||
1087 (symbol->st_shndx != result.first->second->st_shndx)) {
1088 return nullptr; // Failure condition.
1089 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001090 }
1091 }
1092 }
Alex Light3470ab42014-06-18 10:35:45 -07001093 CHECK(*symbol_table != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07001094 typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001095 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -07001096 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001097 }
1098 return it->second;
1099 }
1100
1101 // Fall back to linear search
Tong Shen62d1ca32014-09-03 17:24:56 -07001102 Elf_Shdr* symbol_section = FindSectionByType(section_type);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001103 if (symbol_section == nullptr) {
1104 return nullptr;
1105 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001106 Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001107 if (string_section == nullptr) {
1108 return nullptr;
1109 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001110 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001111 Elf_Sym* symbol = GetSymbol(section_type, i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001112 if (symbol == nullptr) {
1113 return nullptr; // Failure condition.
1114 }
1115 const char* name = GetString(*string_section, symbol->st_name);
Alex Light3470ab42014-06-18 10:35:45 -07001116 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001117 continue;
1118 }
1119 if (symbol_name == name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001120 return symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001121 }
1122 }
Alex Light3470ab42014-06-18 10:35:45 -07001123 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001124}
1125
Tong Shen62d1ca32014-09-03 17:24:56 -07001126template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1127 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1128 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1129Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1130 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1131 ::FindSymbolAddress(Elf_Word section_type,
1132 const std::string& symbol_name,
1133 bool build_map) {
1134 Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -07001135 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001136 return 0;
1137 }
1138 return symbol->st_value;
1139}
1140
Tong Shen62d1ca32014-09-03 17:24:56 -07001141template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1142 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1143 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1144const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1145 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1146 ::GetString(Elf_Shdr& string_section, Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001147 CHECK(!program_header_only_) << file_->GetPath();
1148 // TODO: remove this static_cast from enum when using -std=gnu++0x
Tong Shen62d1ca32014-09-03 17:24:56 -07001149 if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001150 return nullptr; // Failure condition.
1151 }
1152 if (i >= string_section.sh_size) {
1153 return nullptr;
1154 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001155 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -07001156 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001157 }
Ian Rogers13735952014-10-08 12:43:28 -07001158 uint8_t* strings = Begin() + string_section.sh_offset;
1159 uint8_t* string = strings + i;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001160 if (string >= End()) {
1161 return nullptr;
1162 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001163 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001164}
1165
Tong Shen62d1ca32014-09-03 17:24:56 -07001166template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1167 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1168 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1169Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1170 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1171 ::GetDynamicNum() const {
1172 return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001173}
1174
Tong Shen62d1ca32014-09-03 17:24:56 -07001175template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1176 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1177 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1178Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1179 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1180 ::GetDynamic(Elf_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001181 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
1182 return *(GetDynamicSectionStart() + i);
1183}
1184
Tong Shen62d1ca32014-09-03 17:24:56 -07001185template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1186 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1187 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1188Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1189 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1190 ::FindDynamicByType(Elf_Sword type) const {
1191 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1192 Elf_Dyn* dyn = &GetDynamic(i);
Alex Light53cb16b2014-06-12 11:26:29 -07001193 if (dyn->d_tag == type) {
1194 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001195 }
1196 }
Alex Light53cb16b2014-06-12 11:26:29 -07001197 return NULL;
1198}
1199
Tong Shen62d1ca32014-09-03 17:24:56 -07001200template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1201 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1202 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1203Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1204 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1205 ::FindDynamicValueByType(Elf_Sword type) const {
1206 Elf_Dyn* dyn = FindDynamicByType(type);
Alex Light53cb16b2014-06-12 11:26:29 -07001207 if (dyn == NULL) {
1208 return 0;
1209 } else {
1210 return dyn->d_un.d_val;
1211 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001212}
1213
Tong Shen62d1ca32014-09-03 17:24:56 -07001214template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1215 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1216 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1217Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1218 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1219 ::GetRelSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001220 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001221 return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001222}
1223
Tong Shen62d1ca32014-09-03 17:24:56 -07001224template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1225 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1226 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1227Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1228 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1229 ::GetRelNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001230 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001231 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
1232 return section_header.sh_size / section_header.sh_entsize;
1233}
1234
Tong Shen62d1ca32014-09-03 17:24:56 -07001235template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1236 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1237 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1238Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1239 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1240 ::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001241 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001242 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
1243 return *(GetRelSectionStart(section_header) + i);
1244}
1245
Tong Shen62d1ca32014-09-03 17:24:56 -07001246template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1247 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1248 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1249Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1250 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1251 ::GetRelaSectionStart(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001252 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Tong Shen62d1ca32014-09-03 17:24:56 -07001253 return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -08001254}
1255
Tong Shen62d1ca32014-09-03 17:24:56 -07001256template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1257 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1258 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1259Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1260 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1261 ::GetRelaNum(Elf_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001262 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001263 return section_header.sh_size / section_header.sh_entsize;
1264}
1265
Tong Shen62d1ca32014-09-03 17:24:56 -07001266template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1267 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1268 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1269Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1270 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1271 ::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001272 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -08001273 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
1274 return *(GetRelaSectionStart(section_header) + i);
1275}
1276
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001277// Base on bionic phdr_table_get_load_size
Tong Shen62d1ca32014-09-03 17:24:56 -07001278template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1279 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1280 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1281size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1282 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1283 ::GetLoadedSize() const {
1284 Elf_Addr min_vaddr = 0xFFFFFFFFu;
1285 Elf_Addr max_vaddr = 0x00000000u;
1286 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1287 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001288 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001289 continue;
1290 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001291 Elf_Addr begin_vaddr = program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001292 if (begin_vaddr < min_vaddr) {
1293 min_vaddr = begin_vaddr;
1294 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001295 Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001296 if (end_vaddr > max_vaddr) {
1297 max_vaddr = end_vaddr;
1298 }
1299 }
1300 min_vaddr = RoundDown(min_vaddr, kPageSize);
1301 max_vaddr = RoundUp(max_vaddr, kPageSize);
1302 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1303 size_t loaded_size = max_vaddr - min_vaddr;
1304 return loaded_size;
1305}
1306
Tong Shen62d1ca32014-09-03 17:24:56 -07001307template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1308 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1309 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1310bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1311 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1312 ::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001313 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -07001314
1315 if (executable) {
1316 InstructionSet elf_ISA = kNone;
1317 switch (GetHeader().e_machine) {
1318 case EM_ARM: {
1319 elf_ISA = kArm;
1320 break;
1321 }
1322 case EM_AARCH64: {
1323 elf_ISA = kArm64;
1324 break;
1325 }
1326 case EM_386: {
1327 elf_ISA = kX86;
1328 break;
1329 }
1330 case EM_X86_64: {
1331 elf_ISA = kX86_64;
1332 break;
1333 }
1334 case EM_MIPS: {
1335 elf_ISA = kMips;
1336 break;
1337 }
1338 }
1339
1340 if (elf_ISA != kRuntimeISA) {
1341 std::ostringstream oss;
1342 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1343 *error_msg = oss.str();
1344 return false;
1345 }
1346 }
1347
Jim_Guoa62a5882014-04-28 11:11:57 +08001348 bool reserved = false;
Tong Shen62d1ca32014-09-03 17:24:56 -07001349 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1350 Elf_Phdr* program_header = GetProgramHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001351 if (program_header == nullptr) {
1352 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1353 i, file_->GetPath().c_str());
1354 return false;
1355 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001356
1357 // Record .dynamic header information for later use
Andreas Gampedaab38c2014-09-12 18:38:24 -07001358 if (program_header->p_type == PT_DYNAMIC) {
1359 dynamic_program_header_ = program_header;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001360 continue;
1361 }
1362
1363 // Not something to load, move on.
Andreas Gampedaab38c2014-09-12 18:38:24 -07001364 if (program_header->p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001365 continue;
1366 }
1367
1368 // Found something to load.
1369
Jim_Guoa62a5882014-04-28 11:11:57 +08001370 // Before load the actual segments, reserve a contiguous chunk
1371 // of required size and address for all segments, but with no
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001372 // permissions. We'll then carve that up with the proper
1373 // permissions as we load the actual segments. If p_vaddr is
1374 // non-zero, the segments require the specific address specified,
1375 // which either was specified in the file because we already set
1376 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -08001377 int64_t temp_file_length = file_->GetLength();
1378 if (temp_file_length < 0) {
1379 errno = -temp_file_length;
1380 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1381 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1382 return false;
1383 }
1384 size_t file_length = static_cast<size_t>(temp_file_length);
Jim_Guoa62a5882014-04-28 11:11:57 +08001385 if (!reserved) {
Igor Murashkin46774762014-10-22 11:37:02 -07001386 uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1387 uint8_t* reserve_base_override = reserve_base;
1388 // Override the base (e.g. when compiling with --compile-pic)
1389 if (requested_base_ != nullptr) {
1390 reserve_base_override = requested_base_;
1391 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001392 std::string reservation_name("ElfFile reservation for ");
1393 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -07001394 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Igor Murashkin46774762014-10-22 11:37:02 -07001395 reserve_base_override,
Jim_Guoa62a5882014-04-28 11:11:57 +08001396 GetLoadedSize(), PROT_NONE, false,
1397 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001398 if (reserve.get() == nullptr) {
1399 *error_msg = StringPrintf("Failed to allocate %s: %s",
1400 reservation_name.c_str(), error_msg->c_str());
1401 return false;
1402 }
Jim_Guoa62a5882014-04-28 11:11:57 +08001403 reserved = true;
Igor Murashkin46774762014-10-22 11:37:02 -07001404
1405 // Base address is the difference of actual mapped location and the p_vaddr
1406 base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1407 - reinterpret_cast<uintptr_t>(reserve_base));
1408 // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1409 // dynamic memory address of where that object is actually mapped
1410 //
1411 // TODO: base_address_ needs to be calculated in ::Open, otherwise
1412 // FindDynamicSymbolAddress returns the wrong values until Load is called.
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001413 segments_.push_back(reserve.release());
1414 }
1415 // empty segment, nothing to map
Andreas Gampedaab38c2014-09-12 18:38:24 -07001416 if (program_header->p_memsz == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001417 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001418 }
Ian Rogers13735952014-10-08 12:43:28 -07001419 uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001420 int prot = 0;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001421 if (executable && ((program_header->p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001422 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001423 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001424 if ((program_header->p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001425 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001426 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001427 if ((program_header->p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -07001428 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001429 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001430 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001431 if (writable_) {
1432 prot |= PROT_WRITE;
1433 flags |= MAP_SHARED;
1434 } else {
1435 flags |= MAP_PRIVATE;
1436 }
Andreas Gampedaab38c2014-09-12 18:38:24 -07001437 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -08001438 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Tong Shen62d1ca32014-09-03 17:24:56 -07001439 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1440 static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001441 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -07001442 return false;
1443 }
Ian Rogers700a4022014-05-19 16:49:03 -07001444 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Andreas Gampedaab38c2014-09-12 18:38:24 -07001445 program_header->p_memsz,
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001446 prot, flags, file_->Fd(),
Andreas Gampedaab38c2014-09-12 18:38:24 -07001447 program_header->p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -07001448 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001449 file_->GetPath().c_str(),
1450 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -08001451 if (segment.get() == nullptr) {
1452 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1453 i, file_->GetPath().c_str(), error_msg->c_str());
1454 return false;
1455 }
1456 if (segment->Begin() != p_vaddr) {
1457 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1458 "instead mapped to %p",
1459 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1460 return false;
1461 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001462 segments_.push_back(segment.release());
1463 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001464
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001465 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
Ian Rogers13735952014-10-08 12:43:28 -07001466 uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
Andreas Gampedaab38c2014-09-12 18:38:24 -07001467 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1468 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1469 file_->GetPath().c_str());
1470 return false;
1471 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001472 dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001473
Tong Shen62d1ca32014-09-03 17:24:56 -07001474 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1475 Elf_Dyn& elf_dyn = GetDynamic(i);
Ian Rogers13735952014-10-08 12:43:28 -07001476 uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001477 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001478 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001479 if (!ValidPointer(d_ptr)) {
1480 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1481 d_ptr, file_->GetPath().c_str());
1482 return false;
1483 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001484 hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001485 break;
1486 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001487 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001488 if (!ValidPointer(d_ptr)) {
1489 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1490 d_ptr, file_->GetPath().c_str());
1491 return false;
1492 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001493 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1494 break;
1495 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001496 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001497 if (!ValidPointer(d_ptr)) {
1498 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1499 d_ptr, file_->GetPath().c_str());
1500 return false;
1501 }
Tong Shen62d1ca32014-09-03 17:24:56 -07001502 dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001503 break;
1504 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +00001505 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -08001506 if (GetDynamicNum() != i+1) {
1507 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1508 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1509 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1510 return false;
1511 }
Brian Carlstrom265091e2013-01-30 14:08:26 -08001512 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001513 }
1514 }
1515 }
1516
Andreas Gampedaab38c2014-09-12 18:38:24 -07001517 // Check for the existence of some sections.
1518 if (!CheckSectionsExist(error_msg)) {
1519 return false;
1520 }
1521
Mark Mendellae9fd932014-02-10 16:14:35 -08001522 // Use GDB JIT support to do stack backtrace, etc.
1523 if (executable) {
1524 GdbJITSupport();
1525 }
1526
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001527 return true;
1528}
1529
Tong Shen62d1ca32014-09-03 17:24:56 -07001530template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1531 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1532 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1533bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1534 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
Ian Rogers13735952014-10-08 12:43:28 -07001535 ::ValidPointer(const uint8_t* start) const {
Brian Carlstromc1409452014-02-26 14:06:23 -08001536 for (size_t i = 0; i < segments_.size(); ++i) {
1537 const MemMap* segment = segments_[i];
1538 if (segment->Begin() <= start && start < segment->End()) {
1539 return true;
1540 }
1541 }
1542 return false;
1543}
1544
Alex Light3470ab42014-06-18 10:35:45 -07001545
Tong Shen62d1ca32014-09-03 17:24:56 -07001546template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
1547 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
1548 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
1549Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
1550 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
1551 ::FindSectionByName(const std::string& name) const {
Alex Light3470ab42014-06-18 10:35:45 -07001552 CHECK(!program_header_only_);
Tong Shen62d1ca32014-09-03 17:24:56 -07001553 Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
Andreas Gampedaab38c2014-09-12 18:38:24 -07001554 if (shstrtab_sec == nullptr) {
1555 return nullptr;
1556 }
Alex Light3470ab42014-06-18 10:35:45 -07001557 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
Tong Shen62d1ca32014-09-03 17:24:56 -07001558 Elf_Shdr* shdr = GetSectionHeader(i);
Andreas Gampedaab38c2014-09-12 18:38:24 -07001559 if (shdr == nullptr) {
1560 return nullptr;
1561 }
1562 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
Alex Light3470ab42014-06-18 10:35:45 -07001563 if (sec_name == nullptr) {
1564 continue;
1565 }
1566 if (name == sec_name) {
Andreas Gampedaab38c2014-09-12 18:38:24 -07001567 return shdr;
Alex Light3470ab42014-06-18 10:35:45 -07001568 }
1569 }
1570 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001571}
1572
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001573struct PACKED(1) FDE32 {
Alex Light3470ab42014-06-18 10:35:45 -07001574 uint32_t raw_length_;
1575 uint32_t GetLength() {
1576 return raw_length_ + sizeof(raw_length_);
1577 }
1578 uint32_t CIE_pointer;
1579 uint32_t initial_location;
1580 uint32_t address_range;
1581 uint8_t instructions[0];
1582};
1583
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001584static FDE32* NextFDE(FDE32* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001585 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Alex Light3470ab42014-06-18 10:35:45 -07001586 fde_bytes += frame->GetLength();
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001587 return reinterpret_cast<FDE32*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001588}
1589
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001590static bool IsFDE(FDE32* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001591 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001592}
1593
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001594struct PACKED(1) FDE64 {
1595 uint32_t raw_length_;
1596 uint64_t extended_length_;
1597 uint64_t GetLength() {
1598 return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_);
1599 }
1600 uint64_t CIE_pointer;
1601 uint64_t initial_location;
1602 uint64_t address_range;
1603 uint8_t instructions[0];
1604};
1605
1606static FDE64* NextFDE(FDE64* frame) {
Ian Rogers13735952014-10-08 12:43:28 -07001607 uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame);
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001608 fde_bytes += frame->GetLength();
1609 return reinterpret_cast<FDE64*>(fde_bytes);
1610}
1611
1612static bool IsFDE(FDE64* frame) {
1613 return frame->CIE_pointer != 0;
1614}
1615
1616static bool FixupEHFrame(off_t base_address_delta,
Ian Rogers13735952014-10-08 12:43:28 -07001617 uint8_t* eh_frame, size_t eh_frame_size) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001618 if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) {
1619 FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size);
1620 FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame));
1621 for (; frame < last_frame; frame = NextFDE(frame)) {
1622 if (!IsFDE(frame)) {
1623 return false;
1624 }
1625 frame->initial_location += base_address_delta;
1626 }
1627 return true;
1628 } else {
1629 FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size);
1630 FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame));
1631 for (; frame < last_frame; frame = NextFDE(frame)) {
1632 if (!IsFDE(frame)) {
1633 return false;
1634 }
1635 frame->initial_location += base_address_delta;
1636 }
1637 return true;
1638 }
1639}
1640
1641static uint8_t* NextLeb128(uint8_t* current) {
1642 DecodeUnsignedLeb128(const_cast<const uint8_t**>(&current));
1643 return current;
1644}
1645
1646struct PACKED(1) DebugLineHeader {
1647 uint32_t unit_length_; // TODO 32-bit specific size
1648 uint16_t version_;
1649 uint32_t header_length_; // TODO 32-bit specific size
1650 uint8_t minimum_instruction_lenght_;
1651 uint8_t maximum_operations_per_instruction_;
1652 uint8_t default_is_stmt_;
1653 int8_t line_base_;
1654 uint8_t line_range_;
1655 uint8_t opcode_base_;
1656 uint8_t remaining_[0];
1657
1658 bool IsStandardOpcode(const uint8_t* op) const {
1659 return *op != 0 && *op < opcode_base_;
1660 }
1661
1662 bool IsExtendedOpcode(const uint8_t* op) const {
1663 return *op == 0;
1664 }
1665
1666 const uint8_t* GetStandardOpcodeLengths() const {
1667 return remaining_;
1668 }
1669
1670 uint8_t* GetNextOpcode(uint8_t* op) const {
1671 if (IsExtendedOpcode(op)) {
1672 uint8_t* length_field = op + 1;
1673 uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field));
1674 return length_field + length;
1675 } else if (!IsStandardOpcode(op)) {
1676 return op + 1;
1677 } else if (*op == DW_LNS_fixed_advance_pc) {
1678 return op + 1 + sizeof(uint16_t);
1679 } else {
1680 uint8_t num_args = GetStandardOpcodeLengths()[*op - 1];
1681 op += 1;
1682 for (int i = 0; i < num_args; i++) {
1683 op = NextLeb128(op);
1684 }
1685 return op;
1686 }
1687 }
1688
1689 uint8_t* GetDebugLineData() const {
1690 const uint8_t* hdr_start =
1691 reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_);
1692 return const_cast<uint8_t*>(hdr_start + header_length_);
1693 }
1694};
1695
Ian Rogersd4c4d952014-10-16 20:31:53 -07001696class DebugLineInstructionIterator FINAL {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001697 public:
1698 static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) {
1699 std::unique_ptr<DebugLineInstructionIterator> line_iter(
1700 new DebugLineInstructionIterator(header, section_size));
1701 if (line_iter.get() == nullptr) {
1702 return nullptr;
1703 } else {
1704 return line_iter.release();
1705 }
1706 }
1707
1708 ~DebugLineInstructionIterator() {}
1709
1710 bool Next() {
1711 if (current_instruction_ == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07001712 return false;
1713 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001714 current_instruction_ = header_->GetNextOpcode(current_instruction_);
1715 if (current_instruction_ >= last_instruction_) {
1716 current_instruction_ = nullptr;
1717 return false;
1718 } else {
1719 return true;
1720 }
1721 }
1722
Ian Rogersd4c4d952014-10-16 20:31:53 -07001723 uint8_t* GetInstruction() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001724 return current_instruction_;
1725 }
1726
Ian Rogersd4c4d952014-10-16 20:31:53 -07001727 bool IsExtendedOpcode() const {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001728 return header_->IsExtendedOpcode(current_instruction_);
1729 }
1730
1731 uint8_t GetOpcode() {
1732 if (!IsExtendedOpcode()) {
1733 return *current_instruction_;
1734 } else {
1735 uint8_t* len_ptr = current_instruction_ + 1;
1736 return *NextLeb128(len_ptr);
1737 }
1738 }
1739
1740 uint8_t* GetArguments() {
1741 if (!IsExtendedOpcode()) {
1742 return current_instruction_ + 1;
1743 } else {
1744 uint8_t* len_ptr = current_instruction_ + 1;
1745 return NextLeb128(len_ptr) + 1;
1746 }
1747 }
1748
1749 private:
1750 DebugLineInstructionIterator(DebugLineHeader* header, size_t size)
1751 : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size),
1752 current_instruction_(header->GetDebugLineData()) {}
1753
Ian Rogersd4c4d952014-10-16 20:31:53 -07001754 DebugLineHeader* const header_;
1755 uint8_t* const last_instruction_;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001756 uint8_t* current_instruction_;
1757};
1758
1759static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) {
1760 while (iter->Next()) {
1761 if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) {
1762 *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta;
1763 }
Alex Light3470ab42014-06-18 10:35:45 -07001764 }
1765 return true;
1766}
1767
1768struct PACKED(1) DebugInfoHeader {
1769 uint32_t unit_length; // TODO 32-bit specific size
1770 uint16_t version;
1771 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1772 uint8_t address_size;
1773};
1774
1775// Returns -1 if it is variable length, which we will just disallow for now.
1776static int32_t FormLength(uint32_t att) {
1777 switch (att) {
1778 case DW_FORM_data1:
1779 case DW_FORM_flag:
1780 case DW_FORM_flag_present:
1781 case DW_FORM_ref1:
1782 return 1;
1783
1784 case DW_FORM_data2:
1785 case DW_FORM_ref2:
1786 return 2;
1787
1788 case DW_FORM_addr: // TODO 32-bit only
1789 case DW_FORM_ref_addr: // TODO 32-bit only
1790 case DW_FORM_sec_offset: // TODO 32-bit only
1791 case DW_FORM_strp: // TODO 32-bit only
1792 case DW_FORM_data4:
1793 case DW_FORM_ref4:
1794 return 4;
1795
1796 case DW_FORM_data8:
1797 case DW_FORM_ref8:
1798 case DW_FORM_ref_sig8:
1799 return 8;
1800
1801 case DW_FORM_block:
1802 case DW_FORM_block1:
1803 case DW_FORM_block2:
1804 case DW_FORM_block4:
1805 case DW_FORM_exprloc:
1806 case DW_FORM_indirect:
1807 case DW_FORM_ref_udata:
1808 case DW_FORM_sdata:
1809 case DW_FORM_string:
1810 case DW_FORM_udata:
1811 default:
1812 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001813 }
1814}
1815
Ian Rogersd4c4d952014-10-16 20:31:53 -07001816class DebugTag FINAL {
Alex Light3470ab42014-06-18 10:35:45 -07001817 public:
Alex Light3470ab42014-06-18 10:35:45 -07001818 ~DebugTag() {}
1819 // Creates a new tag and moves data pointer up to the start of the next one.
1820 // nullptr means error.
Ian Rogers13735952014-10-08 12:43:28 -07001821 static DebugTag* Create(const uint8_t** data_pointer) {
1822 const uint8_t* data = *data_pointer;
Alex Light3470ab42014-06-18 10:35:45 -07001823 uint32_t index = DecodeUnsignedLeb128(&data);
1824 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1825 tag->size_ = static_cast<uint32_t>(
1826 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1827 // skip the abbrev
1828 tag->tag_ = DecodeUnsignedLeb128(&data);
1829 tag->has_child_ = (*data == 0);
1830 data++;
1831 while (true) {
1832 uint32_t attr = DecodeUnsignedLeb128(&data);
1833 uint32_t form = DecodeUnsignedLeb128(&data);
1834 if (attr == 0 && form == 0) {
1835 break;
1836 } else if (attr == 0 || form == 0) {
1837 // Bad abbrev.
1838 return nullptr;
1839 }
1840 int32_t size = FormLength(form);
1841 if (size == -1) {
1842 return nullptr;
1843 }
1844 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1845 }
1846 *data_pointer = data;
1847 return tag.release();
1848 }
1849
1850 uint32_t GetSize() const {
1851 return size_;
1852 }
1853
Ian Rogersd4c4d952014-10-16 20:31:53 -07001854 bool HasChild() const {
Alex Light3470ab42014-06-18 10:35:45 -07001855 return has_child_;
1856 }
1857
Ian Rogersd4c4d952014-10-16 20:31:53 -07001858 uint32_t GetTagNumber() const {
Alex Light3470ab42014-06-18 10:35:45 -07001859 return tag_;
1860 }
1861
Ian Rogersd4c4d952014-10-16 20:31:53 -07001862 uint32_t GetIndex() const {
1863 return index_;
1864 }
1865
Alex Light3470ab42014-06-18 10:35:45 -07001866 // Gets the offset of a particular attribute in this tag structure.
1867 // Interpretation of the data is left to the consumer. 0 is returned if the
1868 // tag does not contain the attribute.
1869 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1870 auto it = off_map_.find(dwarf_attribute);
1871 if (it == off_map_.end()) {
1872 return 0;
1873 } else {
1874 return it->second;
1875 }
1876 }
1877
1878 // Gets the size of attribute
1879 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1880 auto it = size_map_.find(dwarf_attribute);
1881 if (it == size_map_.end()) {
1882 return 0;
1883 } else {
1884 return it->second;
1885 }
1886 }
1887
1888 private:
Andreas Gampedaab38c2014-09-12 18:38:24 -07001889 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
Alex Light3470ab42014-06-18 10:35:45 -07001890 void AddAttribute(uint32_t type, uint32_t attr_size) {
1891 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1892 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1893 size_ += attr_size;
1894 }
Ian Rogersd4c4d952014-10-16 20:31:53 -07001895
1896 const uint32_t index_;
Alex Light3470ab42014-06-18 10:35:45 -07001897 std::map<uint32_t, uint32_t> off_map_;
1898 std::map<uint32_t, uint32_t> size_map_;
1899 uint32_t size_;
1900 uint32_t tag_;
1901 bool has_child_;
1902};
1903
1904class DebugAbbrev {
1905 public:
1906 ~DebugAbbrev() {}
Ian Rogers13735952014-10-08 12:43:28 -07001907 static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) {
Alex Lightd338ae02014-08-13 17:15:38 -07001908 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size));
1909 if (!abbrev->ReadAtOffset(0)) {
1910 return nullptr;
Alex Light3470ab42014-06-18 10:35:45 -07001911 }
1912 return abbrev.release();
1913 }
1914
Alex Lightd338ae02014-08-13 17:15:38 -07001915 bool ReadAtOffset(uint32_t abbrev_offset) {
1916 tags_.clear();
1917 tag_list_.clear();
Ian Rogers13735952014-10-08 12:43:28 -07001918 const uint8_t* dbg_abbrev = begin_ + abbrev_offset;
Alex Lightd338ae02014-08-13 17:15:38 -07001919 while (dbg_abbrev < end_ && *dbg_abbrev != 0) {
1920 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1921 if (tag.get() == nullptr) {
1922 return false;
1923 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07001924 tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size()));
Alex Lightd338ae02014-08-13 17:15:38 -07001925 tag_list_.push_back(std::move(tag));
1926 }
1927 }
1928 return true;
1929 }
1930
Ian Rogers13735952014-10-08 12:43:28 -07001931 DebugTag* ReadTag(const uint8_t* entry) {
Alex Light3470ab42014-06-18 10:35:45 -07001932 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1933 auto it = tags_.find(tag_num);
1934 if (it == tags_.end()) {
1935 return nullptr;
1936 } else {
1937 CHECK_GT(tag_list_.size(), it->second);
1938 return tag_list_.at(it->second).get();
1939 }
1940 }
1941
1942 private:
Ian Rogers13735952014-10-08 12:43:28 -07001943 DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07001944 const uint8_t* const begin_;
1945 const uint8_t* const end_;
Alex Light3470ab42014-06-18 10:35:45 -07001946 std::map<uint32_t, uint32_t> tags_;
1947 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1948};
1949
1950class DebugInfoIterator {
1951 public:
1952 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1953 DebugAbbrev* abbrev) {
1954 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1955 if (iter->GetCurrentTag() == nullptr) {
1956 return nullptr;
1957 } else {
1958 return iter.release();
1959 }
1960 }
1961 ~DebugInfoIterator() {}
1962
1963 // Moves to the next DIE. Returns false if at last entry.
1964 // TODO Handle variable length attributes.
1965 bool next() {
1966 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1967 return false;
1968 }
Alex Lightd338ae02014-08-13 17:15:38 -07001969 bool reread_abbrev = false;
Alex Light3470ab42014-06-18 10:35:45 -07001970 current_entry_ += current_tag_->GetSize();
Alex Lightd338ae02014-08-13 17:15:38 -07001971 if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) {
1972 current_cu_ = next_cu_;
1973 next_cu_ = GetNextCu(current_cu_);
Ian Rogers13735952014-10-08 12:43:28 -07001974 current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader);
Alex Lightd338ae02014-08-13 17:15:38 -07001975 reread_abbrev = true;
1976 }
Alex Light3470ab42014-06-18 10:35:45 -07001977 if (current_entry_ >= last_entry_) {
1978 current_entry_ = nullptr;
1979 return false;
1980 }
Alex Lightd338ae02014-08-13 17:15:38 -07001981 if (reread_abbrev) {
1982 abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset);
1983 }
Alex Light3470ab42014-06-18 10:35:45 -07001984 current_tag_ = abbrev_->ReadTag(current_entry_);
1985 if (current_tag_ == nullptr) {
1986 current_entry_ = nullptr;
1987 return false;
1988 } else {
1989 return true;
1990 }
1991 }
1992
1993 const DebugTag* GetCurrentTag() {
1994 return const_cast<DebugTag*>(current_tag_);
1995 }
Ian Rogers13735952014-10-08 12:43:28 -07001996 uint8_t* GetPointerToField(uint8_t dwarf_field) {
Alex Light3470ab42014-06-18 10:35:45 -07001997 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1998 return nullptr;
1999 }
2000 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
2001 if (off == 0) {
2002 // tag does not have that field.
2003 return nullptr;
2004 } else {
2005 DCHECK_LT(off, current_tag_->GetSize());
2006 return current_entry_ + off;
2007 }
2008 }
2009
2010 private:
Alex Lightd338ae02014-08-13 17:15:38 -07002011 static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) {
Ian Rogers13735952014-10-08 12:43:28 -07002012 uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr);
Alex Lightd338ae02014-08-13 17:15:38 -07002013 return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length);
2014 }
2015
Alex Light3470ab42014-06-18 10:35:45 -07002016 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
2017 : abbrev_(abbrev),
Alex Lightd338ae02014-08-13 17:15:38 -07002018 current_cu_(header),
2019 next_cu_(GetNextCu(header)),
Ian Rogers13735952014-10-08 12:43:28 -07002020 last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size),
2021 current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)),
Alex Light3470ab42014-06-18 10:35:45 -07002022 current_tag_(abbrev_->ReadTag(current_entry_)) {}
Ian Rogersd4c4d952014-10-16 20:31:53 -07002023 DebugAbbrev* const abbrev_;
Alex Lightd338ae02014-08-13 17:15:38 -07002024 DebugInfoHeader* current_cu_;
2025 DebugInfoHeader* next_cu_;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002026 uint8_t* const last_entry_;
Ian Rogers13735952014-10-08 12:43:28 -07002027 uint8_t* current_entry_;
Alex Light3470ab42014-06-18 10:35:45 -07002028 DebugTag* current_tag_;
2029};
2030
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002031static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) {
Alex Light3470ab42014-06-18 10:35:45 -07002032 do {
2033 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
2034 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002035 LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet.";
Alex Light3470ab42014-06-18 10:35:45 -07002036 return false;
2037 }
2038 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
2039 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
2040 if (PC_low != nullptr && PC_high != nullptr) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002041 *PC_low += base_address_delta;
2042 *PC_high += base_address_delta;
Alex Light3470ab42014-06-18 10:35:45 -07002043 }
2044 } while (iter->next());
2045 return true;
2046}
2047
Tong Shen62d1ca32014-09-03 17:24:56 -07002048template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2049 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2050 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2051bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2052 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2053 ::FixupDebugSections(off_t base_address_delta) {
2054 const Elf_Shdr* debug_info = FindSectionByName(".debug_info");
2055 const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev");
2056 const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame");
2057 const Elf_Shdr* debug_str = FindSectionByName(".debug_str");
2058 const Elf_Shdr* debug_line = FindSectionByName(".debug_line");
2059 const Elf_Shdr* strtab_sec = FindSectionByName(".strtab");
2060 const Elf_Shdr* symtab_sec = FindSectionByName(".symtab");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002061
2062 if (debug_info == nullptr || debug_abbrev == nullptr ||
2063 debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
2064 // Release version of ART does not generate debug info.
2065 return true;
2066 }
2067 if (base_address_delta == 0) {
2068 return true;
2069 }
2070 if (eh_frame != nullptr &&
2071 !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
2072 return false;
2073 }
2074
2075 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset,
2076 debug_abbrev->sh_size));
Alex Light3470ab42014-06-18 10:35:45 -07002077 if (abbrev.get() == nullptr) {
2078 return false;
2079 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002080 DebugInfoHeader* info_header =
2081 reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset);
2082 std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header,
2083 debug_info->sh_size,
2084 abbrev.get()));
2085 if (info_iter.get() == nullptr) {
Alex Light3470ab42014-06-18 10:35:45 -07002086 return false;
2087 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002088 if (debug_line != nullptr) {
2089 DebugLineHeader* line_header =
2090 reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset);
2091 std::unique_ptr<DebugLineInstructionIterator> line_iter(
2092 DebugLineInstructionIterator::Create(line_header, debug_line->sh_size));
2093 if (line_iter.get() == nullptr) {
2094 return false;
2095 }
2096 if (!FixupDebugLine(base_address_delta, line_iter.get())) {
2097 return false;
2098 }
2099 }
2100 return FixupDebugInfo(base_address_delta, info_iter.get());
Alex Light3470ab42014-06-18 10:35:45 -07002101}
Mark Mendellae9fd932014-02-10 16:14:35 -08002102
Tong Shen62d1ca32014-09-03 17:24:56 -07002103template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2104 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2105 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2106void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2107 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2108 ::GdbJITSupport() {
Mark Mendellae9fd932014-02-10 16:14:35 -08002109 // We only get here if we only are mapping the program header.
2110 DCHECK(program_header_only_);
2111
2112 // Well, we need the whole file to do this.
2113 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07002114 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
2115 // sections are there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002116 std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2117 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>>
2118 all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
2119 MAP_PRIVATE, &error_msg));
Alex Light3470ab42014-06-18 10:35:45 -07002120 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002121 return;
2122 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002123 ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2124 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr;
Alex Light3470ab42014-06-18 10:35:45 -07002125
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002126 // We need the eh_frame for gdb but debug info might be present without it.
Tong Shen62d1ca32014-09-03 17:24:56 -07002127 const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002128 if (eh_frame == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08002129 return;
2130 }
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002131
2132 // Do we have interesting sections?
Alex Light3470ab42014-06-18 10:35:45 -07002133 // We need to add in a strtab and symtab to the image.
2134 // all is MAP_PRIVATE so it can be written to freely.
2135 // We also already have strtab and symtab so we are fine there.
Tong Shen62d1ca32014-09-03 17:24:56 -07002136 Elf_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08002137 elf_hdr.e_entry = 0;
2138 elf_hdr.e_phoff = 0;
2139 elf_hdr.e_phnum = 0;
2140 elf_hdr.e_phentsize = 0;
2141 elf_hdr.e_type = ET_EXEC;
2142
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07002143 // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat)
2144 // and the actual address stuff starts at in regular files this is good.
2145 if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) {
Alex Light3470ab42014-06-18 10:35:45 -07002146 LOG(ERROR) << "Failed to load GDB data";
2147 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08002148 }
2149
Alex Light3470ab42014-06-18 10:35:45 -07002150 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
2151 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08002152}
2153
Tong Shen62d1ca32014-09-03 17:24:56 -07002154template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2155 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2156 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2157bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2158 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2159 ::Strip(std::string* error_msg) {
2160 // ELF files produced by MCLinker look roughly like this
2161 //
2162 // +------------+
2163 // | Elf_Ehdr | contains number of Elf_Shdr and offset to first
2164 // +------------+
2165 // | Elf_Phdr | program headers
2166 // | Elf_Phdr |
2167 // | ... |
2168 // | Elf_Phdr |
2169 // +------------+
2170 // | section | mixture of needed and unneeded sections
2171 // +------------+
2172 // | section |
2173 // +------------+
2174 // | ... |
2175 // +------------+
2176 // | section |
2177 // +------------+
2178 // | Elf_Shdr | section headers
2179 // | Elf_Shdr |
2180 // | ... | contains offset to section start
2181 // | Elf_Shdr |
2182 // +------------+
2183 //
2184 // To strip:
2185 // - leave the Elf_Ehdr and Elf_Phdr values in place.
2186 // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
2187 // - move the sections are keeping up to fill in gaps of sections we want to strip
2188 // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
2189 // - truncate rest of file
2190 //
2191
2192 std::vector<Elf_Shdr> section_headers;
2193 std::vector<Elf_Word> section_headers_original_indexes;
2194 section_headers.reserve(GetSectionHeaderNum());
2195
2196
2197 Elf_Shdr* string_section = GetSectionNameStringSection();
2198 CHECK(string_section != nullptr);
2199 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2200 Elf_Shdr* sh = GetSectionHeader(i);
2201 CHECK(sh != nullptr);
2202 const char* name = GetString(*string_section, sh->sh_name);
2203 if (name == nullptr) {
2204 CHECK_EQ(0U, i);
2205 section_headers.push_back(*sh);
2206 section_headers_original_indexes.push_back(0);
2207 continue;
2208 }
2209 if (StartsWith(name, ".debug")
2210 || (strcmp(name, ".strtab") == 0)
2211 || (strcmp(name, ".symtab") == 0)) {
2212 continue;
2213 }
2214 section_headers.push_back(*sh);
2215 section_headers_original_indexes.push_back(i);
2216 }
2217 CHECK_NE(0U, section_headers.size());
2218 CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
2219
2220 // section 0 is the NULL section, sections start at offset of first section
2221 CHECK(GetSectionHeader(1) != nullptr);
2222 Elf_Off offset = GetSectionHeader(1)->sh_offset;
2223 for (size_t i = 1; i < section_headers.size(); i++) {
2224 Elf_Shdr& new_sh = section_headers[i];
2225 Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
2226 CHECK(old_sh != nullptr);
2227 CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
2228 if (old_sh->sh_addralign > 1) {
2229 offset = RoundUp(offset, old_sh->sh_addralign);
2230 }
2231 if (old_sh->sh_offset == offset) {
2232 // already in place
2233 offset += old_sh->sh_size;
2234 continue;
2235 }
2236 // shift section earlier
2237 memmove(Begin() + offset,
2238 Begin() + old_sh->sh_offset,
2239 old_sh->sh_size);
2240 new_sh.sh_offset = offset;
2241 offset += old_sh->sh_size;
2242 }
2243
2244 Elf_Off shoff = offset;
2245 size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
2246 memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
2247 offset += section_headers_size_in_bytes;
2248
2249 GetHeader().e_shnum = section_headers.size();
2250 GetHeader().e_shoff = shoff;
2251 int result = ftruncate(file_->Fd(), offset);
2252 if (result != 0) {
2253 *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
2254 file_->GetPath().c_str(), strerror(errno));
2255 return false;
2256 }
2257 return true;
2258}
2259
2260static const bool DEBUG_FIXUP = false;
2261
2262template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2263 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2264 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2265bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2266 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2267 ::Fixup(uintptr_t base_address) {
2268 if (!FixupDynamic(base_address)) {
2269 LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath();
2270 return false;
2271 }
2272 if (!FixupSectionHeaders(base_address)) {
2273 LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath();
2274 return false;
2275 }
2276 if (!FixupProgramHeaders(base_address)) {
2277 LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath();
2278 return false;
2279 }
2280 if (!FixupSymbols(base_address, true)) {
2281 LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath();
2282 return false;
2283 }
2284 if (!FixupSymbols(base_address, false)) {
2285 LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath();
2286 return false;
2287 }
2288 if (!FixupRelocations(base_address)) {
2289 LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath();
2290 return false;
2291 }
2292 if (!FixupDebugSections(base_address)) {
2293 LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath();
2294 return false;
2295 }
2296 return true;
2297}
2298
2299template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2300 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2301 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2302bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2303 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2304 ::FixupDynamic(uintptr_t base_address) {
2305 for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
2306 Elf_Dyn& elf_dyn = GetDynamic(i);
2307 Elf_Word d_tag = elf_dyn.d_tag;
2308 if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
2309 Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
2310 if (DEBUG_FIXUP) {
2311 LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2312 GetFile().GetPath().c_str(), i,
2313 static_cast<uint64_t>(d_ptr),
2314 static_cast<uint64_t>(d_ptr + base_address));
2315 }
2316 d_ptr += base_address;
2317 elf_dyn.d_un.d_ptr = d_ptr;
2318 }
2319 }
2320 return true;
2321}
2322
2323template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2324 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2325 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2326bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2327 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2328 ::FixupSectionHeaders(uintptr_t base_address) {
2329 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2330 Elf_Shdr* sh = GetSectionHeader(i);
2331 CHECK(sh != nullptr);
2332 // 0 implies that the section will not exist in the memory of the process
2333 if (sh->sh_addr == 0) {
2334 continue;
2335 }
2336 if (DEBUG_FIXUP) {
2337 LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2338 GetFile().GetPath().c_str(), i,
2339 static_cast<uint64_t>(sh->sh_addr),
2340 static_cast<uint64_t>(sh->sh_addr + base_address));
2341 }
2342 sh->sh_addr += base_address;
2343 }
2344 return true;
2345}
2346
2347template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2348 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2349 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2350bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2351 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2352 ::FixupProgramHeaders(uintptr_t base_address) {
2353 // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
2354 for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
2355 Elf_Phdr* ph = GetProgramHeader(i);
2356 CHECK(ph != nullptr);
2357 CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i;
2358 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2359 << GetFile().GetPath() << " i=" << i;
2360 if (DEBUG_FIXUP) {
2361 LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2362 GetFile().GetPath().c_str(), i,
2363 static_cast<uint64_t>(ph->p_vaddr),
2364 static_cast<uint64_t>(ph->p_vaddr + base_address));
2365 }
2366 ph->p_vaddr += base_address;
2367 ph->p_paddr += base_address;
2368 CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
2369 << GetFile().GetPath() << " i=" << i;
2370 }
2371 return true;
2372}
2373
2374template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2375 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2376 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2377bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2378 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2379 ::FixupSymbols(uintptr_t base_address, bool dynamic) {
2380 Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
2381 // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
2382 Elf_Shdr* symbol_section = FindSectionByType(section_type);
2383 if (symbol_section == nullptr) {
2384 // file is missing optional .symtab
2385 CHECK(!dynamic) << GetFile().GetPath();
2386 return true;
2387 }
2388 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
2389 Elf_Sym* symbol = GetSymbol(section_type, i);
2390 CHECK(symbol != nullptr);
2391 if (symbol->st_value != 0) {
2392 if (DEBUG_FIXUP) {
2393 LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
2394 GetFile().GetPath().c_str(), i,
2395 static_cast<uint64_t>(symbol->st_value),
2396 static_cast<uint64_t>(symbol->st_value + base_address));
2397 }
2398 symbol->st_value += base_address;
2399 }
2400 }
2401 return true;
2402}
2403
2404template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word,
2405 typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel,
2406 typename Elf_Rela, typename Elf_Dyn, typename Elf_Off>
2407bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word,
2408 Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>
2409 ::FixupRelocations(uintptr_t base_address) {
2410 for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
2411 Elf_Shdr* sh = GetSectionHeader(i);
2412 CHECK(sh != nullptr);
2413 if (sh->sh_type == SHT_REL) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002414 for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
2415 Elf_Rel& rel = GetRel(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002416 if (DEBUG_FIXUP) {
2417 LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002418 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002419 static_cast<uint64_t>(rel.r_offset),
2420 static_cast<uint64_t>(rel.r_offset + base_address));
2421 }
2422 rel.r_offset += base_address;
2423 }
2424 } else if (sh->sh_type == SHT_RELA) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002425 for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
2426 Elf_Rela& rela = GetRela(*sh, j);
Tong Shen62d1ca32014-09-03 17:24:56 -07002427 if (DEBUG_FIXUP) {
2428 LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002429 GetFile().GetPath().c_str(), j,
Tong Shen62d1ca32014-09-03 17:24:56 -07002430 static_cast<uint64_t>(rela.r_offset),
2431 static_cast<uint64_t>(rela.r_offset + base_address));
2432 }
2433 rela.r_offset += base_address;
2434 }
2435 }
2436 }
2437 return true;
2438}
2439
2440// Explicit instantiations
2441template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word,
2442 Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>;
2443template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word,
2444 Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>;
2445
Ian Rogersd4c4d952014-10-16 20:31:53 -07002446ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002447}
2448
Ian Rogersd4c4d952014-10-16 20:31:53 -07002449ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002450}
2451
2452ElfFile::~ElfFile() {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002453 // Should never have 32 and 64-bit impls.
2454 CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002455}
2456
Igor Murashkin46774762014-10-22 11:37:02 -07002457ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg,
2458 uint8_t* requested_base) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002459 if (file->GetLength() < EI_NIDENT) {
2460 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2461 file->GetPath().c_str());
2462 return nullptr;
2463 }
2464 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2465 file->GetPath().c_str(), error_msg));
2466 if (map == nullptr && map->Size() != EI_NIDENT) {
2467 return nullptr;
2468 }
Ian Rogers13735952014-10-08 12:43:28 -07002469 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002470 if (header[EI_CLASS] == ELFCLASS64) {
Igor Murashkin46774762014-10-22 11:37:02 -07002471 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only,
2472 error_msg, requested_base);
Tong Shen62d1ca32014-09-03 17:24:56 -07002473 if (elf_file_impl == nullptr)
2474 return nullptr;
2475 return new ElfFile(elf_file_impl);
2476 } else if (header[EI_CLASS] == ELFCLASS32) {
Igor Murashkin46774762014-10-22 11:37:02 -07002477 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only,
2478 error_msg, requested_base);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002479 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002480 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002481 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002482 return new ElfFile(elf_file_impl);
2483 } else {
2484 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2485 ELFCLASS32, ELFCLASS64,
2486 file->GetPath().c_str(),
2487 header[EI_CLASS]);
2488 return nullptr;
2489 }
2490}
2491
2492ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
2493 if (file->GetLength() < EI_NIDENT) {
2494 *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
2495 file->GetPath().c_str());
2496 return nullptr;
2497 }
2498 std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0,
2499 file->GetPath().c_str(), error_msg));
2500 if (map == nullptr && map->Size() != EI_NIDENT) {
2501 return nullptr;
2502 }
Ian Rogers13735952014-10-08 12:43:28 -07002503 uint8_t* header = map->Begin();
Tong Shen62d1ca32014-09-03 17:24:56 -07002504 if (header[EI_CLASS] == ELFCLASS64) {
2505 ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002506 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002507 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002508 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002509 return new ElfFile(elf_file_impl);
2510 } else if (header[EI_CLASS] == ELFCLASS32) {
2511 ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg);
Ian Rogersd4c4d952014-10-16 20:31:53 -07002512 if (elf_file_impl == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002513 return nullptr;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002514 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002515 return new ElfFile(elf_file_impl);
2516 } else {
2517 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
2518 ELFCLASS32, ELFCLASS64,
2519 file->GetPath().c_str(),
2520 header[EI_CLASS]);
2521 return nullptr;
2522 }
2523}
2524
2525#define DELEGATE_TO_IMPL(func, ...) \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002526 if (elf64_.get() != nullptr) { \
2527 return elf64_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002528 } else { \
Ian Rogersd4c4d952014-10-16 20:31:53 -07002529 DCHECK(elf32_.get() != nullptr); \
2530 return elf32_->func(__VA_ARGS__); \
Tong Shen62d1ca32014-09-03 17:24:56 -07002531 }
2532
2533bool ElfFile::Load(bool executable, std::string* error_msg) {
2534 DELEGATE_TO_IMPL(Load, executable, error_msg);
2535}
2536
Ian Rogers13735952014-10-08 12:43:28 -07002537const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002538 DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
2539}
2540
2541size_t ElfFile::Size() const {
2542 DELEGATE_TO_IMPL(Size);
2543}
2544
Ian Rogers13735952014-10-08 12:43:28 -07002545uint8_t* ElfFile::Begin() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002546 DELEGATE_TO_IMPL(Begin);
2547}
2548
Ian Rogers13735952014-10-08 12:43:28 -07002549uint8_t* ElfFile::End() const {
Tong Shen62d1ca32014-09-03 17:24:56 -07002550 DELEGATE_TO_IMPL(End);
2551}
2552
2553const File& ElfFile::GetFile() const {
2554 DELEGATE_TO_IMPL(GetFile);
2555}
2556
2557bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002558 if (elf32_.get() == nullptr) {
2559 CHECK(elf64_.get() != nullptr);
Tong Shen62d1ca32014-09-03 17:24:56 -07002560
Ian Rogersd4c4d952014-10-16 20:31:53 -07002561 Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
2562 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002563 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002564 }
2565 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002566 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002567 }
2568 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002569 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002570 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002571 return true;
2572 } else {
Ian Rogersd4c4d952014-10-16 20:31:53 -07002573 Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
2574 if (shdr == nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002575 return false;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002576 }
2577 if (offset != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002578 *offset = shdr->sh_offset;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002579 }
2580 if (size != nullptr) {
Tong Shen62d1ca32014-09-03 17:24:56 -07002581 *size = shdr->sh_size;
Ian Rogersd4c4d952014-10-16 20:31:53 -07002582 }
Tong Shen62d1ca32014-09-03 17:24:56 -07002583 return true;
2584 }
2585}
2586
2587uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
2588 const std::string& symbol_name,
2589 bool build_map) {
2590 DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
2591}
2592
2593size_t ElfFile::GetLoadedSize() const {
2594 DELEGATE_TO_IMPL(GetLoadedSize);
2595}
2596
2597bool ElfFile::Strip(File* file, std::string* error_msg) {
2598 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
2599 if (elf_file.get() == nullptr) {
2600 return false;
2601 }
2602
Ian Rogersd4c4d952014-10-16 20:31:53 -07002603 if (elf_file->elf64_.get() != nullptr)
2604 return elf_file->elf64_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002605 else
Ian Rogersd4c4d952014-10-16 20:31:53 -07002606 return elf_file->elf32_->Strip(error_msg);
Tong Shen62d1ca32014-09-03 17:24:56 -07002607}
2608
2609bool ElfFile::Fixup(uintptr_t base_address) {
2610 DELEGATE_TO_IMPL(Fixup, base_address);
2611}
2612
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002613} // namespace art