blob: 594c65f6e2458a477059f72302a5dced49dd2d3e [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
Alex Light53cb16b2014-06-12 11:26:29 -0700727Elf32_Dyn* ElfFile::FindDynamicByType(Elf32_Sword type) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000728 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
Alex Light53cb16b2014-06-12 11:26:29 -0700729 Elf32_Dyn* dyn = &GetDynamic(i);
730 if (dyn->d_tag == type) {
731 return dyn;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800732 }
733 }
Alex Light53cb16b2014-06-12 11:26:29 -0700734 return NULL;
735}
736
737Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
738 Elf32_Dyn* dyn = FindDynamicByType(type);
739 if (dyn == NULL) {
740 return 0;
741 } else {
742 return dyn->d_un.d_val;
743 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800744}
745
Brian Carlstromc1409452014-02-26 14:06:23 -0800746Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000747 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
748 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800749}
750
Brian Carlstromc1409452014-02-26 14:06:23 -0800751Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000752 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800753 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
754 return section_header.sh_size / section_header.sh_entsize;
755}
756
Brian Carlstromc1409452014-02-26 14:06:23 -0800757Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000758 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800759 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
760 return *(GetRelSectionStart(section_header) + i);
761}
762
Brian Carlstromc1409452014-02-26 14:06:23 -0800763Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000764 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
765 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800766}
767
Brian Carlstromc1409452014-02-26 14:06:23 -0800768Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000769 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800770 return section_header.sh_size / section_header.sh_entsize;
771}
772
Brian Carlstromc1409452014-02-26 14:06:23 -0800773Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000774 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800775 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
776 return *(GetRelaSectionStart(section_header) + i);
777}
778
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800779// Base on bionic phdr_table_get_load_size
Brian Carlstromc1409452014-02-26 14:06:23 -0800780size_t ElfFile::GetLoadedSize() const {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000781 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
782 Elf32_Addr max_vaddr = 0x00000000u;
783 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
784 Elf32_Phdr& program_header = GetProgramHeader(i);
785 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800786 continue;
787 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000788 Elf32_Addr begin_vaddr = program_header.p_vaddr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800789 if (begin_vaddr < min_vaddr) {
790 min_vaddr = begin_vaddr;
791 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000792 Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800793 if (end_vaddr > max_vaddr) {
794 max_vaddr = end_vaddr;
795 }
796 }
797 min_vaddr = RoundDown(min_vaddr, kPageSize);
798 max_vaddr = RoundUp(max_vaddr, kPageSize);
799 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
800 size_t loaded_size = max_vaddr - min_vaddr;
801 return loaded_size;
802}
803
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700804bool ElfFile::Load(bool executable, std::string* error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800805 CHECK(program_header_only_) << file_->GetPath();
Andreas Gampe91268c12014-04-03 17:50:24 -0700806
807 if (executable) {
808 InstructionSet elf_ISA = kNone;
809 switch (GetHeader().e_machine) {
810 case EM_ARM: {
811 elf_ISA = kArm;
812 break;
813 }
814 case EM_AARCH64: {
815 elf_ISA = kArm64;
816 break;
817 }
818 case EM_386: {
819 elf_ISA = kX86;
820 break;
821 }
822 case EM_X86_64: {
823 elf_ISA = kX86_64;
824 break;
825 }
826 case EM_MIPS: {
827 elf_ISA = kMips;
828 break;
829 }
830 }
831
832 if (elf_ISA != kRuntimeISA) {
833 std::ostringstream oss;
834 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
835 *error_msg = oss.str();
836 return false;
837 }
838 }
839
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000840 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
841 Elf32_Phdr& program_header = GetProgramHeader(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800842
843 // Record .dynamic header information for later use
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000844 if (program_header.p_type == PT_DYNAMIC) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800845 dynamic_program_header_ = &program_header;
846 continue;
847 }
848
849 // Not something to load, move on.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000850 if (program_header.p_type != PT_LOAD) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800851 continue;
852 }
853
854 // Found something to load.
855
856 // If p_vaddr is zero, it must be the first loadable segment,
857 // since they must be in order. Since it is zero, there isn't a
858 // specific address requested, so first request a contiguous chunk
859 // of required size for all segments, but with no
860 // permissions. We'll then carve that up with the proper
861 // permissions as we load the actual segments. If p_vaddr is
862 // non-zero, the segments require the specific address specified,
863 // which either was specified in the file because we already set
864 // base_address_ after the first zero segment).
Ian Rogerscdfcf372014-01-23 20:38:36 -0800865 int64_t temp_file_length = file_->GetLength();
866 if (temp_file_length < 0) {
867 errno = -temp_file_length;
868 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
869 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
870 return false;
871 }
872 size_t file_length = static_cast<size_t>(temp_file_length);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800873 if (program_header.p_vaddr == 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700874 std::string reservation_name("ElfFile reservation for ");
875 reservation_name += file_->GetPath();
Ian Rogers700a4022014-05-19 16:49:03 -0700876 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
Alex Light3470ab42014-06-18 10:35:45 -0700877 nullptr, GetLoadedSize(), PROT_NONE, false,
Brian Carlstromc1409452014-02-26 14:06:23 -0800878 error_msg));
879 if (reserve.get() == nullptr) {
880 *error_msg = StringPrintf("Failed to allocate %s: %s",
881 reservation_name.c_str(), error_msg->c_str());
882 return false;
883 }
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700884 base_address_ = reserve->Begin();
885 segments_.push_back(reserve.release());
886 }
887 // empty segment, nothing to map
888 if (program_header.p_memsz == 0) {
889 continue;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800890 }
891 byte* p_vaddr = base_address_ + program_header.p_vaddr;
892 int prot = 0;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000893 if (executable && ((program_header.p_flags & PF_X) != 0)) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700894 prot |= PROT_EXEC;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800895 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000896 if ((program_header.p_flags & PF_W) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700897 prot |= PROT_WRITE;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800898 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000899 if ((program_header.p_flags & PF_R) != 0) {
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -0700900 prot |= PROT_READ;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800901 }
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700902 int flags = 0;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800903 if (writable_) {
904 prot |= PROT_WRITE;
905 flags |= MAP_SHARED;
906 } else {
907 flags |= MAP_PRIVATE;
908 }
Brian Carlstrom3a223612013-10-10 17:18:24 -0700909 if (file_length < (program_header.p_offset + program_header.p_memsz)) {
Ian Rogerscdfcf372014-01-23 20:38:36 -0800910 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700911 "%d of %d bytes: '%s'", file_length, i,
912 program_header.p_offset + program_header.p_memsz,
913 file_->GetPath().c_str());
Brian Carlstrom3a223612013-10-10 17:18:24 -0700914 return false;
915 }
Ian Rogers700a4022014-05-19 16:49:03 -0700916 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800917 program_header.p_memsz,
918 prot, flags, file_->Fd(),
919 program_header.p_offset,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700920 true, // implies MAP_FIXED
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700921 file_->GetPath().c_str(),
922 error_msg));
Brian Carlstromc1409452014-02-26 14:06:23 -0800923 if (segment.get() == nullptr) {
924 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
925 i, file_->GetPath().c_str(), error_msg->c_str());
926 return false;
927 }
928 if (segment->Begin() != p_vaddr) {
929 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
930 "instead mapped to %p",
931 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
932 return false;
933 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800934 segments_.push_back(segment.release());
935 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800936
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800937 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
938 dynamic_section_start_
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000939 = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr);
940 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
941 Elf32_Dyn& elf_dyn = GetDynamic(i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800942 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
943 switch (elf_dyn.d_tag) {
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000944 case DT_HASH: {
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 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000950 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800951 break;
952 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000953 case DT_STRTAB: {
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 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800959 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
960 break;
961 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000962 case DT_SYMTAB: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800963 if (!ValidPointer(d_ptr)) {
964 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
965 d_ptr, file_->GetPath().c_str());
966 return false;
967 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000968 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800969 break;
970 }
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000971 case DT_NULL: {
Brian Carlstromc1409452014-02-26 14:06:23 -0800972 if (GetDynamicNum() != i+1) {
973 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
974 "expected %d as implied by size of PT_DYNAMIC segment in %s",
975 i + 1, GetDynamicNum(), file_->GetPath().c_str());
976 return false;
977 }
Brian Carlstrom265091e2013-01-30 14:08:26 -0800978 break;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800979 }
980 }
981 }
982
Mark Mendellae9fd932014-02-10 16:14:35 -0800983 // Use GDB JIT support to do stack backtrace, etc.
984 if (executable) {
985 GdbJITSupport();
986 }
987
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800988 return true;
989}
990
Brian Carlstromc1409452014-02-26 14:06:23 -0800991bool ElfFile::ValidPointer(const byte* start) const {
992 for (size_t i = 0; i < segments_.size(); ++i) {
993 const MemMap* segment = segments_[i];
994 if (segment->Begin() <= start && start < segment->End()) {
995 return true;
996 }
997 }
998 return false;
999}
1000
Alex Light3470ab42014-06-18 10:35:45 -07001001
1002Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const {
1003 CHECK(!program_header_only_);
1004 Elf32_Shdr& shstrtab_sec = GetSectionNameStringSection();
1005 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
1006 Elf32_Shdr& shdr = GetSectionHeader(i);
1007 const char* sec_name = GetString(shstrtab_sec, shdr.sh_name);
1008 if (sec_name == nullptr) {
1009 continue;
1010 }
1011 if (name == sec_name) {
1012 return &shdr;
1013 }
1014 }
1015 return nullptr;
Mark Mendellae9fd932014-02-10 16:14:35 -08001016}
1017
Alex Light3470ab42014-06-18 10:35:45 -07001018struct PACKED(1) FDE {
1019 uint32_t raw_length_;
1020 uint32_t GetLength() {
1021 return raw_length_ + sizeof(raw_length_);
1022 }
1023 uint32_t CIE_pointer;
1024 uint32_t initial_location;
1025 uint32_t address_range;
1026 uint8_t instructions[0];
1027};
1028
1029static FDE* NextFDE(FDE* frame) {
1030 byte* fde_bytes = reinterpret_cast<byte*>(frame);
1031 fde_bytes += frame->GetLength();
1032 return reinterpret_cast<FDE*>(fde_bytes);
Mark Mendellae9fd932014-02-10 16:14:35 -08001033}
1034
Alex Light3470ab42014-06-18 10:35:45 -07001035static bool IsFDE(FDE* frame) {
Tong Shen35e1e6a2014-07-30 09:31:22 -07001036 return frame->CIE_pointer != 0;
Alex Light3470ab42014-06-18 10:35:45 -07001037}
1038
1039// TODO This only works for 32-bit Elf Files.
Tong Shen35e1e6a2014-07-30 09:31:22 -07001040static bool FixupEHFrame(uintptr_t text_start, byte* eh_frame, size_t eh_frame_size) {
1041 FDE* last_frame = reinterpret_cast<FDE*>(eh_frame + eh_frame_size);
1042 FDE* frame = NextFDE(reinterpret_cast<FDE*>(eh_frame));
Alex Light3470ab42014-06-18 10:35:45 -07001043 for (; frame < last_frame; frame = NextFDE(frame)) {
1044 if (!IsFDE(frame)) {
1045 return false;
1046 }
1047 frame->initial_location += text_start;
1048 }
1049 return true;
1050}
1051
1052struct PACKED(1) DebugInfoHeader {
1053 uint32_t unit_length; // TODO 32-bit specific size
1054 uint16_t version;
1055 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1056 uint8_t address_size;
1057};
1058
1059// Returns -1 if it is variable length, which we will just disallow for now.
1060static int32_t FormLength(uint32_t att) {
1061 switch (att) {
1062 case DW_FORM_data1:
1063 case DW_FORM_flag:
1064 case DW_FORM_flag_present:
1065 case DW_FORM_ref1:
1066 return 1;
1067
1068 case DW_FORM_data2:
1069 case DW_FORM_ref2:
1070 return 2;
1071
1072 case DW_FORM_addr: // TODO 32-bit only
1073 case DW_FORM_ref_addr: // TODO 32-bit only
1074 case DW_FORM_sec_offset: // TODO 32-bit only
1075 case DW_FORM_strp: // TODO 32-bit only
1076 case DW_FORM_data4:
1077 case DW_FORM_ref4:
1078 return 4;
1079
1080 case DW_FORM_data8:
1081 case DW_FORM_ref8:
1082 case DW_FORM_ref_sig8:
1083 return 8;
1084
1085 case DW_FORM_block:
1086 case DW_FORM_block1:
1087 case DW_FORM_block2:
1088 case DW_FORM_block4:
1089 case DW_FORM_exprloc:
1090 case DW_FORM_indirect:
1091 case DW_FORM_ref_udata:
1092 case DW_FORM_sdata:
1093 case DW_FORM_string:
1094 case DW_FORM_udata:
1095 default:
1096 return -1;
Mark Mendellae9fd932014-02-10 16:14:35 -08001097 }
1098}
1099
Alex Light3470ab42014-06-18 10:35:45 -07001100class DebugTag {
1101 public:
1102 const uint32_t index_;
1103 ~DebugTag() {}
1104 // Creates a new tag and moves data pointer up to the start of the next one.
1105 // nullptr means error.
1106 static DebugTag* Create(const byte** data_pointer) {
1107 const byte* data = *data_pointer;
1108 uint32_t index = DecodeUnsignedLeb128(&data);
1109 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1110 tag->size_ = static_cast<uint32_t>(
1111 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1112 // skip the abbrev
1113 tag->tag_ = DecodeUnsignedLeb128(&data);
1114 tag->has_child_ = (*data == 0);
1115 data++;
1116 while (true) {
1117 uint32_t attr = DecodeUnsignedLeb128(&data);
1118 uint32_t form = DecodeUnsignedLeb128(&data);
1119 if (attr == 0 && form == 0) {
1120 break;
1121 } else if (attr == 0 || form == 0) {
1122 // Bad abbrev.
1123 return nullptr;
1124 }
1125 int32_t size = FormLength(form);
1126 if (size == -1) {
1127 return nullptr;
1128 }
1129 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1130 }
1131 *data_pointer = data;
1132 return tag.release();
1133 }
1134
1135 uint32_t GetSize() const {
1136 return size_;
1137 }
1138
1139 bool HasChild() {
1140 return has_child_;
1141 }
1142
1143 uint32_t GetTagNumber() {
1144 return tag_;
1145 }
1146
1147 // Gets the offset of a particular attribute in this tag structure.
1148 // Interpretation of the data is left to the consumer. 0 is returned if the
1149 // tag does not contain the attribute.
1150 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1151 auto it = off_map_.find(dwarf_attribute);
1152 if (it == off_map_.end()) {
1153 return 0;
1154 } else {
1155 return it->second;
1156 }
1157 }
1158
1159 // Gets the size of attribute
1160 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1161 auto it = size_map_.find(dwarf_attribute);
1162 if (it == size_map_.end()) {
1163 return 0;
1164 } else {
1165 return it->second;
1166 }
1167 }
1168
1169 private:
1170 explicit DebugTag(uint32_t index) : index_(index) {}
1171 void AddAttribute(uint32_t type, uint32_t attr_size) {
1172 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1173 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1174 size_ += attr_size;
1175 }
1176 std::map<uint32_t, uint32_t> off_map_;
1177 std::map<uint32_t, uint32_t> size_map_;
1178 uint32_t size_;
1179 uint32_t tag_;
1180 bool has_child_;
1181};
1182
1183class DebugAbbrev {
1184 public:
1185 ~DebugAbbrev() {}
1186 static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) {
1187 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev);
1188 const byte* last = dbg_abbrev + dbg_abbrev_size;
1189 while (dbg_abbrev < last) {
1190 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1191 if (tag.get() == nullptr) {
1192 return nullptr;
1193 } else {
1194 abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size()));
1195 abbrev->tag_list_.push_back(std::move(tag));
1196 }
1197 }
1198 return abbrev.release();
1199 }
1200
1201 DebugTag* ReadTag(const byte* entry) {
1202 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1203 auto it = tags_.find(tag_num);
1204 if (it == tags_.end()) {
1205 return nullptr;
1206 } else {
1207 CHECK_GT(tag_list_.size(), it->second);
1208 return tag_list_.at(it->second).get();
1209 }
1210 }
1211
1212 private:
1213 DebugAbbrev() {}
1214 std::map<uint32_t, uint32_t> tags_;
1215 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1216};
1217
1218class DebugInfoIterator {
1219 public:
1220 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1221 DebugAbbrev* abbrev) {
1222 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1223 if (iter->GetCurrentTag() == nullptr) {
1224 return nullptr;
1225 } else {
1226 return iter.release();
1227 }
1228 }
1229 ~DebugInfoIterator() {}
1230
1231 // Moves to the next DIE. Returns false if at last entry.
1232 // TODO Handle variable length attributes.
1233 bool next() {
1234 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1235 return false;
1236 }
1237 current_entry_ += current_tag_->GetSize();
1238 if (current_entry_ >= last_entry_) {
1239 current_entry_ = nullptr;
1240 return false;
1241 }
1242 current_tag_ = abbrev_->ReadTag(current_entry_);
1243 if (current_tag_ == nullptr) {
1244 current_entry_ = nullptr;
1245 return false;
1246 } else {
1247 return true;
1248 }
1249 }
1250
1251 const DebugTag* GetCurrentTag() {
1252 return const_cast<DebugTag*>(current_tag_);
1253 }
1254 byte* GetPointerToField(uint8_t dwarf_field) {
1255 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1256 return nullptr;
1257 }
1258 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1259 if (off == 0) {
1260 // tag does not have that field.
1261 return nullptr;
1262 } else {
1263 DCHECK_LT(off, current_tag_->GetSize());
1264 return current_entry_ + off;
1265 }
1266 }
1267
1268 private:
1269 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
1270 : abbrev_(abbrev),
1271 last_entry_(reinterpret_cast<byte*>(header) + frame_size),
1272 current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)),
1273 current_tag_(abbrev_->ReadTag(current_entry_)) {}
1274 DebugAbbrev* abbrev_;
1275 byte* last_entry_;
1276 byte* current_entry_;
1277 DebugTag* current_tag_;
1278};
1279
1280static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) {
1281 do {
1282 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
1283 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
1284 return false;
1285 }
1286 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
1287 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
1288 if (PC_low != nullptr && PC_high != nullptr) {
1289 *PC_low += text_start;
1290 *PC_high += text_start;
1291 }
1292 } while (iter->next());
1293 return true;
1294}
1295
1296static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size,
1297 uintptr_t text_start,
1298 byte* dbg_info, size_t dbg_info_size,
Tong Shen35e1e6a2014-07-30 09:31:22 -07001299 byte* eh_frame, size_t eh_frame_size) {
Alex Light3470ab42014-06-18 10:35:45 -07001300 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size));
1301 if (abbrev.get() == nullptr) {
1302 return false;
1303 }
1304 std::unique_ptr<DebugInfoIterator> iter(
1305 DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info),
1306 dbg_info_size, abbrev.get()));
1307 if (iter.get() == nullptr) {
1308 return false;
1309 }
1310 return FixupDebugInfo(text_start, iter.get())
Tong Shen35e1e6a2014-07-30 09:31:22 -07001311 && FixupEHFrame(text_start, eh_frame, eh_frame_size);
Alex Light3470ab42014-06-18 10:35:45 -07001312}
Mark Mendellae9fd932014-02-10 16:14:35 -08001313
1314void ElfFile::GdbJITSupport() {
1315 // We only get here if we only are mapping the program header.
1316 DCHECK(program_header_only_);
1317
1318 // Well, we need the whole file to do this.
1319 std::string error_msg;
Alex Light3470ab42014-06-18 10:35:45 -07001320 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
1321 // sections are there.
1322 std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
1323 MAP_PRIVATE, &error_msg));
1324 if (all_ptr.get() == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001325 return;
1326 }
Alex Light3470ab42014-06-18 10:35:45 -07001327 ElfFile& all = *all_ptr;
1328
1329 // Do we have interesting sections?
1330 const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info");
1331 const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev");
Tong Shen35e1e6a2014-07-30 09:31:22 -07001332 const Elf32_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
Alex Light3470ab42014-06-18 10:35:45 -07001333 const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str");
1334 const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab");
1335 const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab");
1336 Elf32_Shdr* text_sec = all.FindSectionByName(".text");
Tong Shen35e1e6a2014-07-30 09:31:22 -07001337 if (debug_info == nullptr || debug_abbrev == nullptr || eh_frame == nullptr ||
Alex Light3470ab42014-06-18 10:35:45 -07001338 debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) {
Mark Mendellae9fd932014-02-10 16:14:35 -08001339 return;
1340 }
Alex Light3470ab42014-06-18 10:35:45 -07001341 // We need to add in a strtab and symtab to the image.
1342 // all is MAP_PRIVATE so it can be written to freely.
1343 // We also already have strtab and symtab so we are fine there.
1344 Elf32_Ehdr& elf_hdr = all.GetHeader();
Mark Mendellae9fd932014-02-10 16:14:35 -08001345 elf_hdr.e_entry = 0;
1346 elf_hdr.e_phoff = 0;
1347 elf_hdr.e_phnum = 0;
1348 elf_hdr.e_phentsize = 0;
1349 elf_hdr.e_type = ET_EXEC;
1350
Alex Light3470ab42014-06-18 10:35:45 -07001351 text_sec->sh_type = SHT_NOBITS;
1352 text_sec->sh_offset = 0;
Mark Mendellae9fd932014-02-10 16:14:35 -08001353
Alex Light3470ab42014-06-18 10:35:45 -07001354 if (!FixupDebugSections(
1355 all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr,
1356 all.Begin() + debug_info->sh_offset, debug_info->sh_size,
Tong Shen35e1e6a2014-07-30 09:31:22 -07001357 all.Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
Alex Light3470ab42014-06-18 10:35:45 -07001358 LOG(ERROR) << "Failed to load GDB data";
1359 return;
Mark Mendellae9fd932014-02-10 16:14:35 -08001360 }
1361
Alex Light3470ab42014-06-18 10:35:45 -07001362 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
1363 gdb_file_mapping_.reset(all_ptr.release());
Mark Mendellae9fd932014-02-10 16:14:35 -08001364}
1365
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001366} // namespace art