blob: bb33978761dc48f910a389ea65eb1fdee1f7144d [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
Nicolas Geoffraya7f198c2014-03-10 11:12:54 +000019#include <sys/types.h>
20#include <unistd.h>
21
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022#include "base/logging.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070023#include "base/stringprintf.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/stl_util.h"
Alex Light3470ab42014-06-18 10:35:45 -070025#include "dwarf.h"
26#include "leb128.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027#include "utils.h"
Andreas Gampe91268c12014-04-03 17:50:24 -070028#include "instruction_set.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029
30namespace art {
31
Mark Mendellae9fd932014-02-10 16:14:35 -080032// -------------------------------------------------------------------
33// Binary GDB JIT Interface as described in
34// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
35extern "C" {
36 typedef enum {
37 JIT_NOACTION = 0,
38 JIT_REGISTER_FN,
39 JIT_UNREGISTER_FN
40 } JITAction;
41
42 struct JITCodeEntry {
43 JITCodeEntry* next_;
44 JITCodeEntry* prev_;
45 const byte *symfile_addr_;
46 uint64_t symfile_size_;
47 };
48
49 struct JITDescriptor {
50 uint32_t version_;
51 uint32_t action_flag_;
52 JITCodeEntry* relevant_entry_;
53 JITCodeEntry* first_entry_;
54 };
55
56 // GDB will place breakpoint into this function.
57 // To prevent GCC from inlining or removing it we place noinline attribute
58 // and inline assembler statement inside.
59 void __attribute__((noinline)) __jit_debug_register_code() {
60 __asm__("");
61 }
62
63 // GDB will inspect contents of this descriptor.
64 // Static initialization is necessary to prevent GDB from seeing
65 // uninitialized descriptor.
66 JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr };
67}
68
69
70static JITCodeEntry* CreateCodeEntry(const byte *symfile_addr,
71 uintptr_t symfile_size) {
72 JITCodeEntry* entry = new JITCodeEntry;
73 entry->symfile_addr_ = symfile_addr;
74 entry->symfile_size_ = symfile_size;
75 entry->prev_ = nullptr;
76
77 // TODO: Do we need a lock here?
78 entry->next_ = __jit_debug_descriptor.first_entry_;
79 if (entry->next_ != nullptr) {
80 entry->next_->prev_ = entry;
81 }
82 __jit_debug_descriptor.first_entry_ = entry;
83 __jit_debug_descriptor.relevant_entry_ = entry;
84
85 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
86 __jit_debug_register_code();
87 return entry;
88}
89
90
91static void UnregisterCodeEntry(JITCodeEntry* entry) {
92 // TODO: Do we need a lock here?
93 if (entry->prev_ != nullptr) {
94 entry->prev_->next_ = entry->next_;
95 } else {
96 __jit_debug_descriptor.first_entry_ = entry->next_;
97 }
98
99 if (entry->next_ != nullptr) {
100 entry->next_->prev_ = entry->prev_;
101 }
102
103 __jit_debug_descriptor.relevant_entry_ = entry;
104 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
105 __jit_debug_register_code();
106 delete entry;
107}
108
Brian Carlstromc1409452014-02-26 14:06:23 -0800109ElfFile::ElfFile(File* file, bool writable, bool program_header_only)
110 : file_(file),
111 writable_(writable),
112 program_header_only_(program_header_only),
Alex Light3470ab42014-06-18 10:35:45 -0700113 header_(nullptr),
114 base_address_(nullptr),
115 program_headers_start_(nullptr),
116 section_headers_start_(nullptr),
117 dynamic_program_header_(nullptr),
118 dynamic_section_start_(nullptr),
119 symtab_section_start_(nullptr),
120 dynsym_section_start_(nullptr),
121 strtab_section_start_(nullptr),
122 dynstr_section_start_(nullptr),
123 hash_section_start_(nullptr),
124 symtab_symbol_table_(nullptr),
125 dynsym_symbol_table_(nullptr),
126 jit_elf_image_(nullptr),
127 jit_gdb_entry_(nullptr) {
128 CHECK(file != nullptr);
Brian Carlstromc1409452014-02-26 14:06:23 -0800129}
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700131ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
132 std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700133 std::unique_ptr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800134 int prot;
135 int flags;
Alex Light3470ab42014-06-18 10:35:45 -0700136 if (writable) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800137 prot = PROT_READ | PROT_WRITE;
138 flags = MAP_SHARED;
139 } else {
140 prot = PROT_READ;
141 flags = MAP_PRIVATE;
142 }
Alex Light3470ab42014-06-18 10:35:45 -0700143 if (!elf_file->Setup(prot, flags, error_msg)) {
144 return nullptr;
145 }
146 return elf_file.release();
147}
148
149ElfFile* ElfFile::Open(File* file, int prot, int flags, std::string* error_msg) {
150 std::unique_ptr<ElfFile> elf_file(new ElfFile(file, (prot & PROT_WRITE) == PROT_WRITE, false));
151 if (!elf_file->Setup(prot, flags, error_msg)) {
152 return nullptr;
153 }
154 return elf_file.release();
155}
156
157bool ElfFile::Setup(int prot, int flags, std::string* error_msg) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800158 int64_t temp_file_length = file_->GetLength();
159 if (temp_file_length < 0) {
160 errno = -temp_file_length;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
162 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800163 return false;
164 }
Ian Rogerscdfcf372014-01-23 20:38:36 -0800165 size_t file_length = static_cast<size_t>(temp_file_length);
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000166 if (file_length < sizeof(Elf32_Ehdr)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800167 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000168 "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700169 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170 return false;
171 }
172
Brian Carlstromc1409452014-02-26 14:06:23 -0800173 if (program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800174 // first just map ELF header to get program header size information
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000175 size_t elf_header_size = sizeof(Elf32_Ehdr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700176 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800177 file_->GetPath().c_str(), error_msg),
178 error_msg)) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800179 return false;
180 }
181 // then remap to cover program header
182 size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
Brian Carlstrom3a223612013-10-10 17:18:24 -0700183 if (file_length < program_header_size) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800184 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700185 "header of %zd bytes: '%s'", file_length,
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000186 sizeof(Elf32_Ehdr), file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700187 return false;
188 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700189 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800190 file_->GetPath().c_str(), error_msg),
191 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700192 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800193 return false;
194 }
195 } else {
196 // otherwise map entire file
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800198 file_->GetPath().c_str(), error_msg),
199 error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700200 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800201 return false;
202 }
203 }
204
205 // Either way, the program header is relative to the elf header
206 program_headers_start_ = Begin() + GetHeader().e_phoff;
207
Brian Carlstromc1409452014-02-26 14:06:23 -0800208 if (!program_header_only_) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800209 // Setup section headers.
210 section_headers_start_ = Begin() + GetHeader().e_shoff;
211
212 // Find .dynamic section info from program header
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000213 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
Alex Light3470ab42014-06-18 10:35:45 -0700214 if (dynamic_program_header_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
216 file_->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800217 return false;
218 }
219
220 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000221 = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800222
223 // Find other sections from section headers
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000224 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
225 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800226 byte* section_addr = Begin() + section_header.sh_offset;
227 switch (section_header.sh_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000228 case SHT_SYMTAB: {
229 symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 break;
231 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000232 case SHT_DYNSYM: {
233 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800234 break;
235 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000236 case SHT_STRTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800237 // TODO: base these off of sh_link from .symtab and .dynsym above
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000238 if ((section_header.sh_flags & SHF_ALLOC) != 0) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800239 dynstr_section_start_ = reinterpret_cast<char*>(section_addr);
240 } else {
241 strtab_section_start_ = reinterpret_cast<char*>(section_addr);
242 }
243 break;
244 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000245 case SHT_DYNAMIC: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800246 if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) {
247 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
Brian Carlstrom265091e2013-01-30 14:08:26 -0800248 << file_->GetPath() << ": " << std::hex
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800249 << reinterpret_cast<void*>(dynamic_section_start_)
250 << " != " << reinterpret_cast<void*>(section_addr);
251 return false;
252 }
253 break;
254 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000255 case SHT_HASH: {
256 hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800257 break;
258 }
259 }
260 }
261 }
262 return true;
263}
264
265ElfFile::~ElfFile() {
266 STLDeleteElements(&segments_);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800267 delete symtab_symbol_table_;
268 delete dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800269 delete jit_elf_image_;
270 if (jit_gdb_entry_) {
271 UnregisterCodeEntry(jit_gdb_entry_);
272 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800273}
274
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800275bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
Alex Light3470ab42014-06-18 10:35:45 -0700276 if (map == nullptr) {
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800277 // MemMap::Open should have already set an error.
278 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800279 return false;
280 }
281 map_.reset(map);
Alex Light3470ab42014-06-18 10:35:45 -0700282 CHECK(map_.get() != nullptr) << file_->GetPath();
283 CHECK(map_->Begin() != nullptr) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800284
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000285 header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin());
286 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
287 || (ELFMAG1 != header_->e_ident[EI_MAG1])
288 || (ELFMAG2 != header_->e_ident[EI_MAG2])
289 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800290 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
291 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800292 file_->GetPath().c_str(),
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000293 header_->e_ident[EI_MAG0],
294 header_->e_ident[EI_MAG1],
295 header_->e_ident[EI_MAG2],
296 header_->e_ident[EI_MAG3]);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800297 return false;
298 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800299 if (ELFCLASS32 != header_->e_ident[EI_CLASS]) {
300 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
301 ELFCLASS32,
302 file_->GetPath().c_str(),
303 header_->e_ident[EI_CLASS]);
304 return false;
305 }
306 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
307 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
308 ELFDATA2LSB,
309 file_->GetPath().c_str(),
310 header_->e_ident[EI_CLASS]);
311 return false;
312 }
313 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
314 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
315 EV_CURRENT,
316 file_->GetPath().c_str(),
317 header_->e_ident[EI_CLASS]);
318 return false;
319 }
320 if (ET_DYN != header_->e_type) {
321 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
322 ET_DYN,
323 file_->GetPath().c_str(),
324 header_->e_type);
325 return false;
326 }
327 if (EV_CURRENT != header_->e_version) {
328 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
329 EV_CURRENT,
330 file_->GetPath().c_str(),
331 header_->e_version);
332 return false;
333 }
334 if (0 != header_->e_entry) {
335 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
336 0,
337 file_->GetPath().c_str(),
338 header_->e_entry);
339 return false;
340 }
341 if (0 == header_->e_phoff) {
342 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
343 file_->GetPath().c_str());
344 return false;
345 }
346 if (0 == header_->e_shoff) {
347 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
348 file_->GetPath().c_str());
349 return false;
350 }
351 if (0 == header_->e_ehsize) {
352 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
353 file_->GetPath().c_str());
354 return false;
355 }
356 if (0 == header_->e_phentsize) {
357 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
358 file_->GetPath().c_str());
359 return false;
360 }
361 if (0 == header_->e_phnum) {
362 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
363 file_->GetPath().c_str());
364 return false;
365 }
366 if (0 == header_->e_shentsize) {
367 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
368 file_->GetPath().c_str());
369 return false;
370 }
371 if (0 == header_->e_shnum) {
372 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
373 file_->GetPath().c_str());
374 return false;
375 }
376 if (0 == header_->e_shstrndx) {
377 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
378 file_->GetPath().c_str());
379 return false;
380 }
381 if (header_->e_shstrndx >= header_->e_shnum) {
382 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
383 header_->e_shstrndx,
384 header_->e_shnum,
385 file_->GetPath().c_str());
386 return false;
387 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800388
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800389 if (!program_header_only_) {
Brian Carlstromc1409452014-02-26 14:06:23 -0800390 if (header_->e_phoff >= Size()) {
Dmitry Petrochenko659d87d2014-02-27 14:23:11 +0700391 *error_msg = StringPrintf("Failed to find e_phoff value %d less than %zd in %s",
Brian Carlstromc1409452014-02-26 14:06:23 -0800392 header_->e_phoff,
393 Size(),
394 file_->GetPath().c_str());
395 return false;
396 }
397 if (header_->e_shoff >= Size()) {
Dmitry Petrochenko659d87d2014-02-27 14:23:11 +0700398 *error_msg = StringPrintf("Failed to find e_shoff value %d less than %zd in %s",
Brian Carlstromc1409452014-02-26 14:06:23 -0800399 header_->e_shoff,
400 Size(),
401 file_->GetPath().c_str());
402 return false;
403 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800404 }
405 return true;
406}
407
408
Brian Carlstromc1409452014-02-26 14:06:23 -0800409Elf32_Ehdr& ElfFile::GetHeader() const {
Alex Light3470ab42014-06-18 10:35:45 -0700410 CHECK(header_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800411 return *header_;
412}
413
Brian Carlstromc1409452014-02-26 14:06:23 -0800414byte* ElfFile::GetProgramHeadersStart() const {
Alex Light3470ab42014-06-18 10:35:45 -0700415 CHECK(program_headers_start_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800416 return program_headers_start_;
417}
418
Brian Carlstromc1409452014-02-26 14:06:23 -0800419byte* ElfFile::GetSectionHeadersStart() const {
Alex Light3470ab42014-06-18 10:35:45 -0700420 CHECK(section_headers_start_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800421 return section_headers_start_;
422}
423
Brian Carlstromc1409452014-02-26 14:06:23 -0800424Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const {
Alex Light3470ab42014-06-18 10:35:45 -0700425 CHECK(dynamic_program_header_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426 return *dynamic_program_header_;
427}
428
Brian Carlstromc1409452014-02-26 14:06:23 -0800429Elf32_Dyn* ElfFile::GetDynamicSectionStart() const {
Alex Light3470ab42014-06-18 10:35:45 -0700430 CHECK(dynamic_section_start_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800431 return dynamic_section_start_;
432}
433
Brian Carlstromc1409452014-02-26 14:06:23 -0800434Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800435 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000436 Elf32_Sym* symbol_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800437 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000438 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800439 symbol_section_start = symtab_section_start_;
440 break;
441 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000442 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800443 symbol_section_start = dynsym_section_start_;
444 break;
445 }
446 default: {
447 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700448 symbol_section_start = nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 }
450 }
Alex Light3470ab42014-06-18 10:35:45 -0700451 CHECK(symbol_section_start != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800452 return symbol_section_start;
453}
454
Brian Carlstromc1409452014-02-26 14:06:23 -0800455const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800456 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800457 const char* string_section_start;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800458 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000459 case SHT_SYMTAB: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800460 string_section_start = strtab_section_start_;
461 break;
462 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000463 case SHT_DYNSYM: {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800464 string_section_start = dynstr_section_start_;
465 break;
466 }
467 default: {
468 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700469 string_section_start = nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800470 }
471 }
Alex Light3470ab42014-06-18 10:35:45 -0700472 CHECK(string_section_start != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800473 return string_section_start;
474}
475
Brian Carlstromc1409452014-02-26 14:06:23 -0800476const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800477 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
478 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700479 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800480 }
481 const char* string_section_start = GetStringSectionStart(section_type);
482 const char* string = string_section_start + i;
483 return string;
484}
485
Brian Carlstromc1409452014-02-26 14:06:23 -0800486Elf32_Word* ElfFile::GetHashSectionStart() const {
Alex Light3470ab42014-06-18 10:35:45 -0700487 CHECK(hash_section_start_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800488 return hash_section_start_;
489}
490
Brian Carlstromc1409452014-02-26 14:06:23 -0800491Elf32_Word ElfFile::GetHashBucketNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800492 return GetHashSectionStart()[0];
493}
494
Brian Carlstromc1409452014-02-26 14:06:23 -0800495Elf32_Word ElfFile::GetHashChainNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800496 return GetHashSectionStart()[1];
497}
498
Brian Carlstromc1409452014-02-26 14:06:23 -0800499Elf32_Word ElfFile::GetHashBucket(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800500 CHECK_LT(i, GetHashBucketNum());
501 // 0 is nbucket, 1 is nchain
502 return GetHashSectionStart()[2 + i];
503}
504
Brian Carlstromc1409452014-02-26 14:06:23 -0800505Elf32_Word ElfFile::GetHashChain(size_t i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800506 CHECK_LT(i, GetHashChainNum());
507 // 0 is nbucket, 1 is nchain, & chains are after buckets
508 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
509}
510
Brian Carlstromc1409452014-02-26 14:06:23 -0800511Elf32_Word ElfFile::GetProgramHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800512 return GetHeader().e_phnum;
513}
514
Brian Carlstromc1409452014-02-26 14:06:23 -0800515Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800516 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath();
517 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
518 CHECK_LT(program_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000519 return *reinterpret_cast<Elf32_Phdr*>(program_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800520}
521
Brian Carlstromc1409452014-02-26 14:06:23 -0800522Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000523 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
524 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800525 if (program_header.p_type == type) {
526 return &program_header;
527 }
528 }
Alex Light3470ab42014-06-18 10:35:45 -0700529 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800530}
531
Brian Carlstromc1409452014-02-26 14:06:23 -0800532Elf32_Word ElfFile::GetSectionHeaderNum() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800533 return GetHeader().e_shnum;
534}
535
Brian Carlstromc1409452014-02-26 14:06:23 -0800536Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800537 // Can only access arbitrary sections when we have the whole file, not just program header.
538 // Even if we Load(), it doesn't bring in all the sections.
539 CHECK(!program_header_only_) << file_->GetPath();
540 CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath();
541 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
542 CHECK_LT(section_header, End()) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000543 return *reinterpret_cast<Elf32_Shdr*>(section_header);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800544}
545
Brian Carlstromc1409452014-02-26 14:06:23 -0800546Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800547 // Can only access arbitrary sections when we have the whole file, not just program header.
548 // We could change this to switch on known types if they were detected during loading.
549 CHECK(!program_header_only_) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000550 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
551 Elf32_Shdr& section_header = GetSectionHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800552 if (section_header.sh_type == type) {
553 return &section_header;
554 }
555 }
Alex Light3470ab42014-06-18 10:35:45 -0700556 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800557}
558
559// from bionic
Brian Carlstrom265091e2013-01-30 14:08:26 -0800560static unsigned elfhash(const char *_name) {
561 const unsigned char *name = (const unsigned char *) _name;
562 unsigned h = 0, g;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800563
Brian Carlstromdf629502013-07-17 22:39:56 -0700564 while (*name) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800565 h = (h << 4) + *name++;
566 g = h & 0xf0000000;
567 h ^= g;
568 h ^= g >> 24;
569 }
570 return h;
571}
572
Brian Carlstromc1409452014-02-26 14:06:23 -0800573Elf32_Shdr& ElfFile::GetSectionNameStringSection() const {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800574 return GetSectionHeader(GetHeader().e_shstrndx);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800575}
576
Brian Carlstromc1409452014-02-26 14:06:23 -0800577const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
Alex Light3470ab42014-06-18 10:35:45 -0700578 const Elf32_Sym* sym = FindDynamicSymbol(symbol_name);
579 if (sym != nullptr) {
580 return base_address_ + sym->st_value;
581 } else {
582 return nullptr;
583 }
584}
585
586const Elf32_Sym* ElfFile::FindDynamicSymbol(const std::string& symbol_name) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000587 Elf32_Word hash = elfhash(symbol_name.c_str());
588 Elf32_Word bucket_index = hash % GetHashBucketNum();
589 Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800590 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000591 Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
592 const char* name = GetString(SHT_DYNSYM, symbol.st_name);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800593 if (symbol_name == name) {
Alex Light3470ab42014-06-18 10:35:45 -0700594 return &symbol;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800595 }
596 symbol_and_chain_index = GetHashChain(symbol_and_chain_index);
597 }
Alex Light3470ab42014-06-18 10:35:45 -0700598 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800599}
600
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000601bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) {
602 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800603}
604
Brian Carlstromc1409452014-02-26 14:06:23 -0800605Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const {
606 CHECK(IsSymbolSectionType(section_header.sh_type))
607 << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800608 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
609 return section_header.sh_size / section_header.sh_entsize;
610}
611
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000612Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800613 Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800614 return *(GetSymbolSectionStart(section_type) + i);
615}
616
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000617ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800618 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
619 switch (section_type) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000620 case SHT_SYMTAB: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800621 return &symtab_symbol_table_;
622 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000623 case SHT_DYNSYM: {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800624 return &dynsym_symbol_table_;
625 }
626 default: {
627 LOG(FATAL) << section_type;
Alex Light3470ab42014-06-18 10:35:45 -0700628 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800629 }
630 }
631}
632
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000633Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type,
634 const std::string& symbol_name,
635 bool build_map) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800636 CHECK(!program_header_only_) << file_->GetPath();
637 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800638
639 SymbolTable** symbol_table = GetSymbolTable(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700640 if (*symbol_table != nullptr || build_map) {
641 if (*symbol_table == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800642 DCHECK(build_map);
643 *symbol_table = new SymbolTable;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000644 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700645 CHECK(symbol_section != nullptr) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000646 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800647 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000648 Elf32_Sym& symbol = GetSymbol(section_type, i);
649 unsigned char type = ELF32_ST_TYPE(symbol.st_info);
650 if (type == STT_NOTYPE) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800651 continue;
652 }
653 const char* name = GetString(string_section, symbol.st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700654 if (name == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -0800655 continue;
656 }
Brian Carlstromc1409452014-02-26 14:06:23 -0800657 std::pair<SymbolTable::iterator, bool> result =
658 (*symbol_table)->insert(std::make_pair(name, &symbol));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800659 if (!result.second) {
660 // If a duplicate, make sure it has the same logical value. Seen on x86.
661 CHECK_EQ(symbol.st_value, result.first->second->st_value);
662 CHECK_EQ(symbol.st_size, result.first->second->st_size);
663 CHECK_EQ(symbol.st_info, result.first->second->st_info);
664 CHECK_EQ(symbol.st_other, result.first->second->st_other);
665 CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx);
666 }
667 }
668 }
Alex Light3470ab42014-06-18 10:35:45 -0700669 CHECK(*symbol_table != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800670 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
671 if (it == (*symbol_table)->end()) {
Alex Light3470ab42014-06-18 10:35:45 -0700672 return nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800673 }
674 return it->second;
675 }
676
677 // Fall back to linear search
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000678 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
Alex Light3470ab42014-06-18 10:35:45 -0700679 CHECK(symbol_section != nullptr) << file_->GetPath();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000680 Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800681 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000682 Elf32_Sym& symbol = GetSymbol(section_type, i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800683 const char* name = GetString(string_section, symbol.st_name);
Alex Light3470ab42014-06-18 10:35:45 -0700684 if (name == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800685 continue;
686 }
687 if (symbol_name == name) {
688 return &symbol;
689 }
690 }
Alex Light3470ab42014-06-18 10:35:45 -0700691 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800692}
693
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000694Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800695 const std::string& symbol_name,
696 bool build_map) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000697 Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
Alex Light3470ab42014-06-18 10:35:45 -0700698 if (symbol == nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699 return 0;
700 }
701 return symbol->st_value;
702}
703
Brian Carlstromc1409452014-02-26 14:06:23 -0800704const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800705 CHECK(!program_header_only_) << file_->GetPath();
706 // TODO: remove this static_cast from enum when using -std=gnu++0x
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000707 CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800708 CHECK_LT(i, string_section.sh_size) << file_->GetPath();
709 if (i == 0) {
Alex Light3470ab42014-06-18 10:35:45 -0700710 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800711 }
712 byte* strings = Begin() + string_section.sh_offset;
713 byte* string = strings + i;
714 CHECK_LT(string, End()) << file_->GetPath();
Brian Carlstrom265091e2013-01-30 14:08:26 -0800715 return reinterpret_cast<const char*>(string);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800716}
717
Brian Carlstromc1409452014-02-26 14:06:23 -0800718Elf32_Word ElfFile::GetDynamicNum() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000719 return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800720}
721
Brian Carlstromc1409452014-02-26 14:06:23 -0800722Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800723 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
724 return *(GetDynamicSectionStart() + i);
725}
726
Brian Carlstromc1409452014-02-26 14:06:23 -0800727Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000728 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
729 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800730 if (elf_dyn.d_tag == type) {
731 return elf_dyn.d_un.d_val;
732 }
733 }
734 return 0;
735}
736
Brian Carlstromc1409452014-02-26 14:06:23 -0800737Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000738 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
739 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800740}
741
Brian Carlstromc1409452014-02-26 14:06:23 -0800742Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000743 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800744 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
745 return section_header.sh_size / section_header.sh_entsize;
746}
747
Brian Carlstromc1409452014-02-26 14:06:23 -0800748Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000749 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800750 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
751 return *(GetRelSectionStart(section_header) + i);
752}
753
Brian Carlstromc1409452014-02-26 14:06:23 -0800754Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000755 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
756 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800757}
758
Brian Carlstromc1409452014-02-26 14:06:23 -0800759Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000760 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800761 return section_header.sh_size / section_header.sh_entsize;
762}
763
Brian Carlstromc1409452014-02-26 14:06:23 -0800764Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000765 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800766 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
767 return *(GetRelaSectionStart(section_header) + i);
768}
769
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800770// Base on bionic phdr_table_get_load_size
Brian Carlstromc1409452014-02-26 14:06:23 -0800771size_t ElfFile::GetLoadedSize() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000772 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
773 Elf32_Addr max_vaddr = 0x00000000u;
774 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
775 Elf32_Phdr& program_header = GetProgramHeader(i);
776 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800777 continue;
778 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000779 Elf32_Addr begin_vaddr = program_header.p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800780 if (begin_vaddr < min_vaddr) {
781 min_vaddr = begin_vaddr;
782 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000783 Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800784 if (end_vaddr > max_vaddr) {
785 max_vaddr = end_vaddr;
786 }
787 }
788 min_vaddr = RoundDown(min_vaddr, kPageSize);
789 max_vaddr = RoundUp(max_vaddr, kPageSize);
790 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
791 size_t loaded_size = max_vaddr - min_vaddr;
792 return loaded_size;
793}
794
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700795bool ElfFile::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800796 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -0700797
798 if (executable) {
799 InstructionSet elf_ISA = kNone;
800 switch (GetHeader().e_machine) {
801 case EM_ARM: {
802 elf_ISA = kArm;
803 break;
804 }
805 case EM_AARCH64: {
806 elf_ISA = kArm64;
807 break;
808 }
809 case EM_386: {
810 elf_ISA = kX86;
811 break;
812 }
813 case EM_X86_64: {
814 elf_ISA = kX86_64;
815 break;
816 }
817 case EM_MIPS: {
818 elf_ISA = kMips;
819 break;
820 }
821 }
822
823 if (elf_ISA != kRuntimeISA) {
824 std::ostringstream oss;
825 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
826 *error_msg = oss.str();
827 return false;
828 }
829 }
830
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000831 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
832 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800833
834 // Record .dynamic header information for later use
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000835 if (program_header.p_type == PT_DYNAMIC) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800836 dynamic_program_header_ = &program_header;
837 continue;
838 }
839
840 // Not something to load, move on.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000841 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800842 continue;
843 }
844
845 // Found something to load.
846
847 // If p_vaddr is zero, it must be the first loadable segment,
848 // since they must be in order. Since it is zero, there isn't a
849 // specific address requested, so first request a contiguous chunk
850 // of required size for all segments, but with no
851 // permissions. We'll then carve that up with the proper
852 // permissions as we load the actual segments. If p_vaddr is
853 // non-zero, the segments require the specific address specified,
854 // which either was specified in the file because we already set
855 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -0800856 int64_t temp_file_length = file_->GetLength();
857 if (temp_file_length < 0) {
858 errno = -temp_file_length;
859 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
860 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
861 return false;
862 }
863 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800864 if (program_header.p_vaddr == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700865 std::string reservation_name("ElfFile reservation for ");
866 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -0700867 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Alex Light3470ab42014-06-18 10:35:45 -0700868 nullptr, GetLoadedSize(), PROT_NONE, false,
Brian Carlstromc1409452014-02-26 14:06:23 -0800869 error_msg));
870 if (reserve.get() == nullptr) {
871 *error_msg = StringPrintf("Failed to allocate %s: %s",
872 reservation_name.c_str(), error_msg->c_str());
873 return false;
874 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700875 base_address_ = reserve->Begin();
876 segments_.push_back(reserve.release());
877 }
878 // empty segment, nothing to map
879 if (program_header.p_memsz == 0) {
880 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800881 }
882 byte* p_vaddr = base_address_ + program_header.p_vaddr;
883 int prot = 0;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000884 if (executable && ((program_header.p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700885 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800886 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000887 if ((program_header.p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700888 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800889 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000890 if ((program_header.p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700891 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800892 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700893 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800894 if (writable_) {
895 prot |= PROT_WRITE;
896 flags |= MAP_SHARED;
897 } else {
898 flags |= MAP_PRIVATE;
899 }
Brian Carlstrom3a223612013-10-10 17:18:24 -0700900 if (file_length < (program_header.p_offset + program_header.p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800901 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700902 "%d of %d bytes: '%s'", file_length, i,
903 program_header.p_offset + program_header.p_memsz,
904 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700905 return false;
906 }
Ian Rogers700a4022014-05-19 16:49:03 -0700907 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800908 program_header.p_memsz,
909 prot, flags, file_->Fd(),
910 program_header.p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700911 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700912 file_->GetPath().c_str(),
913 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -0800914 if (segment.get() == nullptr) {
915 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
916 i, file_->GetPath().c_str(), error_msg->c_str());
917 return false;
918 }
919 if (segment->Begin() != p_vaddr) {
920 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
921 "instead mapped to %p",
922 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
923 return false;
924 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800925 segments_.push_back(segment.release());
926 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800927
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800928 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
929 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000930 = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr);
931 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
932 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800933 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
934 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000935 case DT_HASH: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800936 if (!ValidPointer(d_ptr)) {
937 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
938 d_ptr, file_->GetPath().c_str());
939 return false;
940 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000941 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800942 break;
943 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000944 case DT_STRTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800945 if (!ValidPointer(d_ptr)) {
946 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
947 d_ptr, file_->GetPath().c_str());
948 return false;
949 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800950 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
951 break;
952 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000953 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800954 if (!ValidPointer(d_ptr)) {
955 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
956 d_ptr, file_->GetPath().c_str());
957 return false;
958 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000959 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800960 break;
961 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000962 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800963 if (GetDynamicNum() != i+1) {
964 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
965 "expected %d as implied by size of PT_DYNAMIC segment in %s",
966 i + 1, GetDynamicNum(), file_->GetPath().c_str());
967 return false;
968 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800969 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800970 }
971 }
972 }
973
Mark Mendellae9fd932014-02-10 16:14:35 -0800974 // Use GDB JIT support to do stack backtrace, etc.
975 if (executable) {
976 GdbJITSupport();
977 }
978
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800979 return true;
980}
981
Brian Carlstromc1409452014-02-26 14:06:23 -0800982bool ElfFile::ValidPointer(const byte* start) const {
983 for (size_t i = 0; i < segments_.size(); ++i) {
984 const MemMap* segment = segments_[i];
985 if (segment->Begin() <= start && start < segment->End()) {
986 return true;
987 }
988 }
989 return false;
990}
991
Alex Light3470ab42014-06-18 10:35:45 -0700992
993Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const {
994 CHECK(!program_header_only_);
995 Elf32_Shdr& shstrtab_sec = GetSectionNameStringSection();
996 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
997 Elf32_Shdr& shdr = GetSectionHeader(i);
998 const char* sec_name = GetString(shstrtab_sec, shdr.sh_name);
999 if (sec_name == nullptr) {
1000 continue;
1001 }
1002 if (name == sec_name) {
1003 return &shdr;
1004 }
1005 }
1006 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001007}
1008
Alex Light3470ab42014-06-18 10:35:45 -07001009struct PACKED(1) FDE {
1010 uint32_t raw_length_;
1011 uint32_t GetLength() {
1012 return raw_length_ + sizeof(raw_length_);
1013 }
1014 uint32_t CIE_pointer;
1015 uint32_t initial_location;
1016 uint32_t address_range;
1017 uint8_t instructions[0];
1018};
1019
1020static FDE* NextFDE(FDE* frame) {
1021 byte* fde_bytes = reinterpret_cast<byte*>(frame);
1022 fde_bytes += frame->GetLength();
1023 return reinterpret_cast<FDE*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001024}
1025
Alex Light3470ab42014-06-18 10:35:45 -07001026static bool IsFDE(FDE* frame) {
1027 // TODO This seems to be the constant everyone uses (for the .debug_frame
1028 // section at least), however we should investigate this further.
1029 const uint32_t kDwarfCIE_id = 0xffffffff;
1030 const uint32_t kReservedLengths[] = {0xffffffff, 0xfffffff0};
1031 return frame->CIE_pointer != kDwarfCIE_id &&
1032 frame->raw_length_ != kReservedLengths[0] && frame->raw_length_ != kReservedLengths[1];
1033}
1034
1035// TODO This only works for 32-bit Elf Files.
1036static bool FixupDebugFrame(uintptr_t text_start, byte* dbg_frame, size_t dbg_frame_size) {
1037 FDE* last_frame = reinterpret_cast<FDE*>(dbg_frame + dbg_frame_size);
1038 FDE* frame = NextFDE(reinterpret_cast<FDE*>(dbg_frame));
1039 for (; frame < last_frame; frame = NextFDE(frame)) {
1040 if (!IsFDE(frame)) {
1041 return false;
1042 }
1043 frame->initial_location += text_start;
1044 }
1045 return true;
1046}
1047
1048struct PACKED(1) DebugInfoHeader {
1049 uint32_t unit_length; // TODO 32-bit specific size
1050 uint16_t version;
1051 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1052 uint8_t address_size;
1053};
1054
1055// Returns -1 if it is variable length, which we will just disallow for now.
1056static int32_t FormLength(uint32_t att) {
1057 switch (att) {
1058 case DW_FORM_data1:
1059 case DW_FORM_flag:
1060 case DW_FORM_flag_present:
1061 case DW_FORM_ref1:
1062 return 1;
1063
1064 case DW_FORM_data2:
1065 case DW_FORM_ref2:
1066 return 2;
1067
1068 case DW_FORM_addr: // TODO 32-bit only
1069 case DW_FORM_ref_addr: // TODO 32-bit only
1070 case DW_FORM_sec_offset: // TODO 32-bit only
1071 case DW_FORM_strp: // TODO 32-bit only
1072 case DW_FORM_data4:
1073 case DW_FORM_ref4:
1074 return 4;
1075
1076 case DW_FORM_data8:
1077 case DW_FORM_ref8:
1078 case DW_FORM_ref_sig8:
1079 return 8;
1080
1081 case DW_FORM_block:
1082 case DW_FORM_block1:
1083 case DW_FORM_block2:
1084 case DW_FORM_block4:
1085 case DW_FORM_exprloc:
1086 case DW_FORM_indirect:
1087 case DW_FORM_ref_udata:
1088 case DW_FORM_sdata:
1089 case DW_FORM_string:
1090 case DW_FORM_udata:
1091 default:
1092 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001093 }
1094}
1095
Alex Light3470ab42014-06-18 10:35:45 -07001096class DebugTag {
1097 public:
1098 const uint32_t index_;
1099 ~DebugTag() {}
1100 // Creates a new tag and moves data pointer up to the start of the next one.
1101 // nullptr means error.
1102 static DebugTag* Create(const byte** data_pointer) {
1103 const byte* data = *data_pointer;
1104 uint32_t index = DecodeUnsignedLeb128(&data);
1105 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1106 tag->size_ = static_cast<uint32_t>(
1107 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1108 // skip the abbrev
1109 tag->tag_ = DecodeUnsignedLeb128(&data);
1110 tag->has_child_ = (*data == 0);
1111 data++;
1112 while (true) {
1113 uint32_t attr = DecodeUnsignedLeb128(&data);
1114 uint32_t form = DecodeUnsignedLeb128(&data);
1115 if (attr == 0 && form == 0) {
1116 break;
1117 } else if (attr == 0 || form == 0) {
1118 // Bad abbrev.
1119 return nullptr;
1120 }
1121 int32_t size = FormLength(form);
1122 if (size == -1) {
1123 return nullptr;
1124 }
1125 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1126 }
1127 *data_pointer = data;
1128 return tag.release();
1129 }
1130
1131 uint32_t GetSize() const {
1132 return size_;
1133 }
1134
1135 bool HasChild() {
1136 return has_child_;
1137 }
1138
1139 uint32_t GetTagNumber() {
1140 return tag_;
1141 }
1142
1143 // Gets the offset of a particular attribute in this tag structure.
1144 // Interpretation of the data is left to the consumer. 0 is returned if the
1145 // tag does not contain the attribute.
1146 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1147 auto it = off_map_.find(dwarf_attribute);
1148 if (it == off_map_.end()) {
1149 return 0;
1150 } else {
1151 return it->second;
1152 }
1153 }
1154
1155 // Gets the size of attribute
1156 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1157 auto it = size_map_.find(dwarf_attribute);
1158 if (it == size_map_.end()) {
1159 return 0;
1160 } else {
1161 return it->second;
1162 }
1163 }
1164
1165 private:
1166 explicit DebugTag(uint32_t index) : index_(index) {}
1167 void AddAttribute(uint32_t type, uint32_t attr_size) {
1168 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1169 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1170 size_ += attr_size;
1171 }
1172 std::map<uint32_t, uint32_t> off_map_;
1173 std::map<uint32_t, uint32_t> size_map_;
1174 uint32_t size_;
1175 uint32_t tag_;
1176 bool has_child_;
1177};
1178
1179class DebugAbbrev {
1180 public:
1181 ~DebugAbbrev() {}
1182 static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) {
1183 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev);
1184 const byte* last = dbg_abbrev + dbg_abbrev_size;
1185 while (dbg_abbrev < last) {
1186 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1187 if (tag.get() == nullptr) {
1188 return nullptr;
1189 } else {
1190 abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size()));
1191 abbrev->tag_list_.push_back(std::move(tag));
1192 }
1193 }
1194 return abbrev.release();
1195 }
1196
1197 DebugTag* ReadTag(const byte* entry) {
1198 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1199 auto it = tags_.find(tag_num);
1200 if (it == tags_.end()) {
1201 return nullptr;
1202 } else {
1203 CHECK_GT(tag_list_.size(), it->second);
1204 return tag_list_.at(it->second).get();
1205 }
1206 }
1207
1208 private:
1209 DebugAbbrev() {}
1210 std::map<uint32_t, uint32_t> tags_;
1211 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1212};
1213
1214class DebugInfoIterator {
1215 public:
1216 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1217 DebugAbbrev* abbrev) {
1218 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1219 if (iter->GetCurrentTag() == nullptr) {
1220 return nullptr;
1221 } else {
1222 return iter.release();
1223 }
1224 }
1225 ~DebugInfoIterator() {}
1226
1227 // Moves to the next DIE. Returns false if at last entry.
1228 // TODO Handle variable length attributes.
1229 bool next() {
1230 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1231 return false;
1232 }
1233 current_entry_ += current_tag_->GetSize();
1234 if (current_entry_ >= last_entry_) {
1235 current_entry_ = nullptr;
1236 return false;
1237 }
1238 current_tag_ = abbrev_->ReadTag(current_entry_);
1239 if (current_tag_ == nullptr) {
1240 current_entry_ = nullptr;
1241 return false;
1242 } else {
1243 return true;
1244 }
1245 }
1246
1247 const DebugTag* GetCurrentTag() {
1248 return const_cast<DebugTag*>(current_tag_);
1249 }
1250 byte* GetPointerToField(uint8_t dwarf_field) {
1251 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1252 return nullptr;
1253 }
1254 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1255 if (off == 0) {
1256 // tag does not have that field.
1257 return nullptr;
1258 } else {
1259 DCHECK_LT(off, current_tag_->GetSize());
1260 return current_entry_ + off;
1261 }
1262 }
1263
1264 private:
1265 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
1266 : abbrev_(abbrev),
1267 last_entry_(reinterpret_cast<byte*>(header) + frame_size),
1268 current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)),
1269 current_tag_(abbrev_->ReadTag(current_entry_)) {}
1270 DebugAbbrev* abbrev_;
1271 byte* last_entry_;
1272 byte* current_entry_;
1273 DebugTag* current_tag_;
1274};
1275
1276static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) {
1277 do {
1278 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
1279 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
1280 return false;
1281 }
1282 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
1283 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
1284 if (PC_low != nullptr && PC_high != nullptr) {
1285 *PC_low += text_start;
1286 *PC_high += text_start;
1287 }
1288 } while (iter->next());
1289 return true;
1290}
1291
1292static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size,
1293 uintptr_t text_start,
1294 byte* dbg_info, size_t dbg_info_size,
1295 byte* dbg_frame, size_t dbg_frame_size) {
1296 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size));
1297 if (abbrev.get() == nullptr) {
1298 return false;
1299 }
1300 std::unique_ptr<DebugInfoIterator> iter(
1301 DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info),
1302 dbg_info_size, abbrev.get()));
1303 if (iter.get() == nullptr) {
1304 return false;
1305 }
1306 return FixupDebugInfo(text_start, iter.get())
1307 && FixupDebugFrame(text_start, dbg_frame, dbg_frame_size);
1308}
Mark Mendellae9fd932014-02-10 16:14:35 -08001309
1310void ElfFile::GdbJITSupport() {
1311 // We only get here if we only are mapping the program header.
1312 DCHECK(program_header_only_);
1313
1314 // Well, we need the whole file to do this.
1315 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07001316 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
1317 // sections are there.
1318 std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
1319 MAP_PRIVATE, &error_msg));
1320 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001321 return;
1322 }
Alex Light3470ab42014-06-18 10:35:45 -07001323 ElfFile& all = *all_ptr;
1324
1325 // Do we have interesting sections?
1326 const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info");
1327 const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev");
1328 const Elf32_Shdr* debug_frame = all.FindSectionByName(".debug_frame");
1329 const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str");
1330 const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab");
1331 const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab");
1332 Elf32_Shdr* text_sec = all.FindSectionByName(".text");
1333 if (debug_info == nullptr || debug_abbrev == nullptr || debug_frame == nullptr ||
1334 debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001335 return;
1336 }
Ian Rogers1a570662014-03-12 01:02:21 -07001337#ifdef __LP64__
1338 if (true) {
1339 return; // No ELF debug support in 64bit.
1340 }
1341#endif
Alex Light3470ab42014-06-18 10:35:45 -07001342 // We need to add in a strtab and symtab to the image.
1343 // all is MAP_PRIVATE so it can be written to freely.
1344 // We also already have strtab and symtab so we are fine there.
1345 Elf32_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08001346 elf_hdr.e_entry = 0;
1347 elf_hdr.e_phoff = 0;
1348 elf_hdr.e_phnum = 0;
1349 elf_hdr.e_phentsize = 0;
1350 elf_hdr.e_type = ET_EXEC;
1351
Alex Light3470ab42014-06-18 10:35:45 -07001352 text_sec->sh_type = SHT_NOBITS;
1353 text_sec->sh_offset = 0;
Mark Mendellae9fd932014-02-10 16:14:35 -08001354
Alex Light3470ab42014-06-18 10:35:45 -07001355 if (!FixupDebugSections(
1356 all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr,
1357 all.Begin() + debug_info->sh_offset, debug_info->sh_size,
1358 all.Begin() + debug_frame->sh_offset, debug_frame->sh_size)) {
1359 LOG(ERROR) << "Failed to load GDB data";
1360 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08001361 }
1362
Alex Light3470ab42014-06-18 10:35:45 -07001363 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
1364 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08001365}
1366
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001367} // namespace art