blob: fac6f53e74c0e2e923c4bf7c0ac0933af6bca691 [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
19#include "base/logging.h"
20#include "base/stl_util.h"
21#include "utils.h"
22
23namespace art {
24
Mark Mendellae9fd932014-02-10 16:14:35 -080025// -------------------------------------------------------------------
26// Binary GDB JIT Interface as described in
27// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
28extern "C" {
29 typedef enum {
30 JIT_NOACTION = 0,
31 JIT_REGISTER_FN,
32 JIT_UNREGISTER_FN
33 } JITAction;
34
35 struct JITCodeEntry {
36 JITCodeEntry* next_;
37 JITCodeEntry* prev_;
38 const byte *symfile_addr_;
39 uint64_t symfile_size_;
40 };
41
42 struct JITDescriptor {
43 uint32_t version_;
44 uint32_t action_flag_;
45 JITCodeEntry* relevant_entry_;
46 JITCodeEntry* first_entry_;
47 };
48
49 // GDB will place breakpoint into this function.
50 // To prevent GCC from inlining or removing it we place noinline attribute
51 // and inline assembler statement inside.
52 void __attribute__((noinline)) __jit_debug_register_code() {
53 __asm__("");
54 }
55
56 // GDB will inspect contents of this descriptor.
57 // Static initialization is necessary to prevent GDB from seeing
58 // uninitialized descriptor.
59 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
60}
61
62
63static JITCodeEntry* CreateCodeEntry(const byte *symfile_addr,
64 uintptr_t symfile_size) {
65 JITCodeEntry* entry = new JITCodeEntry;
66 entry->symfile_addr_ = symfile_addr;
67 entry->symfile_size_ = symfile_size;
68 entry->prev_ = nullptr;
69
70 // TODO: Do we need a lock here?
71 entry->next_ = __jit_debug_descriptor.first_entry_;
72 if (entry->next_ != nullptr) {
73 entry->next_->prev_ = entry;
74 }
75 __jit_debug_descriptor.first_entry_ = entry;
76 __jit_debug_descriptor.relevant_entry_ = entry;
77
78 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
79 __jit_debug_register_code();
80 return entry;
81}
82
83
84static void UnregisterCodeEntry(JITCodeEntry* entry) {
85 // TODO: Do we need a lock here?
86 if (entry->prev_ != nullptr) {
87 entry->prev_->next_ = entry->next_;
88 } else {
89 __jit_debug_descriptor.first_entry_ = entry->next_;
90 }
91
92 if (entry->next_ != nullptr) {
93 entry->next_->prev_ = entry->prev_;
94 }
95
96 __jit_debug_descriptor.relevant_entry_ = entry;
97 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
98 __jit_debug_register_code();
99 delete entry;
100}
101
Brian Carlstromc1409452014-02-26 14:06:23 -0800102ElfFile::ElfFile(File* file, bool writable, bool program_header_only)
103 : file_(file),
104 writable_(writable),
105 program_header_only_(program_header_only),
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700106 header_(NULL),
107 base_address_(NULL),
108 program_headers_start_(NULL),
109 section_headers_start_(NULL),
110 dynamic_program_header_(NULL),
111 dynamic_section_start_(NULL),
112 symtab_section_start_(NULL),
113 dynsym_section_start_(NULL),
114 strtab_section_start_(NULL),
115 dynstr_section_start_(NULL),
116 hash_section_start_(NULL),
117 symtab_symbol_table_(NULL),
Mark Mendellae9fd932014-02-10 16:14:35 -0800118 dynsym_symbol_table_(NULL),
119 jit_elf_image_(NULL),
120 jit_gdb_entry_(NULL) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800121 CHECK(file != NULL);
122}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700124ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
125 std::string* error_msg) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800126 UniquePtr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only));
127 if (!elf_file->Setup(error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700128 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129 }
130 return elf_file.release();
131}
132
Brian Carlstromc1409452014-02-26 14:06:23 -0800133bool ElfFile::Setup(std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800134 int prot;
135 int flags;
136 if (writable_) {
137 prot = PROT_READ | PROT_WRITE;
138 flags = MAP_SHARED;
139 } else {
140 prot = PROT_READ;
141 flags = MAP_PRIVATE;
142 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800143 int64_t temp_file_length = file_->GetLength();
144 if (temp_file_length < 0) {
145 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700146 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
147 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800148 return false;
149 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800150 size_t file_length = static_cast<size_t>(temp_file_length);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000151 if (file_length < sizeof(Elf32_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800152 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000153 "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700154 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800155 return false;
156 }
157
Brian Carlstromc1409452014-02-26 14:06:23 -0800158 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 // first just map ELF header to get program header size information
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000160 size_t elf_header_size = sizeof(Elf32_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800162 file_->GetPath().c_str(), error_msg),
163 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164 return false;
165 }
166 // then remap to cover program header
167 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700168 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800169 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700170 "header of %zd bytes: '%s'", file_length,
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000171 sizeof(Elf32_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700172 return false;
173 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700174 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800175 file_->GetPath().c_str(), error_msg),
176 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700177 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800178 return false;
179 }
180 } else {
181 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800183 file_->GetPath().c_str(), error_msg),
184 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700185 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800186 return false;
187 }
188 }
189
190 // Either way, the program header is relative to the elf header
191 program_headers_start_ = Begin() + GetHeader().e_phoff;
192
Brian Carlstromc1409452014-02-26 14:06:23 -0800193 if (!program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800194 // Setup section headers.
195 section_headers_start_ = Begin() + GetHeader().e_shoff;
196
197 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000198 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800199 if (dynamic_program_header_ == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
201 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800202 return false;
203 }
204
205 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000206 = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800207
208 // Find other sections from section headers
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000209 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
210 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800211 byte* section_addr = Begin() + section_header.sh_offset;
212 switch (section_header.sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000213 case SHT_SYMTAB: {
214 symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 break;
216 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000217 case SHT_DYNSYM: {
218 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800219 break;
220 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000221 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800222 // TODO: base these off of sh_link from .symtab and .dynsym above
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000223 if ((section_header.sh_flags & SHF_ALLOC) != 0) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800224 dynstr_section_start_ = reinterpret_cast<char*>(section_addr);
225 } else {
226 strtab_section_start_ = reinterpret_cast<char*>(section_addr);
227 }
228 break;
229 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000230 case SHT_DYNAMIC: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800231 if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) {
232 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800233 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800234 << reinterpret_cast<void*>(dynamic_section_start_)
235 << " != " << reinterpret_cast<void*>(section_addr);
236 return false;
237 }
238 break;
239 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000240 case SHT_HASH: {
241 hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800242 break;
243 }
244 }
245 }
246 }
247 return true;
248}
249
250ElfFile::~ElfFile() {
251 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800252 delete symtab_symbol_table_;
253 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800254 delete jit_elf_image_;
255 if (jit_gdb_entry_) {
256 UnregisterCodeEntry(jit_gdb_entry_);
257 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800258}
259
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800260bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800261 if (map == NULL) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800262 // MemMap::Open should have already set an error.
263 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800264 return false;
265 }
266 map_.reset(map);
267 CHECK(map_.get() != NULL) << file_->GetPath();
268 CHECK(map_->Begin() != NULL) << file_->GetPath();
269
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000270 header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin());
271 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
272 || (ELFMAG1 != header_->e_ident[EI_MAG1])
273 || (ELFMAG2 != header_->e_ident[EI_MAG2])
274 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800275 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
276 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800277 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000278 header_->e_ident[EI_MAG0],
279 header_->e_ident[EI_MAG1],
280 header_->e_ident[EI_MAG2],
281 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800282 return false;
283 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800284 if (ELFCLASS32 != header_->e_ident[EI_CLASS]) {
285 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
286 ELFCLASS32,
287 file_->GetPath().c_str(),
288 header_->e_ident[EI_CLASS]);
289 return false;
290 }
291 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
292 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
293 ELFDATA2LSB,
294 file_->GetPath().c_str(),
295 header_->e_ident[EI_CLASS]);
296 return false;
297 }
298 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
299 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
300 EV_CURRENT,
301 file_->GetPath().c_str(),
302 header_->e_ident[EI_CLASS]);
303 return false;
304 }
305 if (ET_DYN != header_->e_type) {
306 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
307 ET_DYN,
308 file_->GetPath().c_str(),
309 header_->e_type);
310 return false;
311 }
312 if (EV_CURRENT != header_->e_version) {
313 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
314 EV_CURRENT,
315 file_->GetPath().c_str(),
316 header_->e_version);
317 return false;
318 }
319 if (0 != header_->e_entry) {
320 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
321 0,
322 file_->GetPath().c_str(),
323 header_->e_entry);
324 return false;
325 }
326 if (0 == header_->e_phoff) {
327 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
328 file_->GetPath().c_str());
329 return false;
330 }
331 if (0 == header_->e_shoff) {
332 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
333 file_->GetPath().c_str());
334 return false;
335 }
336 if (0 == header_->e_ehsize) {
337 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
338 file_->GetPath().c_str());
339 return false;
340 }
341 if (0 == header_->e_phentsize) {
342 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
343 file_->GetPath().c_str());
344 return false;
345 }
346 if (0 == header_->e_phnum) {
347 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
348 file_->GetPath().c_str());
349 return false;
350 }
351 if (0 == header_->e_shentsize) {
352 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
353 file_->GetPath().c_str());
354 return false;
355 }
356 if (0 == header_->e_shnum) {
357 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
358 file_->GetPath().c_str());
359 return false;
360 }
361 if (0 == header_->e_shstrndx) {
362 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
363 file_->GetPath().c_str());
364 return false;
365 }
366 if (header_->e_shstrndx >= header_->e_shnum) {
367 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
368 header_->e_shstrndx,
369 header_->e_shnum,
370 file_->GetPath().c_str());
371 return false;
372 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800373
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800374 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800375 if (header_->e_phoff >= Size()) {
Dmitry Petrochenko659d87d2014-02-27 14:23:11 +0700376 *error_msg = StringPrintf("Failed to find e_phoff value %d less than %zd in %s",
Brian Carlstromc1409452014-02-26 14:06:23 -0800377 header_->e_phoff,
378 Size(),
379 file_->GetPath().c_str());
380 return false;
381 }
382 if (header_->e_shoff >= Size()) {
Dmitry Petrochenko659d87d2014-02-27 14:23:11 +0700383 *error_msg = StringPrintf("Failed to find e_shoff value %d less than %zd in %s",
Brian Carlstromc1409452014-02-26 14:06:23 -0800384 header_->e_shoff,
385 Size(),
386 file_->GetPath().c_str());
387 return false;
388 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800389 }
390 return true;
391}
392
393
Brian Carlstromc1409452014-02-26 14:06:23 -0800394Elf32_Ehdr& ElfFile::GetHeader() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800395 CHECK(header_ != NULL);
396 return *header_;
397}
398
Brian Carlstromc1409452014-02-26 14:06:23 -0800399byte* ElfFile::GetProgramHeadersStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800400 CHECK(program_headers_start_ != NULL);
401 return program_headers_start_;
402}
403
Brian Carlstromc1409452014-02-26 14:06:23 -0800404byte* ElfFile::GetSectionHeadersStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800405 CHECK(section_headers_start_ != NULL);
406 return section_headers_start_;
407}
408
Brian Carlstromc1409452014-02-26 14:06:23 -0800409Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800410 CHECK(dynamic_program_header_ != NULL);
411 return *dynamic_program_header_;
412}
413
Brian Carlstromc1409452014-02-26 14:06:23 -0800414Elf32_Dyn* ElfFile::GetDynamicSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800415 CHECK(dynamic_section_start_ != NULL);
416 return dynamic_section_start_;
417}
418
Brian Carlstromc1409452014-02-26 14:06:23 -0800419Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800420 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000421 Elf32_Sym* symbol_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800422 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000423 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800424 symbol_section_start = symtab_section_start_;
425 break;
426 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000427 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800428 symbol_section_start = dynsym_section_start_;
429 break;
430 }
431 default: {
432 LOG(FATAL) << section_type;
433 symbol_section_start = NULL;
434 }
435 }
436 CHECK(symbol_section_start != NULL);
437 return symbol_section_start;
438}
439
Brian Carlstromc1409452014-02-26 14:06:23 -0800440const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800441 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800442 const char* string_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800443 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000444 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800445 string_section_start = strtab_section_start_;
446 break;
447 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000448 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 string_section_start = dynstr_section_start_;
450 break;
451 }
452 default: {
453 LOG(FATAL) << section_type;
454 string_section_start = NULL;
455 }
456 }
457 CHECK(string_section_start != NULL);
458 return string_section_start;
459}
460
Brian Carlstromc1409452014-02-26 14:06:23 -0800461const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800462 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
463 if (i == 0) {
464 return NULL;
465 }
466 const char* string_section_start = GetStringSectionStart(section_type);
467 const char* string = string_section_start + i;
468 return string;
469}
470
Brian Carlstromc1409452014-02-26 14:06:23 -0800471Elf32_Word* ElfFile::GetHashSectionStart() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800472 CHECK(hash_section_start_ != NULL);
473 return hash_section_start_;
474}
475
Brian Carlstromc1409452014-02-26 14:06:23 -0800476Elf32_Word ElfFile::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800477 return GetHashSectionStart()[0];
478}
479
Brian Carlstromc1409452014-02-26 14:06:23 -0800480Elf32_Word ElfFile::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800481 return GetHashSectionStart()[1];
482}
483
Brian Carlstromc1409452014-02-26 14:06:23 -0800484Elf32_Word ElfFile::GetHashBucket(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800485 CHECK_LT(i, GetHashBucketNum());
486 // 0 is nbucket, 1 is nchain
487 return GetHashSectionStart()[2 + i];
488}
489
Brian Carlstromc1409452014-02-26 14:06:23 -0800490Elf32_Word ElfFile::GetHashChain(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800491 CHECK_LT(i, GetHashChainNum());
492 // 0 is nbucket, 1 is nchain, & chains are after buckets
493 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
494}
495
Brian Carlstromc1409452014-02-26 14:06:23 -0800496Elf32_Word ElfFile::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800497 return GetHeader().e_phnum;
498}
499
Brian Carlstromc1409452014-02-26 14:06:23 -0800500Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800501 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath();
502 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
503 CHECK_LT(program_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000504 return *reinterpret_cast<Elf32_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800505}
506
Brian Carlstromc1409452014-02-26 14:06:23 -0800507Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000508 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
509 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800510 if (program_header.p_type == type) {
511 return &program_header;
512 }
513 }
514 return NULL;
515}
516
Brian Carlstromc1409452014-02-26 14:06:23 -0800517Elf32_Word ElfFile::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800518 return GetHeader().e_shnum;
519}
520
Brian Carlstromc1409452014-02-26 14:06:23 -0800521Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800522 // Can only access arbitrary sections when we have the whole file, not just program header.
523 // Even if we Load(), it doesn't bring in all the sections.
524 CHECK(!program_header_only_) << file_->GetPath();
525 CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath();
526 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
527 CHECK_LT(section_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000528 return *reinterpret_cast<Elf32_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800529}
530
Brian Carlstromc1409452014-02-26 14:06:23 -0800531Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800532 // Can only access arbitrary sections when we have the whole file, not just program header.
533 // We could change this to switch on known types if they were detected during loading.
534 CHECK(!program_header_only_) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000535 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
536 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800537 if (section_header.sh_type == type) {
538 return &section_header;
539 }
540 }
541 return NULL;
542}
543
544// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800545static unsigned elfhash(const char *_name) {
546 const unsigned char *name = (const unsigned char *) _name;
547 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800548
Brian Carlstromdf629502013-07-17 22:39:56 -0700549 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800550 h = (h << 4) + *name++;
551 g = h & 0xf0000000;
552 h ^= g;
553 h ^= g >> 24;
554 }
555 return h;
556}
557
Brian Carlstromc1409452014-02-26 14:06:23 -0800558Elf32_Shdr& ElfFile::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800559 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800560}
561
Brian Carlstromc1409452014-02-26 14:06:23 -0800562const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000563 Elf32_Word hash = elfhash(symbol_name.c_str());
564 Elf32_Word bucket_index = hash % GetHashBucketNum();
565 Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800566 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000567 Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
568 const char* name = GetString(SHT_DYNSYM, symbol.st_name);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800569 if (symbol_name == name) {
570 return base_address_ + symbol.st_value;
571 }
572 symbol_and_chain_index = GetHashChain(symbol_and_chain_index);
573 }
574 return NULL;
575}
576
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000577bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) {
578 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800579}
580
Brian Carlstromc1409452014-02-26 14:06:23 -0800581Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const {
582 CHECK(IsSymbolSectionType(section_header.sh_type))
583 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800584 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
585 return section_header.sh_size / section_header.sh_entsize;
586}
587
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000588Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800589 Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800590 return *(GetSymbolSectionStart(section_type) + i);
591}
592
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000593ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800594 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
595 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000596 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800597 return &symtab_symbol_table_;
598 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000599 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800600 return &dynsym_symbol_table_;
601 }
602 default: {
603 LOG(FATAL) << section_type;
604 return NULL;
605 }
606 }
607}
608
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000609Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type,
610 const std::string& symbol_name,
611 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800612 CHECK(!program_header_only_) << file_->GetPath();
613 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800614
615 SymbolTable** symbol_table = GetSymbolTable(section_type);
616 if (*symbol_table != NULL || build_map) {
617 if (*symbol_table == NULL) {
618 DCHECK(build_map);
619 *symbol_table = new SymbolTable;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000620 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800621 CHECK(symbol_section != NULL) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000622 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800623 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000624 Elf32_Sym& symbol = GetSymbol(section_type, i);
625 unsigned char type = ELF32_ST_TYPE(symbol.st_info);
626 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800627 continue;
628 }
629 const char* name = GetString(string_section, symbol.st_name);
630 if (name == NULL) {
631 continue;
632 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800633 std::pair<SymbolTable::iterator, bool> result =
634 (*symbol_table)->insert(std::make_pair(name, &symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800635 if (!result.second) {
636 // If a duplicate, make sure it has the same logical value. Seen on x86.
637 CHECK_EQ(symbol.st_value, result.first->second->st_value);
638 CHECK_EQ(symbol.st_size, result.first->second->st_size);
639 CHECK_EQ(symbol.st_info, result.first->second->st_info);
640 CHECK_EQ(symbol.st_other, result.first->second->st_other);
641 CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx);
642 }
643 }
644 }
645 CHECK(*symbol_table != NULL);
646 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
647 if (it == (*symbol_table)->end()) {
648 return NULL;
649 }
650 return it->second;
651 }
652
653 // Fall back to linear search
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000654 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800655 CHECK(symbol_section != NULL) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000656 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800657 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000658 Elf32_Sym& symbol = GetSymbol(section_type, i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800659 const char* name = GetString(string_section, symbol.st_name);
660 if (name == NULL) {
661 continue;
662 }
663 if (symbol_name == name) {
664 return &symbol;
665 }
666 }
667 return NULL;
668}
669
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000670Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800671 const std::string& symbol_name,
672 bool build_map) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000673 Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800674 if (symbol == NULL) {
675 return 0;
676 }
677 return symbol->st_value;
678}
679
Brian Carlstromc1409452014-02-26 14:06:23 -0800680const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800681 CHECK(!program_header_only_) << file_->GetPath();
682 // TODO: remove this static_cast from enum when using -std=gnu++0x
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000683 CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800684 CHECK_LT(i, string_section.sh_size) << file_->GetPath();
685 if (i == 0) {
686 return NULL;
687 }
688 byte* strings = Begin() + string_section.sh_offset;
689 byte* string = strings + i;
690 CHECK_LT(string, End()) << file_->GetPath();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800691 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692}
693
Brian Carlstromc1409452014-02-26 14:06:23 -0800694Elf32_Word ElfFile::GetDynamicNum() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000695 return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800696}
697
Brian Carlstromc1409452014-02-26 14:06:23 -0800698Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
700 return *(GetDynamicSectionStart() + i);
701}
702
Brian Carlstromc1409452014-02-26 14:06:23 -0800703Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000704 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
705 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800706 if (elf_dyn.d_tag == type) {
707 return elf_dyn.d_un.d_val;
708 }
709 }
710 return 0;
711}
712
Brian Carlstromc1409452014-02-26 14:06:23 -0800713Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000714 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
715 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800716}
717
Brian Carlstromc1409452014-02-26 14:06:23 -0800718Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000719 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800720 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
721 return section_header.sh_size / section_header.sh_entsize;
722}
723
Brian Carlstromc1409452014-02-26 14:06:23 -0800724Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000725 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800726 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
727 return *(GetRelSectionStart(section_header) + i);
728}
729
Brian Carlstromc1409452014-02-26 14:06:23 -0800730Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000731 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
732 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800733}
734
Brian Carlstromc1409452014-02-26 14:06:23 -0800735Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000736 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800737 return section_header.sh_size / section_header.sh_entsize;
738}
739
Brian Carlstromc1409452014-02-26 14:06:23 -0800740Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000741 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800742 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
743 return *(GetRelaSectionStart(section_header) + i);
744}
745
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800746// Base on bionic phdr_table_get_load_size
Brian Carlstromc1409452014-02-26 14:06:23 -0800747size_t ElfFile::GetLoadedSize() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000748 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
749 Elf32_Addr max_vaddr = 0x00000000u;
750 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
751 Elf32_Phdr& program_header = GetProgramHeader(i);
752 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800753 continue;
754 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000755 Elf32_Addr begin_vaddr = program_header.p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800756 if (begin_vaddr < min_vaddr) {
757 min_vaddr = begin_vaddr;
758 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000759 Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800760 if (end_vaddr > max_vaddr) {
761 max_vaddr = end_vaddr;
762 }
763 }
764 min_vaddr = RoundDown(min_vaddr, kPageSize);
765 max_vaddr = RoundUp(max_vaddr, kPageSize);
766 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
767 size_t loaded_size = max_vaddr - min_vaddr;
768 return loaded_size;
769}
770
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700771bool ElfFile::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800772 CHECK(program_header_only_) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000773 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
774 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800775
776 // Record .dynamic header information for later use
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000777 if (program_header.p_type == PT_DYNAMIC) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800778 dynamic_program_header_ = &program_header;
779 continue;
780 }
781
782 // Not something to load, move on.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000783 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800784 continue;
785 }
786
787 // Found something to load.
788
789 // If p_vaddr is zero, it must be the first loadable segment,
790 // since they must be in order. Since it is zero, there isn't a
791 // specific address requested, so first request a contiguous chunk
792 // of required size for all segments, but with no
793 // permissions. We'll then carve that up with the proper
794 // permissions as we load the actual segments. If p_vaddr is
795 // non-zero, the segments require the specific address specified,
796 // which either was specified in the file because we already set
797 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -0800798 int64_t temp_file_length = file_->GetLength();
799 if (temp_file_length < 0) {
800 errno = -temp_file_length;
801 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
802 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
803 return false;
804 }
805 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800806 if (program_header.p_vaddr == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700807 std::string reservation_name("ElfFile reservation for ");
808 reservation_name += file_->GetPath();
809 UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800810 NULL, GetLoadedSize(), PROT_NONE, false,
Brian Carlstromc1409452014-02-26 14:06:23 -0800811 error_msg));
812 if (reserve.get() == nullptr) {
813 *error_msg = StringPrintf("Failed to allocate %s: %s",
814 reservation_name.c_str(), error_msg->c_str());
815 return false;
816 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700817 base_address_ = reserve->Begin();
818 segments_.push_back(reserve.release());
819 }
820 // empty segment, nothing to map
821 if (program_header.p_memsz == 0) {
822 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800823 }
824 byte* p_vaddr = base_address_ + program_header.p_vaddr;
825 int prot = 0;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000826 if (executable && ((program_header.p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700827 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800828 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000829 if ((program_header.p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700830 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800831 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000832 if ((program_header.p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700833 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800834 }
835 int flags = MAP_FIXED;
836 if (writable_) {
837 prot |= PROT_WRITE;
838 flags |= MAP_SHARED;
839 } else {
840 flags |= MAP_PRIVATE;
841 }
Brian Carlstrom3a223612013-10-10 17:18:24 -0700842 if (file_length < (program_header.p_offset + program_header.p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800843 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700844 "%d of %d bytes: '%s'", file_length, i,
845 program_header.p_offset + program_header.p_memsz,
846 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700847 return false;
848 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800849 UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
850 program_header.p_memsz,
851 prot, flags, file_->Fd(),
852 program_header.p_offset,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700853 true,
854 file_->GetPath().c_str(),
855 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -0800856 if (segment.get() == nullptr) {
857 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
858 i, file_->GetPath().c_str(), error_msg->c_str());
859 return false;
860 }
861 if (segment->Begin() != p_vaddr) {
862 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
863 "instead mapped to %p",
864 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
865 return false;
866 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800867 segments_.push_back(segment.release());
868 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800869
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800870 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
871 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000872 = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr);
873 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
874 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800875 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
876 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000877 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800878 if (!ValidPointer(d_ptr)) {
879 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
880 d_ptr, file_->GetPath().c_str());
881 return false;
882 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000883 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800884 break;
885 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000886 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800887 if (!ValidPointer(d_ptr)) {
888 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
889 d_ptr, file_->GetPath().c_str());
890 return false;
891 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800892 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
893 break;
894 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000895 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800896 if (!ValidPointer(d_ptr)) {
897 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
898 d_ptr, file_->GetPath().c_str());
899 return false;
900 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000901 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800902 break;
903 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000904 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800905 if (GetDynamicNum() != i+1) {
906 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
907 "expected %d as implied by size of PT_DYNAMIC segment in %s",
908 i + 1, GetDynamicNum(), file_->GetPath().c_str());
909 return false;
910 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800911 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800912 }
913 }
914 }
915
Mark Mendellae9fd932014-02-10 16:14:35 -0800916 // Use GDB JIT support to do stack backtrace, etc.
917 if (executable) {
918 GdbJITSupport();
919 }
920
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800921 return true;
922}
923
Brian Carlstromc1409452014-02-26 14:06:23 -0800924bool ElfFile::ValidPointer(const byte* start) const {
925 for (size_t i = 0; i < segments_.size(); ++i) {
926 const MemMap* segment = segments_[i];
927 if (segment->Begin() <= start && start < segment->End()) {
928 return true;
929 }
930 }
931 return false;
932}
933
Mark Mendellae9fd932014-02-10 16:14:35 -0800934static bool check_section_name(ElfFile& file, int section_num, const char *name) {
935 Elf32_Shdr& section_header = file.GetSectionHeader(section_num);
936 const char *section_name = file.GetString(SHT_SYMTAB, section_header.sh_name);
937 return strcmp(name, section_name) == 0;
938}
939
940static void IncrementUint32(byte *p, uint32_t increment) {
941 uint32_t *u = reinterpret_cast<uint32_t *>(p);
942 *u += increment;
943}
944
945static void RoundAndClear(byte *image, uint32_t& offset, int pwr2) {
946 uint32_t mask = pwr2 - 1;
947 while (offset & mask) {
948 image[offset++] = 0;
949 }
950}
951
952// Simple macro to bump a point to a section header to the next one.
953#define BUMP_SHENT(sp) \
954 sp = reinterpret_cast<Elf32_Shdr *> (\
955 reinterpret_cast<byte*>(sp) + elf_hdr.e_shentsize);\
956 offset += elf_hdr.e_shentsize
957
958void ElfFile::GdbJITSupport() {
959 // We only get here if we only are mapping the program header.
960 DCHECK(program_header_only_);
961
962 // Well, we need the whole file to do this.
963 std::string error_msg;
964 UniquePtr<ElfFile> ptr(Open(const_cast<File*>(file_), false, false, &error_msg));
965 ElfFile& all = *ptr;
966
967 // Do we have interesting sections?
968 // Is this an OAT file with interesting sections?
969 if (all.GetSectionHeaderNum() != kExpectedSectionsInOATFile) {
970 return;
971 }
972 if (!check_section_name(all, 8, ".debug_info") ||
973 !check_section_name(all, 9, ".debug_abbrev") ||
974 !check_section_name(all, 10, ".debug_frame") ||
975 !check_section_name(all, 11, ".debug_str")) {
976 return;
977 }
978
Kenny Root6243e0e2014-03-03 13:33:48 -0800979 // This is not needed if we have no .text segment.
980 uint32_t text_start_addr = 0;
981 for (uint32_t i = 0; i < segments_.size(); i++) {
982 if (segments_[i]->GetProtect() & PROT_EXEC) {
983 // We found the .text section.
984 text_start_addr = PointerToLowMemUInt32(segments_[i]->Begin());
985 break;
986 }
987 }
988 if (text_start_addr == 0U) {
989 return;
990 }
991
Mark Mendellae9fd932014-02-10 16:14:35 -0800992 // Okay, we are good enough. Fake up an ELF image and tell GDB about it.
993 // We need some extra space for the debug and string sections, the ELF header, and the
994 // section header.
995 uint32_t needed_size = KB;
996
997 for (Elf32_Word i = 1; i < all.GetSectionHeaderNum(); i++) {
998 Elf32_Shdr& section_header = all.GetSectionHeader(i);
999 if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) {
1000 // Debug section: we need it.
1001 needed_size += section_header.sh_size;
1002 } else if (section_header.sh_type == SHT_STRTAB &&
1003 strcmp(".shstrtab",
1004 all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) {
1005 // We also need the shared string table.
1006 needed_size += section_header.sh_size;
1007
1008 // We also need the extra strings .symtab\0.strtab\0
1009 needed_size += 16;
1010 }
1011 }
1012
1013 // Start creating our image.
1014 jit_elf_image_ = new byte[needed_size];
1015
1016 // Create the Elf Header by copying the old one
1017 Elf32_Ehdr& elf_hdr =
1018 *reinterpret_cast<Elf32_Ehdr*>(jit_elf_image_);
1019
1020 elf_hdr = all.GetHeader();
1021 elf_hdr.e_entry = 0;
1022 elf_hdr.e_phoff = 0;
1023 elf_hdr.e_phnum = 0;
1024 elf_hdr.e_phentsize = 0;
1025 elf_hdr.e_type = ET_EXEC;
1026
1027 uint32_t offset = sizeof(Elf32_Ehdr);
1028
1029 // Copy the debug sections and string table.
1030 uint32_t debug_offsets[kExpectedSectionsInOATFile];
1031 memset(debug_offsets, '\0', sizeof debug_offsets);
1032 Elf32_Shdr *text_header = nullptr;
1033 int extra_shstrtab_entries = -1;
1034 int text_section_index = -1;
1035 int section_index = 1;
1036 for (Elf32_Word i = 1; i < kExpectedSectionsInOATFile; i++) {
1037 Elf32_Shdr& section_header = all.GetSectionHeader(i);
1038 // Round up to multiple of 4, ensuring zero fill.
1039 RoundAndClear(jit_elf_image_, offset, 4);
1040 if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) {
1041 // Debug section: we need it. Unfortunately, it wasn't mapped in.
1042 debug_offsets[i] = offset;
1043 // Read it from the file.
1044 lseek(file_->Fd(), section_header.sh_offset, SEEK_SET);
1045 read(file_->Fd(), jit_elf_image_ + offset, section_header.sh_size);
1046 offset += section_header.sh_size;
1047 section_index++;
1048 offset += 16;
1049 } else if (section_header.sh_type == SHT_STRTAB &&
1050 strcmp(".shstrtab",
1051 all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) {
1052 // We also need the shared string table.
1053 debug_offsets[i] = offset;
1054 // Read it from the file.
1055 lseek(file_->Fd(), section_header.sh_offset, SEEK_SET);
1056 read(file_->Fd(), jit_elf_image_ + offset, section_header.sh_size);
1057 offset += section_header.sh_size;
1058 // We also need the extra strings .symtab\0.strtab\0
1059 extra_shstrtab_entries = section_header.sh_size;
1060 memcpy(jit_elf_image_+offset, ".symtab\0.strtab\0", 16);
1061 offset += 16;
1062 section_index++;
1063 } else if (section_header.sh_flags & SHF_EXECINSTR) {
1064 DCHECK(strcmp(".text", all.GetString(SHT_SYMTAB,
1065 section_header.sh_name)) == 0);
1066 text_header = &section_header;
1067 text_section_index = section_index++;
1068 }
1069 }
1070 DCHECK(text_header != nullptr);
1071 DCHECK_NE(extra_shstrtab_entries, -1);
1072
1073 // We now need to update the addresses for debug_info and debug_frame to get to the
1074 // correct offset within the .text section.
Mark Mendellae9fd932014-02-10 16:14:35 -08001075 byte *p = jit_elf_image_+debug_offsets[8];
1076 byte *end = p + all.GetSectionHeader(8).sh_size;
1077
1078 // For debug_info; patch compilation using low_pc @ offset 13, high_pc at offset 17.
1079 IncrementUint32(p + 13, text_start_addr);
1080 IncrementUint32(p + 17, text_start_addr);
1081
1082 // Now fix the low_pc, high_pc for each method address.
1083 // First method starts at offset 0x15, each subsequent method is 1+3*4 bytes further.
1084 for (p += 0x15; p < end; p += 1 /* attr# */ + 3 * sizeof(uint32_t) /* addresses */) {
1085 IncrementUint32(p + 1 + sizeof(uint32_t), text_start_addr);
1086 IncrementUint32(p + 1 + 2 * sizeof(uint32_t), text_start_addr);
1087 }
1088
1089 // Now we have to handle the debug_frame method start addresses
1090 p = jit_elf_image_+debug_offsets[10];
1091 end = p + all.GetSectionHeader(10).sh_size;
1092
1093 // Skip past the CIE.
1094 p += *reinterpret_cast<uint32_t *>(p) + 4;
1095
1096 // And walk the FDEs.
1097 for (; p < end; p += *reinterpret_cast<uint32_t *>(p) + sizeof(uint32_t)) {
1098 IncrementUint32(p + 2 * sizeof(uint32_t), text_start_addr);
1099 }
1100
1101 // Create the data for the symbol table.
1102 const int kSymbtabAlignment = 16;
1103 RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment);
1104 uint32_t symtab_offset = offset;
1105
1106 // First entry is empty.
1107 memset(jit_elf_image_+offset, 0, sizeof(Elf32_Sym));
1108 offset += sizeof(Elf32_Sym);
1109
1110 // Symbol 1 is the real .text section.
1111 Elf32_Sym& sym_ent = *reinterpret_cast<Elf32_Sym*>(jit_elf_image_+offset);
1112 sym_ent.st_name = 1; /* .text */
1113 sym_ent.st_value = text_start_addr;
1114 sym_ent.st_size = text_header->sh_size;
1115 SetBindingAndType(&sym_ent, STB_LOCAL, STT_SECTION);
1116 sym_ent.st_other = 0;
1117 sym_ent.st_shndx = text_section_index;
1118 offset += sizeof(Elf32_Sym);
1119
1120 // Create the data for the string table.
1121 RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment);
1122 const int kTextStringSize = 7;
1123 uint32_t strtab_offset = offset;
1124 memcpy(jit_elf_image_+offset, "\0.text", kTextStringSize);
1125 offset += kTextStringSize;
1126
1127 // Create the section header table.
1128 // Round up to multiple of kSymbtabAlignment, ensuring zero fill.
1129 RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment);
1130 elf_hdr.e_shoff = offset;
1131 Elf32_Shdr *sp =
1132 reinterpret_cast<Elf32_Shdr *>(jit_elf_image_ + offset);
1133
1134 // Copy the first empty index.
1135 *sp = all.GetSectionHeader(0);
1136 BUMP_SHENT(sp);
1137
1138 elf_hdr.e_shnum = 1;
1139 for (Elf32_Word i = 1; i < kExpectedSectionsInOATFile; i++) {
1140 Elf32_Shdr& section_header = all.GetSectionHeader(i);
1141 if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) {
1142 // Debug section: we need it.
1143 *sp = section_header;
1144 sp->sh_offset = debug_offsets[i];
1145 sp->sh_addr = 0;
1146 elf_hdr.e_shnum++;
1147 BUMP_SHENT(sp);
1148 } else if (section_header.sh_type == SHT_STRTAB &&
1149 strcmp(".shstrtab",
1150 all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) {
1151 // We also need the shared string table.
1152 *sp = section_header;
1153 sp->sh_offset = debug_offsets[i];
1154 sp->sh_size += 16; /* sizeof ".symtab\0.strtab\0" */
1155 sp->sh_addr = 0;
1156 elf_hdr.e_shstrndx = elf_hdr.e_shnum;
1157 elf_hdr.e_shnum++;
1158 BUMP_SHENT(sp);
1159 }
1160 }
1161
1162 // Add a .text section for the matching code section.
1163 *sp = *text_header;
1164 sp->sh_type = SHT_NOBITS;
1165 sp->sh_offset = 0;
1166 sp->sh_addr = text_start_addr;
1167 elf_hdr.e_shnum++;
1168 BUMP_SHENT(sp);
1169
1170 // .symtab section: Need an empty index and the .text entry
1171 sp->sh_name = extra_shstrtab_entries;
1172 sp->sh_type = SHT_SYMTAB;
1173 sp->sh_flags = 0;
1174 sp->sh_addr = 0;
1175 sp->sh_offset = symtab_offset;
1176 sp->sh_size = 2 * sizeof(Elf32_Sym);
1177 sp->sh_link = elf_hdr.e_shnum + 1; // Link to .strtab section.
1178 sp->sh_info = 0;
1179 sp->sh_addralign = 16;
1180 sp->sh_entsize = sizeof(Elf32_Sym);
1181 elf_hdr.e_shnum++;
1182 BUMP_SHENT(sp);
1183
1184 // .strtab section: Enough for .text\0.
1185 sp->sh_name = extra_shstrtab_entries + 8;
1186 sp->sh_type = SHT_STRTAB;
1187 sp->sh_flags = 0;
1188 sp->sh_addr = 0;
1189 sp->sh_offset = strtab_offset;
1190 sp->sh_size = kTextStringSize;
1191 sp->sh_link = 0;
1192 sp->sh_info = 0;
1193 sp->sh_addralign = 16;
1194 sp->sh_entsize = 0;
1195 elf_hdr.e_shnum++;
1196 BUMP_SHENT(sp);
1197
1198 // We now have enough information to tell GDB about our file.
1199 jit_gdb_entry_ = CreateCodeEntry(jit_elf_image_, offset);
1200}
1201
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001202} // namespace art