1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "elf_file.h"
18
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include "base/logging.h"
23 #include "base/stringprintf.h"
24 #include "base/stl_util.h"
25 #include "dwarf.h"
26 #include "leb128.h"
27 #include "utils.h"
28 #include "instruction_set.h"
29
30 namespace art {
31
32 // -------------------------------------------------------------------
33 // Binary GDB JIT Interface as described in
34 // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
35 extern "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.
__jit_debug_register_code()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
CreateCodeEntry(const byte * symfile_addr,uintptr_t symfile_size)70 static 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
UnregisterCodeEntry(JITCodeEntry * entry)91 static 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
ElfFile(File * file,bool writable,bool program_header_only)109 ElfFile::ElfFile(File* file, bool writable, bool program_header_only)
110 : file_(file),
111 writable_(writable),
112 program_header_only_(program_header_only),
113 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);
129 }
130
Open(File * file,bool writable,bool program_header_only,std::string * error_msg)131 ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only,
132 std::string* error_msg) {
133 std::unique_ptr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only));
134 int prot;
135 int flags;
136 if (writable) {
137 prot = PROT_READ | PROT_WRITE;
138 flags = MAP_SHARED;
139 } else {
140 prot = PROT_READ;
141 flags = MAP_PRIVATE;
142 }
143 if (!elf_file->Setup(prot, flags, error_msg)) {
144 return nullptr;
145 }
146 return elf_file.release();
147 }
148
Open(File * file,int prot,int flags,std::string * error_msg)149 ElfFile* 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
Setup(int prot,int flags,std::string * error_msg)157 bool ElfFile::Setup(int prot, int flags, std::string* error_msg) {
158 int64_t temp_file_length = file_->GetLength();
159 if (temp_file_length < 0) {
160 errno = -temp_file_length;
161 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
162 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
163 return false;
164 }
165 size_t file_length = static_cast<size_t>(temp_file_length);
166 if (file_length < sizeof(Elf32_Ehdr)) {
167 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
168 "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr),
169 file_->GetPath().c_str());
170 return false;
171 }
172
173 if (program_header_only_) {
174 // first just map ELF header to get program header size information
175 size_t elf_header_size = sizeof(Elf32_Ehdr);
176 if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0,
177 file_->GetPath().c_str(), error_msg),
178 error_msg)) {
179 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);
183 if (file_length < program_header_size) {
184 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
185 "header of %zd bytes: '%s'", file_length,
186 sizeof(Elf32_Ehdr), file_->GetPath().c_str());
187 return false;
188 }
189 if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0,
190 file_->GetPath().c_str(), error_msg),
191 error_msg)) {
192 *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
193 return false;
194 }
195 } else {
196 // otherwise map entire file
197 if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0,
198 file_->GetPath().c_str(), error_msg),
199 error_msg)) {
200 *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
201 return false;
202 }
203 }
204
205 if (program_header_only_) {
206 program_headers_start_ = Begin() + GetHeader().e_phoff;
207 } else {
208 if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
209 return false;
210 }
211
212 // Setup section headers.
213 if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) {
214 return false;
215 }
216
217 // Find shstrtab.
218 Elf32_Shdr* shstrtab_section_header = GetSectionNameStringSection();
219 if (shstrtab_section_header == nullptr) {
220 *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
221 file_->GetPath().c_str());
222 return false;
223 }
224
225 // Find .dynamic section info from program header
226 dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
227 if (dynamic_program_header_ == nullptr) {
228 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
229 file_->GetPath().c_str());
230 return false;
231 }
232
233 if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
234 reinterpret_cast<byte**>(&dynamic_section_start_), error_msg)) {
235 return false;
236 }
237
238 // Find other sections from section headers
239 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
240 Elf32_Shdr* section_header = GetSectionHeader(i);
241 if (section_header == nullptr) {
242 *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
243 i, file_->GetPath().c_str());
244 return false;
245 }
246 switch (section_header->sh_type) {
247 case SHT_SYMTAB: {
248 if (!CheckAndSet(section_header->sh_offset, "symtab",
249 reinterpret_cast<byte**>(&symtab_section_start_), error_msg)) {
250 return false;
251 }
252 break;
253 }
254 case SHT_DYNSYM: {
255 if (!CheckAndSet(section_header->sh_offset, "dynsym",
256 reinterpret_cast<byte**>(&dynsym_section_start_), error_msg)) {
257 return false;
258 }
259 break;
260 }
261 case SHT_STRTAB: {
262 // TODO: base these off of sh_link from .symtab and .dynsym above
263 if ((section_header->sh_flags & SHF_ALLOC) != 0) {
264 // Check that this is named ".dynstr" and ignore otherwise.
265 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
266 if (strncmp(".dynstr", header_name, 8) == 0) {
267 if (!CheckAndSet(section_header->sh_offset, "dynstr",
268 reinterpret_cast<byte**>(&dynstr_section_start_), error_msg)) {
269 return false;
270 }
271 }
272 } else {
273 // Check that this is named ".strtab" and ignore otherwise.
274 const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
275 if (strncmp(".strtab", header_name, 8) == 0) {
276 if (!CheckAndSet(section_header->sh_offset, "strtab",
277 reinterpret_cast<byte**>(&strtab_section_start_), error_msg)) {
278 return false;
279 }
280 }
281 }
282 break;
283 }
284 case SHT_DYNAMIC: {
285 if (reinterpret_cast<byte*>(dynamic_section_start_) !=
286 Begin() + section_header->sh_offset) {
287 LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
288 << file_->GetPath() << ": " << std::hex
289 << reinterpret_cast<void*>(dynamic_section_start_)
290 << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
291 return false;
292 }
293 break;
294 }
295 case SHT_HASH: {
296 if (!CheckAndSet(section_header->sh_offset, "hash section",
297 reinterpret_cast<byte**>(&hash_section_start_), error_msg)) {
298 return false;
299 }
300 break;
301 }
302 }
303 }
304
305 // Check for the existence of some sections.
306 if (!CheckSectionsExist(error_msg)) {
307 return false;
308 }
309 }
310
311 return true;
312 }
313
~ElfFile()314 ElfFile::~ElfFile() {
315 STLDeleteElements(&segments_);
316 delete symtab_symbol_table_;
317 delete dynsym_symbol_table_;
318 delete jit_elf_image_;
319 if (jit_gdb_entry_) {
320 UnregisterCodeEntry(jit_gdb_entry_);
321 }
322 }
323
CheckAndSet(Elf32_Off offset,const char * label,byte ** target,std::string * error_msg)324 bool ElfFile::CheckAndSet(Elf32_Off offset, const char* label,
325 byte** target, std::string* error_msg) {
326 if (Begin() + offset >= End()) {
327 *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
328 file_->GetPath().c_str());
329 return false;
330 }
331 *target = Begin() + offset;
332 return true;
333 }
334
CheckSectionsLinked(const byte * source,const byte * target) const335 bool ElfFile::CheckSectionsLinked(const byte* source, const byte* target) const {
336 // Only works in whole-program mode, as we need to iterate over the sections.
337 // Note that we normally can't search by type, as duplicates are allowed for most section types.
338 if (program_header_only_) {
339 return true;
340 }
341
342 Elf32_Shdr* source_section = nullptr;
343 Elf32_Word target_index = 0;
344 bool target_found = false;
345 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
346 Elf32_Shdr* section_header = GetSectionHeader(i);
347
348 if (Begin() + section_header->sh_offset == source) {
349 // Found the source.
350 source_section = section_header;
351 if (target_index) {
352 break;
353 }
354 } else if (Begin() + section_header->sh_offset == target) {
355 target_index = i;
356 target_found = true;
357 if (source_section != nullptr) {
358 break;
359 }
360 }
361 }
362
363 return target_found && source_section != nullptr && source_section->sh_link == target_index;
364 }
365
CheckSectionsExist(std::string * error_msg) const366 bool ElfFile::CheckSectionsExist(std::string* error_msg) const {
367 if (!program_header_only_) {
368 // If in full mode, need section headers.
369 if (section_headers_start_ == nullptr) {
370 *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str());
371 return false;
372 }
373 }
374
375 // This is redundant, but defensive.
376 if (dynamic_program_header_ == nullptr) {
377 *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
378 file_->GetPath().c_str());
379 return false;
380 }
381
382 // Need a dynamic section. This is redundant, but defensive.
383 if (dynamic_section_start_ == nullptr) {
384 *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
385 file_->GetPath().c_str());
386 return false;
387 }
388
389 // Symtab validation. These is not really a hard failure, as we are currently not using the
390 // symtab internally, but it's nice to be defensive.
391 if (symtab_section_start_ != nullptr) {
392 // When there's a symtab, there should be a strtab.
393 if (strtab_section_start_ == nullptr) {
394 *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str());
395 return false;
396 }
397
398 // The symtab should link to the strtab.
399 if (!CheckSectionsLinked(reinterpret_cast<const byte*>(symtab_section_start_),
400 reinterpret_cast<const byte*>(strtab_section_start_))) {
401 *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
402 file_->GetPath().c_str());
403 return false;
404 }
405 }
406
407 // We always need a dynstr & dynsym.
408 if (dynstr_section_start_ == nullptr) {
409 *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str());
410 return false;
411 }
412 if (dynsym_section_start_ == nullptr) {
413 *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str());
414 return false;
415 }
416
417 // Need a hash section for dynamic symbol lookup.
418 if (hash_section_start_ == nullptr) {
419 *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
420 file_->GetPath().c_str());
421 return false;
422 }
423
424 // And the hash section should be linking to the dynsym.
425 if (!CheckSectionsLinked(reinterpret_cast<const byte*>(hash_section_start_),
426 reinterpret_cast<const byte*>(dynsym_section_start_))) {
427 *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
428 file_->GetPath().c_str());
429 return false;
430 }
431
432 return true;
433 }
434
SetMap(MemMap * map,std::string * error_msg)435 bool ElfFile::SetMap(MemMap* map, std::string* error_msg) {
436 if (map == nullptr) {
437 // MemMap::Open should have already set an error.
438 DCHECK(!error_msg->empty());
439 return false;
440 }
441 map_.reset(map);
442 CHECK(map_.get() != nullptr) << file_->GetPath();
443 CHECK(map_->Begin() != nullptr) << file_->GetPath();
444
445 header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin());
446 if ((ELFMAG0 != header_->e_ident[EI_MAG0])
447 || (ELFMAG1 != header_->e_ident[EI_MAG1])
448 || (ELFMAG2 != header_->e_ident[EI_MAG2])
449 || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
450 *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
451 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
452 file_->GetPath().c_str(),
453 header_->e_ident[EI_MAG0],
454 header_->e_ident[EI_MAG1],
455 header_->e_ident[EI_MAG2],
456 header_->e_ident[EI_MAG3]);
457 return false;
458 }
459 if (ELFCLASS32 != header_->e_ident[EI_CLASS]) {
460 *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
461 ELFCLASS32,
462 file_->GetPath().c_str(),
463 header_->e_ident[EI_CLASS]);
464 return false;
465 }
466 if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
467 *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
468 ELFDATA2LSB,
469 file_->GetPath().c_str(),
470 header_->e_ident[EI_CLASS]);
471 return false;
472 }
473 if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
474 *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
475 EV_CURRENT,
476 file_->GetPath().c_str(),
477 header_->e_ident[EI_CLASS]);
478 return false;
479 }
480 if (ET_DYN != header_->e_type) {
481 *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
482 ET_DYN,
483 file_->GetPath().c_str(),
484 header_->e_type);
485 return false;
486 }
487 if (EV_CURRENT != header_->e_version) {
488 *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
489 EV_CURRENT,
490 file_->GetPath().c_str(),
491 header_->e_version);
492 return false;
493 }
494 if (0 != header_->e_entry) {
495 *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
496 0,
497 file_->GetPath().c_str(),
498 header_->e_entry);
499 return false;
500 }
501 if (0 == header_->e_phoff) {
502 *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
503 file_->GetPath().c_str());
504 return false;
505 }
506 if (0 == header_->e_shoff) {
507 *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
508 file_->GetPath().c_str());
509 return false;
510 }
511 if (0 == header_->e_ehsize) {
512 *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
513 file_->GetPath().c_str());
514 return false;
515 }
516 if (0 == header_->e_phentsize) {
517 *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
518 file_->GetPath().c_str());
519 return false;
520 }
521 if (0 == header_->e_phnum) {
522 *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
523 file_->GetPath().c_str());
524 return false;
525 }
526 if (0 == header_->e_shentsize) {
527 *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
528 file_->GetPath().c_str());
529 return false;
530 }
531 if (0 == header_->e_shnum) {
532 *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
533 file_->GetPath().c_str());
534 return false;
535 }
536 if (0 == header_->e_shstrndx) {
537 *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
538 file_->GetPath().c_str());
539 return false;
540 }
541 if (header_->e_shstrndx >= header_->e_shnum) {
542 *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
543 header_->e_shstrndx,
544 header_->e_shnum,
545 file_->GetPath().c_str());
546 return false;
547 }
548
549 if (!program_header_only_) {
550 if (header_->e_phoff >= Size()) {
551 *error_msg = StringPrintf("Failed to find e_phoff value %d less than %zd in %s",
552 header_->e_phoff,
553 Size(),
554 file_->GetPath().c_str());
555 return false;
556 }
557 if (header_->e_shoff >= Size()) {
558 *error_msg = StringPrintf("Failed to find e_shoff value %d less than %zd in %s",
559 header_->e_shoff,
560 Size(),
561 file_->GetPath().c_str());
562 return false;
563 }
564 }
565 return true;
566 }
567
568
GetHeader() const569 Elf32_Ehdr& ElfFile::GetHeader() const {
570 CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check.
571 return *header_;
572 }
573
GetProgramHeadersStart() const574 byte* ElfFile::GetProgramHeadersStart() const {
575 CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity
576 // check.
577 return program_headers_start_;
578 }
579
GetSectionHeadersStart() const580 byte* ElfFile::GetSectionHeadersStart() const {
581 CHECK(!program_header_only_); // Only used in "full" mode.
582 CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
583 return section_headers_start_;
584 }
585
GetDynamicProgramHeader() const586 Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const {
587 CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
588 return *dynamic_program_header_;
589 }
590
GetDynamicSectionStart() const591 Elf32_Dyn* ElfFile::GetDynamicSectionStart() const {
592 CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check.
593 return dynamic_section_start_;
594 }
595
IsSymbolSectionType(Elf32_Word section_type)596 static bool IsSymbolSectionType(Elf32_Word section_type) {
597 return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
598 }
599
GetSymbolSectionStart(Elf32_Word section_type) const600 Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const {
601 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
602 switch (section_type) {
603 case SHT_SYMTAB: {
604 return symtab_section_start_;
605 break;
606 }
607 case SHT_DYNSYM: {
608 return dynsym_section_start_;
609 break;
610 }
611 default: {
612 LOG(FATAL) << section_type;
613 return nullptr;
614 }
615 }
616 }
617
GetStringSectionStart(Elf32_Word section_type) const618 const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const {
619 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
620 switch (section_type) {
621 case SHT_SYMTAB: {
622 return strtab_section_start_;
623 }
624 case SHT_DYNSYM: {
625 return dynstr_section_start_;
626 }
627 default: {
628 LOG(FATAL) << section_type;
629 return nullptr;
630 }
631 }
632 }
633
GetString(Elf32_Word section_type,Elf32_Word i) const634 const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const {
635 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
636 if (i == 0) {
637 return nullptr;
638 }
639 const char* string_section_start = GetStringSectionStart(section_type);
640 if (string_section_start == nullptr) {
641 return nullptr;
642 }
643 return string_section_start + i;
644 }
645
646 // WARNING: The following methods do not check for an error condition (non-existent hash section).
647 // It is the caller's job to do this.
648
GetHashSectionStart() const649 Elf32_Word* ElfFile::GetHashSectionStart() const {
650 return hash_section_start_;
651 }
652
GetHashBucketNum() const653 Elf32_Word ElfFile::GetHashBucketNum() const {
654 return GetHashSectionStart()[0];
655 }
656
GetHashChainNum() const657 Elf32_Word ElfFile::GetHashChainNum() const {
658 return GetHashSectionStart()[1];
659 }
660
GetHashBucket(size_t i,bool * ok) const661 Elf32_Word ElfFile::GetHashBucket(size_t i, bool* ok) const {
662 if (i >= GetHashBucketNum()) {
663 *ok = false;
664 return 0;
665 }
666 *ok = true;
667 // 0 is nbucket, 1 is nchain
668 return GetHashSectionStart()[2 + i];
669 }
670
GetHashChain(size_t i,bool * ok) const671 Elf32_Word ElfFile::GetHashChain(size_t i, bool* ok) const {
672 if (i >= GetHashBucketNum()) {
673 *ok = false;
674 return 0;
675 }
676 *ok = true;
677 // 0 is nbucket, 1 is nchain, & chains are after buckets
678 return GetHashSectionStart()[2 + GetHashBucketNum() + i];
679 }
680
GetProgramHeaderNum() const681 Elf32_Word ElfFile::GetProgramHeaderNum() const {
682 return GetHeader().e_phnum;
683 }
684
GetProgramHeader(Elf32_Word i) const685 Elf32_Phdr* ElfFile::GetProgramHeader(Elf32_Word i) const {
686 CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller.
687 byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
688 if (program_header >= End()) {
689 return nullptr; // Failure condition.
690 }
691 return reinterpret_cast<Elf32_Phdr*>(program_header);
692 }
693
FindProgamHeaderByType(Elf32_Word type) const694 Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const {
695 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
696 Elf32_Phdr* program_header = GetProgramHeader(i);
697 if (program_header->p_type == type) {
698 return program_header;
699 }
700 }
701 return nullptr;
702 }
703
GetSectionHeaderNum() const704 Elf32_Word ElfFile::GetSectionHeaderNum() const {
705 return GetHeader().e_shnum;
706 }
707
GetSectionHeader(Elf32_Word i) const708 Elf32_Shdr* ElfFile::GetSectionHeader(Elf32_Word i) const {
709 // Can only access arbitrary sections when we have the whole file, not just program header.
710 // Even if we Load(), it doesn't bring in all the sections.
711 CHECK(!program_header_only_) << file_->GetPath();
712 if (i >= GetSectionHeaderNum()) {
713 return nullptr; // Failure condition.
714 }
715 byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
716 if (section_header >= End()) {
717 return nullptr; // Failure condition.
718 }
719 return reinterpret_cast<Elf32_Shdr*>(section_header);
720 }
721
FindSectionByType(Elf32_Word type) const722 Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const {
723 // Can only access arbitrary sections when we have the whole file, not just program header.
724 // We could change this to switch on known types if they were detected during loading.
725 CHECK(!program_header_only_) << file_->GetPath();
726 for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) {
727 Elf32_Shdr* section_header = GetSectionHeader(i);
728 if (section_header->sh_type == type) {
729 return section_header;
730 }
731 }
732 return nullptr;
733 }
734
735 // from bionic
elfhash(const char * _name)736 static unsigned elfhash(const char *_name) {
737 const unsigned char *name = (const unsigned char *) _name;
738 unsigned h = 0, g;
739
740 while (*name) {
741 h = (h << 4) + *name++;
742 g = h & 0xf0000000;
743 h ^= g;
744 h ^= g >> 24;
745 }
746 return h;
747 }
748
GetSectionNameStringSection() const749 Elf32_Shdr* ElfFile::GetSectionNameStringSection() const {
750 return GetSectionHeader(GetHeader().e_shstrndx);
751 }
752
FindDynamicSymbolAddress(const std::string & symbol_name) const753 const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
754 // Check that we have a hash section.
755 if (GetHashSectionStart() == nullptr) {
756 return nullptr; // Failure condition.
757 }
758 const Elf32_Sym* sym = FindDynamicSymbol(symbol_name);
759 if (sym != nullptr) {
760 return base_address_ + sym->st_value;
761 } else {
762 return nullptr;
763 }
764 }
765
766 // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
FindDynamicSymbol(const std::string & symbol_name) const767 const Elf32_Sym* ElfFile::FindDynamicSymbol(const std::string& symbol_name) const {
768 if (GetHashBucketNum() == 0) {
769 // No dynamic symbols at all.
770 return nullptr;
771 }
772 Elf32_Word hash = elfhash(symbol_name.c_str());
773 Elf32_Word bucket_index = hash % GetHashBucketNum();
774 bool ok;
775 Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
776 if (!ok) {
777 return nullptr;
778 }
779 while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
780 Elf32_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
781 if (symbol == nullptr) {
782 return nullptr; // Failure condition.
783 }
784 const char* name = GetString(SHT_DYNSYM, symbol->st_name);
785 if (symbol_name == name) {
786 return symbol;
787 }
788 symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
789 if (!ok) {
790 return nullptr;
791 }
792 }
793 return nullptr;
794 }
795
GetSymbolNum(Elf32_Shdr & section_header) const796 Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const {
797 CHECK(IsSymbolSectionType(section_header.sh_type))
798 << file_->GetPath() << " " << section_header.sh_type;
799 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
800 return section_header.sh_size / section_header.sh_entsize;
801 }
802
GetSymbol(Elf32_Word section_type,Elf32_Word i) const803 Elf32_Sym* ElfFile::GetSymbol(Elf32_Word section_type,
804 Elf32_Word i) const {
805 Elf32_Sym* sym_start = GetSymbolSectionStart(section_type);
806 if (sym_start == nullptr) {
807 return nullptr;
808 }
809 return sym_start + i;
810 }
811
GetSymbolTable(Elf32_Word section_type)812 ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) {
813 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
814 switch (section_type) {
815 case SHT_SYMTAB: {
816 return &symtab_symbol_table_;
817 }
818 case SHT_DYNSYM: {
819 return &dynsym_symbol_table_;
820 }
821 default: {
822 LOG(FATAL) << section_type;
823 return nullptr;
824 }
825 }
826 }
827
FindSymbolByName(Elf32_Word section_type,const std::string & symbol_name,bool build_map)828 Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type,
829 const std::string& symbol_name,
830 bool build_map) {
831 CHECK(!program_header_only_) << file_->GetPath();
832 CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type;
833
834 SymbolTable** symbol_table = GetSymbolTable(section_type);
835 if (*symbol_table != nullptr || build_map) {
836 if (*symbol_table == nullptr) {
837 DCHECK(build_map);
838 *symbol_table = new SymbolTable;
839 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
840 if (symbol_section == nullptr) {
841 return nullptr; // Failure condition.
842 }
843 Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
844 if (string_section == nullptr) {
845 return nullptr; // Failure condition.
846 }
847 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
848 Elf32_Sym* symbol = GetSymbol(section_type, i);
849 if (symbol == nullptr) {
850 return nullptr; // Failure condition.
851 }
852 unsigned char type = ELF32_ST_TYPE(symbol->st_info);
853 if (type == STT_NOTYPE) {
854 continue;
855 }
856 const char* name = GetString(*string_section, symbol->st_name);
857 if (name == nullptr) {
858 continue;
859 }
860 std::pair<SymbolTable::iterator, bool> result =
861 (*symbol_table)->insert(std::make_pair(name, symbol));
862 if (!result.second) {
863 // If a duplicate, make sure it has the same logical value. Seen on x86.
864 if ((symbol->st_value != result.first->second->st_value) ||
865 (symbol->st_size != result.first->second->st_size) ||
866 (symbol->st_info != result.first->second->st_info) ||
867 (symbol->st_other != result.first->second->st_other) ||
868 (symbol->st_shndx != result.first->second->st_shndx)) {
869 return nullptr; // Failure condition.
870 }
871 }
872 }
873 }
874 CHECK(*symbol_table != nullptr);
875 SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
876 if (it == (*symbol_table)->end()) {
877 return nullptr;
878 }
879 return it->second;
880 }
881
882 // Fall back to linear search
883 Elf32_Shdr* symbol_section = FindSectionByType(section_type);
884 if (symbol_section == nullptr) {
885 return nullptr;
886 }
887 Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
888 if (string_section == nullptr) {
889 return nullptr;
890 }
891 for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
892 Elf32_Sym* symbol = GetSymbol(section_type, i);
893 if (symbol == nullptr) {
894 return nullptr; // Failure condition.
895 }
896 const char* name = GetString(*string_section, symbol->st_name);
897 if (name == nullptr) {
898 continue;
899 }
900 if (symbol_name == name) {
901 return symbol;
902 }
903 }
904 return nullptr;
905 }
906
FindSymbolAddress(Elf32_Word section_type,const std::string & symbol_name,bool build_map)907 Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type,
908 const std::string& symbol_name,
909 bool build_map) {
910 Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
911 if (symbol == nullptr) {
912 return 0;
913 }
914 return symbol->st_value;
915 }
916
GetString(Elf32_Shdr & string_section,Elf32_Word i) const917 const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const {
918 CHECK(!program_header_only_) << file_->GetPath();
919 // TODO: remove this static_cast from enum when using -std=gnu++0x
920 if (static_cast<Elf32_Word>(SHT_STRTAB) != string_section.sh_type) {
921 return nullptr; // Failure condition.
922 }
923 if (i >= string_section.sh_size) {
924 return nullptr;
925 }
926 if (i == 0) {
927 return nullptr;
928 }
929 byte* strings = Begin() + string_section.sh_offset;
930 byte* string = strings + i;
931 if (string >= End()) {
932 return nullptr;
933 }
934 return reinterpret_cast<const char*>(string);
935 }
936
GetDynamicNum() const937 Elf32_Word ElfFile::GetDynamicNum() const {
938 return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn);
939 }
940
GetDynamic(Elf32_Word i) const941 Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const {
942 CHECK_LT(i, GetDynamicNum()) << file_->GetPath();
943 return *(GetDynamicSectionStart() + i);
944 }
945
FindDynamicByType(Elf32_Sword type) const946 Elf32_Dyn* ElfFile::FindDynamicByType(Elf32_Sword type) const {
947 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
948 Elf32_Dyn* dyn = &GetDynamic(i);
949 if (dyn->d_tag == type) {
950 return dyn;
951 }
952 }
953 return NULL;
954 }
955
FindDynamicValueByType(Elf32_Sword type) const956 Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const {
957 Elf32_Dyn* dyn = FindDynamicByType(type);
958 if (dyn == NULL) {
959 return 0;
960 } else {
961 return dyn->d_un.d_val;
962 }
963 }
964
GetRelSectionStart(Elf32_Shdr & section_header) const965 Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const {
966 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
967 return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset);
968 }
969
GetRelNum(Elf32_Shdr & section_header) const970 Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const {
971 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
972 CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath();
973 return section_header.sh_size / section_header.sh_entsize;
974 }
975
GetRel(Elf32_Shdr & section_header,Elf32_Word i) const976 Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const {
977 CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
978 CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath();
979 return *(GetRelSectionStart(section_header) + i);
980 }
981
GetRelaSectionStart(Elf32_Shdr & section_header) const982 Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const {
983 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
984 return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset);
985 }
986
GetRelaNum(Elf32_Shdr & section_header) const987 Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const {
988 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
989 return section_header.sh_size / section_header.sh_entsize;
990 }
991
GetRela(Elf32_Shdr & section_header,Elf32_Word i) const992 Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const {
993 CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type;
994 CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath();
995 return *(GetRelaSectionStart(section_header) + i);
996 }
997
998 // Base on bionic phdr_table_get_load_size
GetLoadedSize() const999 size_t ElfFile::GetLoadedSize() const {
1000 Elf32_Addr min_vaddr = 0xFFFFFFFFu;
1001 Elf32_Addr max_vaddr = 0x00000000u;
1002 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
1003 Elf32_Phdr* program_header = GetProgramHeader(i);
1004 if (program_header->p_type != PT_LOAD) {
1005 continue;
1006 }
1007 Elf32_Addr begin_vaddr = program_header->p_vaddr;
1008 if (begin_vaddr < min_vaddr) {
1009 min_vaddr = begin_vaddr;
1010 }
1011 Elf32_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
1012 if (end_vaddr > max_vaddr) {
1013 max_vaddr = end_vaddr;
1014 }
1015 }
1016 min_vaddr = RoundDown(min_vaddr, kPageSize);
1017 max_vaddr = RoundUp(max_vaddr, kPageSize);
1018 CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath();
1019 size_t loaded_size = max_vaddr - min_vaddr;
1020 return loaded_size;
1021 }
1022
Load(bool executable,std::string * error_msg)1023 bool ElfFile::Load(bool executable, std::string* error_msg) {
1024 CHECK(program_header_only_) << file_->GetPath();
1025
1026 if (executable) {
1027 InstructionSet elf_ISA = kNone;
1028 switch (GetHeader().e_machine) {
1029 case EM_ARM: {
1030 elf_ISA = kArm;
1031 break;
1032 }
1033 case EM_AARCH64: {
1034 elf_ISA = kArm64;
1035 break;
1036 }
1037 case EM_386: {
1038 elf_ISA = kX86;
1039 break;
1040 }
1041 case EM_X86_64: {
1042 elf_ISA = kX86_64;
1043 break;
1044 }
1045 case EM_MIPS: {
1046 elf_ISA = kMips;
1047 break;
1048 }
1049 }
1050
1051 if (elf_ISA != kRuntimeISA) {
1052 std::ostringstream oss;
1053 oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1054 *error_msg = oss.str();
1055 return false;
1056 }
1057 }
1058
1059 bool reserved = false;
1060 for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) {
1061 Elf32_Phdr* program_header = GetProgramHeader(i);
1062 if (program_header == nullptr) {
1063 *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1064 i, file_->GetPath().c_str());
1065 return false;
1066 }
1067
1068 // Record .dynamic header information for later use
1069 if (program_header->p_type == PT_DYNAMIC) {
1070 dynamic_program_header_ = program_header;
1071 continue;
1072 }
1073
1074 // Not something to load, move on.
1075 if (program_header->p_type != PT_LOAD) {
1076 continue;
1077 }
1078
1079 // Found something to load.
1080
1081 // Before load the actual segments, reserve a contiguous chunk
1082 // of required size and address for all segments, but with no
1083 // permissions. We'll then carve that up with the proper
1084 // permissions as we load the actual segments. If p_vaddr is
1085 // non-zero, the segments require the specific address specified,
1086 // which either was specified in the file because we already set
1087 // base_address_ after the first zero segment).
1088 int64_t temp_file_length = file_->GetLength();
1089 if (temp_file_length < 0) {
1090 errno = -temp_file_length;
1091 *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1092 file_->GetPath().c_str(), file_->Fd(), strerror(errno));
1093 return false;
1094 }
1095 size_t file_length = static_cast<size_t>(temp_file_length);
1096 if (!reserved) {
1097 byte* reserve_base = ((program_header->p_vaddr != 0) ?
1098 reinterpret_cast<byte*>(program_header->p_vaddr) : nullptr);
1099 std::string reservation_name("ElfFile reservation for ");
1100 reservation_name += file_->GetPath();
1101 std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
1102 reserve_base,
1103 GetLoadedSize(), PROT_NONE, false,
1104 error_msg));
1105 if (reserve.get() == nullptr) {
1106 *error_msg = StringPrintf("Failed to allocate %s: %s",
1107 reservation_name.c_str(), error_msg->c_str());
1108 return false;
1109 }
1110 reserved = true;
1111 if (reserve_base == nullptr) {
1112 base_address_ = reserve->Begin();
1113 }
1114 segments_.push_back(reserve.release());
1115 }
1116 // empty segment, nothing to map
1117 if (program_header->p_memsz == 0) {
1118 continue;
1119 }
1120 byte* p_vaddr = base_address_ + program_header->p_vaddr;
1121 int prot = 0;
1122 if (executable && ((program_header->p_flags & PF_X) != 0)) {
1123 prot |= PROT_EXEC;
1124 }
1125 if ((program_header->p_flags & PF_W) != 0) {
1126 prot |= PROT_WRITE;
1127 }
1128 if ((program_header->p_flags & PF_R) != 0) {
1129 prot |= PROT_READ;
1130 }
1131 int flags = 0;
1132 if (writable_) {
1133 prot |= PROT_WRITE;
1134 flags |= MAP_SHARED;
1135 } else {
1136 flags |= MAP_PRIVATE;
1137 }
1138 if (file_length < (program_header->p_offset + program_header->p_memsz)) {
1139 *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1140 "%d of %d bytes: '%s'", file_length, i,
1141 program_header->p_offset + program_header->p_memsz,
1142 file_->GetPath().c_str());
1143 return false;
1144 }
1145 std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr,
1146 program_header->p_memsz,
1147 prot, flags, file_->Fd(),
1148 program_header->p_offset,
1149 true, // implies MAP_FIXED
1150 file_->GetPath().c_str(),
1151 error_msg));
1152 if (segment.get() == nullptr) {
1153 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1154 i, file_->GetPath().c_str(), error_msg->c_str());
1155 return false;
1156 }
1157 if (segment->Begin() != p_vaddr) {
1158 *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1159 "instead mapped to %p",
1160 i, file_->GetPath().c_str(), p_vaddr, segment->Begin());
1161 return false;
1162 }
1163 segments_.push_back(segment.release());
1164 }
1165
1166 // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
1167 byte* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
1168 if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1169 *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1170 file_->GetPath().c_str());
1171 return false;
1172 }
1173 dynamic_section_start_ = reinterpret_cast<Elf32_Dyn*>(dsptr);
1174
1175 for (Elf32_Word i = 0; i < GetDynamicNum(); i++) {
1176 Elf32_Dyn& elf_dyn = GetDynamic(i);
1177 byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
1178 switch (elf_dyn.d_tag) {
1179 case DT_HASH: {
1180 if (!ValidPointer(d_ptr)) {
1181 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1182 d_ptr, file_->GetPath().c_str());
1183 return false;
1184 }
1185 hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr);
1186 break;
1187 }
1188 case DT_STRTAB: {
1189 if (!ValidPointer(d_ptr)) {
1190 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1191 d_ptr, file_->GetPath().c_str());
1192 return false;
1193 }
1194 dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1195 break;
1196 }
1197 case DT_SYMTAB: {
1198 if (!ValidPointer(d_ptr)) {
1199 *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1200 d_ptr, file_->GetPath().c_str());
1201 return false;
1202 }
1203 dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr);
1204 break;
1205 }
1206 case DT_NULL: {
1207 if (GetDynamicNum() != i+1) {
1208 *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1209 "expected %d as implied by size of PT_DYNAMIC segment in %s",
1210 i + 1, GetDynamicNum(), file_->GetPath().c_str());
1211 return false;
1212 }
1213 break;
1214 }
1215 }
1216 }
1217
1218 // Check for the existence of some sections.
1219 if (!CheckSectionsExist(error_msg)) {
1220 return false;
1221 }
1222
1223 // Use GDB JIT support to do stack backtrace, etc.
1224 if (executable) {
1225 GdbJITSupport();
1226 }
1227
1228 return true;
1229 }
1230
ValidPointer(const byte * start) const1231 bool ElfFile::ValidPointer(const byte* start) const {
1232 for (size_t i = 0; i < segments_.size(); ++i) {
1233 const MemMap* segment = segments_[i];
1234 if (segment->Begin() <= start && start < segment->End()) {
1235 return true;
1236 }
1237 }
1238 return false;
1239 }
1240
1241
FindSectionByName(const std::string & name) const1242 Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const {
1243 CHECK(!program_header_only_);
1244 Elf32_Shdr* shstrtab_sec = GetSectionNameStringSection();
1245 if (shstrtab_sec == nullptr) {
1246 return nullptr;
1247 }
1248 for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
1249 Elf32_Shdr* shdr = GetSectionHeader(i);
1250 if (shdr == nullptr) {
1251 return nullptr;
1252 }
1253 const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
1254 if (sec_name == nullptr) {
1255 continue;
1256 }
1257 if (name == sec_name) {
1258 return shdr;
1259 }
1260 }
1261 return nullptr;
1262 }
1263
1264 struct PACKED(1) FDE {
1265 uint32_t raw_length_;
GetLengthart::FDE1266 uint32_t GetLength() {
1267 return raw_length_ + sizeof(raw_length_);
1268 }
1269 uint32_t CIE_pointer;
1270 uint32_t initial_location;
1271 uint32_t address_range;
1272 uint8_t instructions[0];
1273 };
1274
NextFDE(FDE * frame)1275 static FDE* NextFDE(FDE* frame) {
1276 byte* fde_bytes = reinterpret_cast<byte*>(frame);
1277 fde_bytes += frame->GetLength();
1278 return reinterpret_cast<FDE*>(fde_bytes);
1279 }
1280
IsFDE(FDE * frame)1281 static bool IsFDE(FDE* frame) {
1282 return frame->CIE_pointer != 0;
1283 }
1284
1285 // TODO This only works for 32-bit Elf Files.
FixupEHFrame(uintptr_t text_start,byte * eh_frame,size_t eh_frame_size)1286 static bool FixupEHFrame(uintptr_t text_start, byte* eh_frame, size_t eh_frame_size) {
1287 FDE* last_frame = reinterpret_cast<FDE*>(eh_frame + eh_frame_size);
1288 FDE* frame = NextFDE(reinterpret_cast<FDE*>(eh_frame));
1289 for (; frame < last_frame; frame = NextFDE(frame)) {
1290 if (!IsFDE(frame)) {
1291 return false;
1292 }
1293 frame->initial_location += text_start;
1294 }
1295 return true;
1296 }
1297
1298 struct PACKED(1) DebugInfoHeader {
1299 uint32_t unit_length; // TODO 32-bit specific size
1300 uint16_t version;
1301 uint32_t debug_abbrev_offset; // TODO 32-bit specific size
1302 uint8_t address_size;
1303 };
1304
1305 // Returns -1 if it is variable length, which we will just disallow for now.
FormLength(uint32_t att)1306 static int32_t FormLength(uint32_t att) {
1307 switch (att) {
1308 case DW_FORM_data1:
1309 case DW_FORM_flag:
1310 case DW_FORM_flag_present:
1311 case DW_FORM_ref1:
1312 return 1;
1313
1314 case DW_FORM_data2:
1315 case DW_FORM_ref2:
1316 return 2;
1317
1318 case DW_FORM_addr: // TODO 32-bit only
1319 case DW_FORM_ref_addr: // TODO 32-bit only
1320 case DW_FORM_sec_offset: // TODO 32-bit only
1321 case DW_FORM_strp: // TODO 32-bit only
1322 case DW_FORM_data4:
1323 case DW_FORM_ref4:
1324 return 4;
1325
1326 case DW_FORM_data8:
1327 case DW_FORM_ref8:
1328 case DW_FORM_ref_sig8:
1329 return 8;
1330
1331 case DW_FORM_block:
1332 case DW_FORM_block1:
1333 case DW_FORM_block2:
1334 case DW_FORM_block4:
1335 case DW_FORM_exprloc:
1336 case DW_FORM_indirect:
1337 case DW_FORM_ref_udata:
1338 case DW_FORM_sdata:
1339 case DW_FORM_string:
1340 case DW_FORM_udata:
1341 default:
1342 return -1;
1343 }
1344 }
1345
1346 class DebugTag {
1347 public:
1348 const uint32_t index_;
~DebugTag()1349 ~DebugTag() {}
1350 // Creates a new tag and moves data pointer up to the start of the next one.
1351 // nullptr means error.
Create(const byte ** data_pointer)1352 static DebugTag* Create(const byte** data_pointer) {
1353 const byte* data = *data_pointer;
1354 uint32_t index = DecodeUnsignedLeb128(&data);
1355 std::unique_ptr<DebugTag> tag(new DebugTag(index));
1356 tag->size_ = static_cast<uint32_t>(
1357 reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer));
1358 // skip the abbrev
1359 tag->tag_ = DecodeUnsignedLeb128(&data);
1360 tag->has_child_ = (*data == 0);
1361 data++;
1362 while (true) {
1363 uint32_t attr = DecodeUnsignedLeb128(&data);
1364 uint32_t form = DecodeUnsignedLeb128(&data);
1365 if (attr == 0 && form == 0) {
1366 break;
1367 } else if (attr == 0 || form == 0) {
1368 // Bad abbrev.
1369 return nullptr;
1370 }
1371 int32_t size = FormLength(form);
1372 if (size == -1) {
1373 return nullptr;
1374 }
1375 tag->AddAttribute(attr, static_cast<uint32_t>(size));
1376 }
1377 *data_pointer = data;
1378 return tag.release();
1379 }
1380
GetSize() const1381 uint32_t GetSize() const {
1382 return size_;
1383 }
1384
HasChild()1385 bool HasChild() {
1386 return has_child_;
1387 }
1388
GetTagNumber()1389 uint32_t GetTagNumber() {
1390 return tag_;
1391 }
1392
1393 // Gets the offset of a particular attribute in this tag structure.
1394 // Interpretation of the data is left to the consumer. 0 is returned if the
1395 // tag does not contain the attribute.
GetOffsetOf(uint32_t dwarf_attribute) const1396 uint32_t GetOffsetOf(uint32_t dwarf_attribute) const {
1397 auto it = off_map_.find(dwarf_attribute);
1398 if (it == off_map_.end()) {
1399 return 0;
1400 } else {
1401 return it->second;
1402 }
1403 }
1404
1405 // Gets the size of attribute
GetAttrSize(uint32_t dwarf_attribute) const1406 uint32_t GetAttrSize(uint32_t dwarf_attribute) const {
1407 auto it = size_map_.find(dwarf_attribute);
1408 if (it == size_map_.end()) {
1409 return 0;
1410 } else {
1411 return it->second;
1412 }
1413 }
1414
1415 private:
DebugTag(uint32_t index)1416 explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {}
AddAttribute(uint32_t type,uint32_t attr_size)1417 void AddAttribute(uint32_t type, uint32_t attr_size) {
1418 off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_));
1419 size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size));
1420 size_ += attr_size;
1421 }
1422 std::map<uint32_t, uint32_t> off_map_;
1423 std::map<uint32_t, uint32_t> size_map_;
1424 uint32_t size_;
1425 uint32_t tag_;
1426 bool has_child_;
1427 };
1428
1429 class DebugAbbrev {
1430 public:
~DebugAbbrev()1431 ~DebugAbbrev() {}
Create(const byte * dbg_abbrev,size_t dbg_abbrev_size)1432 static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) {
1433 std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev);
1434 const byte* last = dbg_abbrev + dbg_abbrev_size;
1435 while (dbg_abbrev < last) {
1436 std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev));
1437 if (tag.get() == nullptr) {
1438 return nullptr;
1439 } else {
1440 abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size()));
1441 abbrev->tag_list_.push_back(std::move(tag));
1442 }
1443 }
1444 return abbrev.release();
1445 }
1446
ReadTag(const byte * entry)1447 DebugTag* ReadTag(const byte* entry) {
1448 uint32_t tag_num = DecodeUnsignedLeb128(&entry);
1449 auto it = tags_.find(tag_num);
1450 if (it == tags_.end()) {
1451 return nullptr;
1452 } else {
1453 CHECK_GT(tag_list_.size(), it->second);
1454 return tag_list_.at(it->second).get();
1455 }
1456 }
1457
1458 private:
DebugAbbrev()1459 DebugAbbrev() {}
1460 std::map<uint32_t, uint32_t> tags_;
1461 std::vector<std::unique_ptr<DebugTag>> tag_list_;
1462 };
1463
1464 class DebugInfoIterator {
1465 public:
Create(DebugInfoHeader * header,size_t frame_size,DebugAbbrev * abbrev)1466 static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size,
1467 DebugAbbrev* abbrev) {
1468 std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev));
1469 if (iter->GetCurrentTag() == nullptr) {
1470 return nullptr;
1471 } else {
1472 return iter.release();
1473 }
1474 }
~DebugInfoIterator()1475 ~DebugInfoIterator() {}
1476
1477 // Moves to the next DIE. Returns false if at last entry.
1478 // TODO Handle variable length attributes.
next()1479 bool next() {
1480 if (current_entry_ == nullptr || current_tag_ == nullptr) {
1481 return false;
1482 }
1483 current_entry_ += current_tag_->GetSize();
1484 if (current_entry_ >= last_entry_) {
1485 current_entry_ = nullptr;
1486 return false;
1487 }
1488 current_tag_ = abbrev_->ReadTag(current_entry_);
1489 if (current_tag_ == nullptr) {
1490 current_entry_ = nullptr;
1491 return false;
1492 } else {
1493 return true;
1494 }
1495 }
1496
GetCurrentTag()1497 const DebugTag* GetCurrentTag() {
1498 return const_cast<DebugTag*>(current_tag_);
1499 }
GetPointerToField(uint8_t dwarf_field)1500 byte* GetPointerToField(uint8_t dwarf_field) {
1501 if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) {
1502 return nullptr;
1503 }
1504 uint32_t off = current_tag_->GetOffsetOf(dwarf_field);
1505 if (off == 0) {
1506 // tag does not have that field.
1507 return nullptr;
1508 } else {
1509 DCHECK_LT(off, current_tag_->GetSize());
1510 return current_entry_ + off;
1511 }
1512 }
1513
1514 private:
DebugInfoIterator(DebugInfoHeader * header,size_t frame_size,DebugAbbrev * abbrev)1515 DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev)
1516 : abbrev_(abbrev),
1517 last_entry_(reinterpret_cast<byte*>(header) + frame_size),
1518 current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)),
1519 current_tag_(abbrev_->ReadTag(current_entry_)) {}
1520 DebugAbbrev* abbrev_;
1521 byte* last_entry_;
1522 byte* current_entry_;
1523 DebugTag* current_tag_;
1524 };
1525
FixupDebugInfo(uint32_t text_start,DebugInfoIterator * iter)1526 static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) {
1527 do {
1528 if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) ||
1529 iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) {
1530 return false;
1531 }
1532 uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc));
1533 uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc));
1534 if (PC_low != nullptr && PC_high != nullptr) {
1535 *PC_low += text_start;
1536 *PC_high += text_start;
1537 }
1538 } while (iter->next());
1539 return true;
1540 }
1541
FixupDebugSections(const byte * dbg_abbrev,size_t dbg_abbrev_size,uintptr_t text_start,byte * dbg_info,size_t dbg_info_size,byte * eh_frame,size_t eh_frame_size)1542 static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size,
1543 uintptr_t text_start,
1544 byte* dbg_info, size_t dbg_info_size,
1545 byte* eh_frame, size_t eh_frame_size) {
1546 std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size));
1547 if (abbrev.get() == nullptr) {
1548 return false;
1549 }
1550 std::unique_ptr<DebugInfoIterator> iter(
1551 DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info),
1552 dbg_info_size, abbrev.get()));
1553 if (iter.get() == nullptr) {
1554 return false;
1555 }
1556 return FixupDebugInfo(text_start, iter.get())
1557 && FixupEHFrame(text_start, eh_frame, eh_frame_size);
1558 }
1559
GdbJITSupport()1560 void ElfFile::GdbJITSupport() {
1561 // We only get here if we only are mapping the program header.
1562 DCHECK(program_header_only_);
1563
1564 // Well, we need the whole file to do this.
1565 std::string error_msg;
1566 // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary
1567 // sections are there.
1568 std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE,
1569 MAP_PRIVATE, &error_msg));
1570 if (all_ptr.get() == nullptr) {
1571 return;
1572 }
1573 ElfFile& all = *all_ptr;
1574
1575 // Do we have interesting sections?
1576 const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info");
1577 const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev");
1578 const Elf32_Shdr* eh_frame = all.FindSectionByName(".eh_frame");
1579 const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str");
1580 const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab");
1581 const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab");
1582 Elf32_Shdr* text_sec = all.FindSectionByName(".text");
1583 if (debug_info == nullptr || debug_abbrev == nullptr || eh_frame == nullptr ||
1584 debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr ||
1585 symtab_sec == nullptr) {
1586 return;
1587 }
1588 // We need to add in a strtab and symtab to the image.
1589 // all is MAP_PRIVATE so it can be written to freely.
1590 // We also already have strtab and symtab so we are fine there.
1591 Elf32_Ehdr& elf_hdr = all.GetHeader();
1592 elf_hdr.e_entry = 0;
1593 elf_hdr.e_phoff = 0;
1594 elf_hdr.e_phnum = 0;
1595 elf_hdr.e_phentsize = 0;
1596 elf_hdr.e_type = ET_EXEC;
1597
1598 text_sec->sh_type = SHT_NOBITS;
1599 text_sec->sh_offset = 0;
1600
1601 if (!FixupDebugSections(
1602 all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr,
1603 all.Begin() + debug_info->sh_offset, debug_info->sh_size,
1604 all.Begin() + eh_frame->sh_offset, eh_frame->sh_size)) {
1605 LOG(ERROR) << "Failed to load GDB data";
1606 return;
1607 }
1608
1609 jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size());
1610 gdb_file_mapping_.reset(all_ptr.release());
1611 }
1612
1613 } // namespace art
1614