OSDN Git Service

Differentiate between native alloc and normal background GC
[android-x86/art.git] / runtime / elf_file.cc
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 <inttypes.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include "android-base/stringprintf.h"
24 #include "android-base/strings.h"
25
26 #include "arch/instruction_set.h"
27 #include "base/logging.h"
28 #include "base/stl_util.h"
29 #include "base/unix_file/fd_file.h"
30 #include "elf_file_impl.h"
31 #include "elf_utils.h"
32 #include "leb128.h"
33 #include "utils.h"
34
35 namespace art {
36
37 using android::base::StringPrintf;
38
39 template <typename ElfTypes>
40 ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable,
41                                    bool program_header_only,
42                                    uint8_t* requested_base)
43   : writable_(writable),
44     program_header_only_(program_header_only),
45     header_(nullptr),
46     base_address_(nullptr),
47     program_headers_start_(nullptr),
48     section_headers_start_(nullptr),
49     dynamic_program_header_(nullptr),
50     dynamic_section_start_(nullptr),
51     symtab_section_start_(nullptr),
52     dynsym_section_start_(nullptr),
53     strtab_section_start_(nullptr),
54     dynstr_section_start_(nullptr),
55     hash_section_start_(nullptr),
56     symtab_symbol_table_(nullptr),
57     dynsym_symbol_table_(nullptr),
58     requested_base_(requested_base) {
59   CHECK(file != nullptr);
60 }
61
62 template <typename ElfTypes>
63 ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
64                                                    bool writable,
65                                                    bool program_header_only,
66                                                    bool low_4gb,
67                                                    std::string* error_msg,
68                                                    uint8_t* requested_base) {
69   std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
70       (file, writable, program_header_only, requested_base));
71   int prot;
72   int flags;
73   if (writable) {
74     prot = PROT_READ | PROT_WRITE;
75     flags = MAP_SHARED;
76   } else {
77     prot = PROT_READ;
78     flags = MAP_PRIVATE;
79   }
80   if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
81     return nullptr;
82   }
83   return elf_file.release();
84 }
85
86 template <typename ElfTypes>
87 ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open(File* file,
88                                                    int prot,
89                                                    int flags,
90                                                    bool low_4gb,
91                                                    std::string* error_msg) {
92   std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes>
93       (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false,
94       /*requested_base*/nullptr));
95   if (!elf_file->Setup(file, prot, flags, low_4gb, error_msg)) {
96     return nullptr;
97   }
98   return elf_file.release();
99 }
100
101 template <typename ElfTypes>
102 bool ElfFileImpl<ElfTypes>::Setup(File* file,
103                                   int prot,
104                                   int flags,
105                                   bool low_4gb,
106                                   std::string* error_msg) {
107   int64_t temp_file_length = file->GetLength();
108   if (temp_file_length < 0) {
109     errno = -temp_file_length;
110     *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
111                               file->GetPath().c_str(), file->Fd(), strerror(errno));
112     return false;
113   }
114   size_t file_length = static_cast<size_t>(temp_file_length);
115   if (file_length < sizeof(Elf_Ehdr)) {
116     *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of "
117                               "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr),
118                               file->GetPath().c_str());
119     return false;
120   }
121
122   if (program_header_only_) {
123     // first just map ELF header to get program header size information
124     size_t elf_header_size = sizeof(Elf_Ehdr);
125     if (!SetMap(file,
126                 MemMap::MapFile(elf_header_size,
127                                 prot,
128                                 flags,
129                                 file->Fd(),
130                                 0,
131                                 low_4gb,
132                                 file->GetPath().c_str(),
133                                 error_msg),
134                 error_msg)) {
135       return false;
136     }
137     // then remap to cover program header
138     size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum);
139     if (file_length < program_header_size) {
140       *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program "
141                                 "header of %zd bytes: '%s'", file_length,
142                                 sizeof(Elf_Ehdr), file->GetPath().c_str());
143       return false;
144     }
145     if (!SetMap(file,
146                 MemMap::MapFile(program_header_size,
147                                 prot,
148                                 flags,
149                                 file->Fd(),
150                                 0,
151                                 low_4gb,
152                                 file->GetPath().c_str(),
153                                 error_msg),
154                 error_msg)) {
155       *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str());
156       return false;
157     }
158   } else {
159     // otherwise map entire file
160     if (!SetMap(file,
161                 MemMap::MapFile(file->GetLength(),
162                                 prot,
163                                 flags,
164                                 file->Fd(),
165                                 0,
166                                 low_4gb,
167                                 file->GetPath().c_str(),
168                                 error_msg),
169                 error_msg)) {
170       *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str());
171       return false;
172     }
173   }
174
175   if (program_header_only_) {
176     program_headers_start_ = Begin() + GetHeader().e_phoff;
177   } else {
178     if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) {
179       return false;
180     }
181
182     // Setup section headers.
183     if (!CheckAndSet(GetHeader().e_shoff, "section headers", &section_headers_start_, error_msg)) {
184       return false;
185     }
186
187     // Find shstrtab.
188     Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection();
189     if (shstrtab_section_header == nullptr) {
190       *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'",
191                                 file->GetPath().c_str());
192       return false;
193     }
194
195     // Find .dynamic section info from program header
196     dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC);
197     if (dynamic_program_header_ == nullptr) {
198       *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
199                                 file->GetPath().c_str());
200       return false;
201     }
202
203     if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section",
204                      reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) {
205       return false;
206     }
207
208     // Find other sections from section headers
209     for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
210       Elf_Shdr* section_header = GetSectionHeader(i);
211       if (section_header == nullptr) {
212         *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'",
213                                   i, file->GetPath().c_str());
214         return false;
215       }
216       switch (section_header->sh_type) {
217         case SHT_SYMTAB: {
218           if (!CheckAndSet(section_header->sh_offset, "symtab",
219                            reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) {
220             return false;
221           }
222           break;
223         }
224         case SHT_DYNSYM: {
225           if (!CheckAndSet(section_header->sh_offset, "dynsym",
226                            reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) {
227             return false;
228           }
229           break;
230         }
231         case SHT_STRTAB: {
232           // TODO: base these off of sh_link from .symtab and .dynsym above
233           if ((section_header->sh_flags & SHF_ALLOC) != 0) {
234             // Check that this is named ".dynstr" and ignore otherwise.
235             const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
236             if (strncmp(".dynstr", header_name, 8) == 0) {
237               if (!CheckAndSet(section_header->sh_offset, "dynstr",
238                                reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) {
239                 return false;
240               }
241             }
242           } else {
243             // Check that this is named ".strtab" and ignore otherwise.
244             const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name);
245             if (strncmp(".strtab", header_name, 8) == 0) {
246               if (!CheckAndSet(section_header->sh_offset, "strtab",
247                                reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) {
248                 return false;
249               }
250             }
251           }
252           break;
253         }
254         case SHT_DYNAMIC: {
255           if (reinterpret_cast<uint8_t*>(dynamic_section_start_) !=
256               Begin() + section_header->sh_offset) {
257             LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in "
258                          << file->GetPath() << ": " << std::hex
259                          << reinterpret_cast<void*>(dynamic_section_start_)
260                          << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset);
261             return false;
262           }
263           break;
264         }
265         case SHT_HASH: {
266           if (!CheckAndSet(section_header->sh_offset, "hash section",
267                            reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) {
268             return false;
269           }
270           break;
271         }
272       }
273     }
274
275     // Check for the existence of some sections.
276     if (!CheckSectionsExist(file, error_msg)) {
277       return false;
278     }
279   }
280
281   return true;
282 }
283
284 template <typename ElfTypes>
285 ElfFileImpl<ElfTypes>::~ElfFileImpl() {
286   STLDeleteElements(&segments_);
287   delete symtab_symbol_table_;
288   delete dynsym_symbol_table_;
289 }
290
291 template <typename ElfTypes>
292 bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label,
293                                         uint8_t** target, std::string* error_msg) {
294   if (Begin() + offset >= End()) {
295     *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label,
296                               file_path_.c_str());
297     return false;
298   }
299   *target = Begin() + offset;
300   return true;
301 }
302
303 template <typename ElfTypes>
304 bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source,
305                                                 const uint8_t* target) const {
306   // Only works in whole-program mode, as we need to iterate over the sections.
307   // Note that we normally can't search by type, as duplicates are allowed for most section types.
308   if (program_header_only_) {
309     return true;
310   }
311
312   Elf_Shdr* source_section = nullptr;
313   Elf_Word target_index = 0;
314   bool target_found = false;
315   for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
316     Elf_Shdr* section_header = GetSectionHeader(i);
317
318     if (Begin() + section_header->sh_offset == source) {
319       // Found the source.
320       source_section = section_header;
321       if (target_index) {
322         break;
323       }
324     } else if (Begin() + section_header->sh_offset == target) {
325       target_index = i;
326       target_found = true;
327       if (source_section != nullptr) {
328         break;
329       }
330     }
331   }
332
333   return target_found && source_section != nullptr && source_section->sh_link == target_index;
334 }
335
336 template <typename ElfTypes>
337   bool ElfFileImpl<ElfTypes>::CheckSectionsExist(File* file, std::string* error_msg) const {
338   if (!program_header_only_) {
339     // If in full mode, need section headers.
340     if (section_headers_start_ == nullptr) {
341       *error_msg = StringPrintf("No section headers in ELF file: '%s'", file->GetPath().c_str());
342       return false;
343     }
344   }
345
346   // This is redundant, but defensive.
347   if (dynamic_program_header_ == nullptr) {
348     *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'",
349                               file->GetPath().c_str());
350     return false;
351   }
352
353   // Need a dynamic section. This is redundant, but defensive.
354   if (dynamic_section_start_ == nullptr) {
355     *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'",
356                               file->GetPath().c_str());
357     return false;
358   }
359
360   // Symtab validation. These is not really a hard failure, as we are currently not using the
361   // symtab internally, but it's nice to be defensive.
362   if (symtab_section_start_ != nullptr) {
363     // When there's a symtab, there should be a strtab.
364     if (strtab_section_start_ == nullptr) {
365       *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file->GetPath().c_str());
366       return false;
367     }
368
369     // The symtab should link to the strtab.
370     if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_),
371                              reinterpret_cast<const uint8_t*>(strtab_section_start_))) {
372       *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'",
373                                 file->GetPath().c_str());
374       return false;
375     }
376   }
377
378   // We always need a dynstr & dynsym.
379   if (dynstr_section_start_ == nullptr) {
380     *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file->GetPath().c_str());
381     return false;
382   }
383   if (dynsym_section_start_ == nullptr) {
384     *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file->GetPath().c_str());
385     return false;
386   }
387
388   // Need a hash section for dynamic symbol lookup.
389   if (hash_section_start_ == nullptr) {
390     *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'",
391                               file->GetPath().c_str());
392     return false;
393   }
394
395   // And the hash section should be linking to the dynsym.
396   if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_),
397                            reinterpret_cast<const uint8_t*>(dynsym_section_start_))) {
398     *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'",
399                               file->GetPath().c_str());
400     return false;
401   }
402
403   // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for
404   // us). This is usually the last in an oat file, and a good indicator of whether writing was
405   // successful (or the process crashed and left garbage).
406   if (program_header_only_) {
407     // It might not be mapped, but we can compare against the file size.
408     int64_t offset = static_cast<int64_t>(GetHeader().e_shoff +
409                                           (GetHeader().e_shstrndx * GetHeader().e_shentsize));
410     if (offset >= file->GetLength()) {
411       *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'",
412                                 file->GetPath().c_str());
413       return false;
414     }
415   }
416
417   return true;
418 }
419
420 template <typename ElfTypes>
421 bool ElfFileImpl<ElfTypes>::SetMap(File* file, MemMap* map, std::string* error_msg) {
422   if (map == nullptr) {
423     // MemMap::Open should have already set an error.
424     DCHECK(!error_msg->empty());
425     return false;
426   }
427   map_.reset(map);
428   CHECK(map_.get() != nullptr) << file->GetPath();
429   CHECK(map_->Begin() != nullptr) << file->GetPath();
430
431   header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin());
432   if ((ELFMAG0 != header_->e_ident[EI_MAG0])
433       || (ELFMAG1 != header_->e_ident[EI_MAG1])
434       || (ELFMAG2 != header_->e_ident[EI_MAG2])
435       || (ELFMAG3 != header_->e_ident[EI_MAG3])) {
436     *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d",
437                               ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
438                               file->GetPath().c_str(),
439                               header_->e_ident[EI_MAG0],
440                               header_->e_ident[EI_MAG1],
441                               header_->e_ident[EI_MAG2],
442                               header_->e_ident[EI_MAG3]);
443     return false;
444   }
445   uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32;
446   if (elf_class != header_->e_ident[EI_CLASS]) {
447     *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d",
448                               elf_class,
449                               file->GetPath().c_str(),
450                               header_->e_ident[EI_CLASS]);
451     return false;
452   }
453   if (ELFDATA2LSB != header_->e_ident[EI_DATA]) {
454     *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d",
455                               ELFDATA2LSB,
456                               file->GetPath().c_str(),
457                               header_->e_ident[EI_CLASS]);
458     return false;
459   }
460   if (EV_CURRENT != header_->e_ident[EI_VERSION]) {
461     *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d",
462                               EV_CURRENT,
463                               file->GetPath().c_str(),
464                               header_->e_ident[EI_CLASS]);
465     return false;
466   }
467   if (ET_DYN != header_->e_type) {
468     *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d",
469                               ET_DYN,
470                               file->GetPath().c_str(),
471                               header_->e_type);
472     return false;
473   }
474   if (EV_CURRENT != header_->e_version) {
475     *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d",
476                               EV_CURRENT,
477                               file->GetPath().c_str(),
478                               header_->e_version);
479     return false;
480   }
481   if (0 != header_->e_entry) {
482     *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d",
483                               0,
484                               file->GetPath().c_str(),
485                               static_cast<int32_t>(header_->e_entry));
486     return false;
487   }
488   if (0 == header_->e_phoff) {
489     *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s",
490                               file->GetPath().c_str());
491     return false;
492   }
493   if (0 == header_->e_shoff) {
494     *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s",
495                               file->GetPath().c_str());
496     return false;
497   }
498   if (0 == header_->e_ehsize) {
499     *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s",
500                               file->GetPath().c_str());
501     return false;
502   }
503   if (0 == header_->e_phentsize) {
504     *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s",
505                               file->GetPath().c_str());
506     return false;
507   }
508   if (0 == header_->e_phnum) {
509     *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s",
510                               file->GetPath().c_str());
511     return false;
512   }
513   if (0 == header_->e_shentsize) {
514     *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s",
515                               file->GetPath().c_str());
516     return false;
517   }
518   if (0 == header_->e_shnum) {
519     *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s",
520                               file->GetPath().c_str());
521     return false;
522   }
523   if (0 == header_->e_shstrndx) {
524     *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s",
525                               file->GetPath().c_str());
526     return false;
527   }
528   if (header_->e_shstrndx >= header_->e_shnum) {
529     *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s",
530                               header_->e_shstrndx,
531                               header_->e_shnum,
532                               file->GetPath().c_str());
533     return false;
534   }
535
536   if (!program_header_only_) {
537     if (header_->e_phoff >= Size()) {
538       *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s",
539                                 static_cast<uint64_t>(header_->e_phoff),
540                                 Size(),
541                                 file->GetPath().c_str());
542       return false;
543     }
544     if (header_->e_shoff >= Size()) {
545       *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s",
546                                 static_cast<uint64_t>(header_->e_shoff),
547                                 Size(),
548                                 file->GetPath().c_str());
549       return false;
550     }
551   }
552   return true;
553 }
554
555 template <typename ElfTypes>
556 typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const {
557   CHECK(header_ != nullptr);  // Header has been checked in SetMap. This is a sanity check.
558   return *header_;
559 }
560
561 template <typename ElfTypes>
562 uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const {
563   CHECK(program_headers_start_ != nullptr);  // Header has been set in Setup. This is a sanity
564                                              // check.
565   return program_headers_start_;
566 }
567
568 template <typename ElfTypes>
569 uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const {
570   CHECK(!program_header_only_);              // Only used in "full" mode.
571   CHECK(section_headers_start_ != nullptr);  // Is checked in CheckSectionsExist. Sanity check.
572   return section_headers_start_;
573 }
574
575 template <typename ElfTypes>
576 typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const {
577   CHECK(dynamic_program_header_ != nullptr);  // Is checked in CheckSectionsExist. Sanity check.
578   return *dynamic_program_header_;
579 }
580
581 template <typename ElfTypes>
582 typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const {
583   CHECK(dynamic_section_start_ != nullptr);  // Is checked in CheckSectionsExist. Sanity check.
584   return dynamic_section_start_;
585 }
586
587 template <typename ElfTypes>
588 typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart(
589     Elf_Word section_type) const {
590   CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
591   switch (section_type) {
592     case SHT_SYMTAB: {
593       return symtab_section_start_;
594       break;
595     }
596     case SHT_DYNSYM: {
597       return dynsym_section_start_;
598       break;
599     }
600     default: {
601       LOG(FATAL) << section_type;
602       return nullptr;
603     }
604   }
605 }
606
607 template <typename ElfTypes>
608 const char* ElfFileImpl<ElfTypes>::GetStringSectionStart(
609     Elf_Word section_type) const {
610   CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
611   switch (section_type) {
612     case SHT_SYMTAB: {
613       return strtab_section_start_;
614     }
615     case SHT_DYNSYM: {
616       return dynstr_section_start_;
617     }
618     default: {
619       LOG(FATAL) << section_type;
620       return nullptr;
621     }
622   }
623 }
624
625 template <typename ElfTypes>
626 const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type,
627                                              Elf_Word i) const {
628   CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
629   if (i == 0) {
630     return nullptr;
631   }
632   const char* string_section_start = GetStringSectionStart(section_type);
633   if (string_section_start == nullptr) {
634     return nullptr;
635   }
636   return string_section_start + i;
637 }
638
639 // WARNING: The following methods do not check for an error condition (non-existent hash section).
640 //          It is the caller's job to do this.
641
642 template <typename ElfTypes>
643 typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const {
644   return hash_section_start_;
645 }
646
647 template <typename ElfTypes>
648 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const {
649   return GetHashSectionStart()[0];
650 }
651
652 template <typename ElfTypes>
653 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const {
654   return GetHashSectionStart()[1];
655 }
656
657 template <typename ElfTypes>
658 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const {
659   if (i >= GetHashBucketNum()) {
660     *ok = false;
661     return 0;
662   }
663   *ok = true;
664   // 0 is nbucket, 1 is nchain
665   return GetHashSectionStart()[2 + i];
666 }
667
668 template <typename ElfTypes>
669 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const {
670   if (i >= GetHashChainNum()) {
671     *ok = false;
672     return 0;
673   }
674   *ok = true;
675   // 0 is nbucket, 1 is nchain, & chains are after buckets
676   return GetHashSectionStart()[2 + GetHashBucketNum() + i];
677 }
678
679 template <typename ElfTypes>
680 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const {
681   return GetHeader().e_phnum;
682 }
683
684 template <typename ElfTypes>
685 typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const {
686   CHECK_LT(i, GetProgramHeaderNum()) << file_path_;  // Sanity check for caller.
687   uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize);
688   if (program_header >= End()) {
689     return nullptr;  // Failure condition.
690   }
691   return reinterpret_cast<Elf_Phdr*>(program_header);
692 }
693
694 template <typename ElfTypes>
695 typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const {
696   for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
697     Elf_Phdr* program_header = GetProgramHeader(i);
698     if (program_header->p_type == type) {
699       return program_header;
700     }
701   }
702   return nullptr;
703 }
704
705 template <typename ElfTypes>
706 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const {
707   return GetHeader().e_shnum;
708 }
709
710 template <typename ElfTypes>
711 typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const {
712   // Can only access arbitrary sections when we have the whole file, not just program header.
713   // Even if we Load(), it doesn't bring in all the sections.
714   CHECK(!program_header_only_) << file_path_;
715   if (i >= GetSectionHeaderNum()) {
716     return nullptr;  // Failure condition.
717   }
718   uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize);
719   if (section_header >= End()) {
720     return nullptr;  // Failure condition.
721   }
722   return reinterpret_cast<Elf_Shdr*>(section_header);
723 }
724
725 template <typename ElfTypes>
726 typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const {
727   // Can only access arbitrary sections when we have the whole file, not just program header.
728   // We could change this to switch on known types if they were detected during loading.
729   CHECK(!program_header_only_) << file_path_;
730   for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
731     Elf_Shdr* section_header = GetSectionHeader(i);
732     if (section_header->sh_type == type) {
733       return section_header;
734     }
735   }
736   return nullptr;
737 }
738
739 // from bionic
740 static unsigned elfhash(const char *_name) {
741   const unsigned char *name = (const unsigned char *) _name;
742   unsigned h = 0, g;
743
744   while (*name) {
745     h = (h << 4) + *name++;
746     g = h & 0xf0000000;
747     h ^= g;
748     h ^= g >> 24;
749   }
750   return h;
751 }
752
753 template <typename ElfTypes>
754 typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const {
755   return GetSectionHeader(GetHeader().e_shstrndx);
756 }
757
758 template <typename ElfTypes>
759 const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress(
760     const std::string& symbol_name) const {
761   // Check that we have a hash section.
762   if (GetHashSectionStart() == nullptr) {
763     return nullptr;  // Failure condition.
764   }
765   const Elf_Sym* sym = FindDynamicSymbol(symbol_name);
766   if (sym != nullptr) {
767     // TODO: we need to change this to calculate base_address_ in ::Open,
768     // otherwise it will be wrongly 0 if ::Load has not yet been called.
769     return base_address_ + sym->st_value;
770   } else {
771     return nullptr;
772   }
773 }
774
775 // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section.
776 template <typename ElfTypes>
777 const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol(
778     const std::string& symbol_name) const {
779   if (GetHashBucketNum() == 0) {
780     // No dynamic symbols at all.
781     return nullptr;
782   }
783   Elf_Word hash = elfhash(symbol_name.c_str());
784   Elf_Word bucket_index = hash % GetHashBucketNum();
785   bool ok;
786   Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok);
787   if (!ok) {
788     return nullptr;
789   }
790   while (symbol_and_chain_index != 0 /* STN_UNDEF */) {
791     Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index);
792     if (symbol == nullptr) {
793       return nullptr;  // Failure condition.
794     }
795     const char* name = GetString(SHT_DYNSYM, symbol->st_name);
796     if (symbol_name == name) {
797       return symbol;
798     }
799     symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok);
800     if (!ok) {
801       return nullptr;
802     }
803   }
804   return nullptr;
805 }
806
807 template <typename ElfTypes>
808 bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) {
809   return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM));
810 }
811
812 template <typename ElfTypes>
813 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const {
814   CHECK(IsSymbolSectionType(section_header.sh_type))
815       << file_path_ << " " << section_header.sh_type;
816   CHECK_NE(0U, section_header.sh_entsize) << file_path_;
817   return section_header.sh_size / section_header.sh_entsize;
818 }
819
820 template <typename ElfTypes>
821 typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const {
822   Elf_Sym* sym_start = GetSymbolSectionStart(section_type);
823   if (sym_start == nullptr) {
824     return nullptr;
825   }
826   return sym_start + i;
827 }
828
829 template <typename ElfTypes>
830 typename ElfFileImpl<ElfTypes>::SymbolTable**
831 ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) {
832   CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
833   switch (section_type) {
834     case SHT_SYMTAB: {
835       return &symtab_symbol_table_;
836     }
837     case SHT_DYNSYM: {
838       return &dynsym_symbol_table_;
839     }
840     default: {
841       LOG(FATAL) << section_type;
842       return nullptr;
843     }
844   }
845 }
846
847 template <typename ElfTypes>
848 typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName(
849     Elf_Word section_type, const std::string& symbol_name, bool build_map) {
850   CHECK(!program_header_only_) << file_path_;
851   CHECK(IsSymbolSectionType(section_type)) << file_path_ << " " << section_type;
852
853   SymbolTable** symbol_table = GetSymbolTable(section_type);
854   if (*symbol_table != nullptr || build_map) {
855     if (*symbol_table == nullptr) {
856       DCHECK(build_map);
857       *symbol_table = new SymbolTable;
858       Elf_Shdr* symbol_section = FindSectionByType(section_type);
859       if (symbol_section == nullptr) {
860         return nullptr;  // Failure condition.
861       }
862       Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
863       if (string_section == nullptr) {
864         return nullptr;  // Failure condition.
865       }
866       for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
867         Elf_Sym* symbol = GetSymbol(section_type, i);
868         if (symbol == nullptr) {
869           return nullptr;  // Failure condition.
870         }
871         unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr))
872                              ? ELF64_ST_TYPE(symbol->st_info)
873                              : ELF32_ST_TYPE(symbol->st_info);
874         if (type == STT_NOTYPE) {
875           continue;
876         }
877         const char* name = GetString(*string_section, symbol->st_name);
878         if (name == nullptr) {
879           continue;
880         }
881         std::pair<typename SymbolTable::iterator, bool> result =
882             (*symbol_table)->insert(std::make_pair(name, symbol));
883         if (!result.second) {
884           // If a duplicate, make sure it has the same logical value. Seen on x86.
885           if ((symbol->st_value != result.first->second->st_value) ||
886               (symbol->st_size != result.first->second->st_size) ||
887               (symbol->st_info != result.first->second->st_info) ||
888               (symbol->st_other != result.first->second->st_other) ||
889               (symbol->st_shndx != result.first->second->st_shndx)) {
890             return nullptr;  // Failure condition.
891           }
892         }
893       }
894     }
895     CHECK(*symbol_table != nullptr);
896     typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name);
897     if (it == (*symbol_table)->end()) {
898       return nullptr;
899     }
900     return it->second;
901   }
902
903   // Fall back to linear search
904   Elf_Shdr* symbol_section = FindSectionByType(section_type);
905   if (symbol_section == nullptr) {
906     return nullptr;
907   }
908   Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link);
909   if (string_section == nullptr) {
910     return nullptr;
911   }
912   for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
913     Elf_Sym* symbol = GetSymbol(section_type, i);
914     if (symbol == nullptr) {
915       return nullptr;  // Failure condition.
916     }
917     const char* name = GetString(*string_section, symbol->st_name);
918     if (name == nullptr) {
919       continue;
920     }
921     if (symbol_name == name) {
922       return symbol;
923     }
924   }
925   return nullptr;
926 }
927
928 template <typename ElfTypes>
929 typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress(
930     Elf_Word section_type, const std::string& symbol_name, bool build_map) {
931   Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map);
932   if (symbol == nullptr) {
933     return 0;
934   }
935   return symbol->st_value;
936 }
937
938 template <typename ElfTypes>
939 const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section,
940                                              Elf_Word i) const {
941   CHECK(!program_header_only_) << file_path_;
942   // TODO: remove this static_cast from enum when using -std=gnu++0x
943   if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) {
944     return nullptr;  // Failure condition.
945   }
946   if (i >= string_section.sh_size) {
947     return nullptr;
948   }
949   if (i == 0) {
950     return nullptr;
951   }
952   uint8_t* strings = Begin() + string_section.sh_offset;
953   uint8_t* string = strings + i;
954   if (string >= End()) {
955     return nullptr;
956   }
957   return reinterpret_cast<const char*>(string);
958 }
959
960 template <typename ElfTypes>
961 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const {
962   return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn);
963 }
964
965 template <typename ElfTypes>
966 typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const {
967   CHECK_LT(i, GetDynamicNum()) << file_path_;
968   return *(GetDynamicSectionStart() + i);
969 }
970
971 template <typename ElfTypes>
972 typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const {
973   for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
974     Elf_Dyn* dyn = &GetDynamic(i);
975     if (dyn->d_tag == type) {
976       return dyn;
977     }
978   }
979   return nullptr;
980 }
981
982 template <typename ElfTypes>
983 typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const {
984   Elf_Dyn* dyn = FindDynamicByType(type);
985   if (dyn == nullptr) {
986     return 0;
987   } else {
988     return dyn->d_un.d_val;
989   }
990 }
991
992 template <typename ElfTypes>
993 typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const {
994   CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
995   return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset);
996 }
997
998 template <typename ElfTypes>
999 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const {
1000   CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1001   CHECK_NE(0U, section_header.sh_entsize) << file_path_;
1002   return section_header.sh_size / section_header.sh_entsize;
1003 }
1004
1005 template <typename ElfTypes>
1006 typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const {
1007   CHECK(SHT_REL == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1008   CHECK_LT(i, GetRelNum(section_header)) << file_path_;
1009   return *(GetRelSectionStart(section_header) + i);
1010 }
1011
1012 template <typename ElfTypes>
1013 typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const {
1014   CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1015   return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset);
1016 }
1017
1018 template <typename ElfTypes>
1019 typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const {
1020   CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1021   return section_header.sh_size / section_header.sh_entsize;
1022 }
1023
1024 template <typename ElfTypes>
1025 typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const {
1026   CHECK(SHT_RELA == section_header.sh_type) << file_path_ << " " << section_header.sh_type;
1027   CHECK_LT(i, GetRelaNum(section_header)) << file_path_;
1028   return *(GetRelaSectionStart(section_header) + i);
1029 }
1030
1031 // Base on bionic phdr_table_get_load_size
1032 template <typename ElfTypes>
1033 bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const {
1034   Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1);
1035   Elf_Addr max_vaddr = 0u;
1036   for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1037     Elf_Phdr* program_header = GetProgramHeader(i);
1038     if (program_header->p_type != PT_LOAD) {
1039       continue;
1040     }
1041     Elf_Addr begin_vaddr = program_header->p_vaddr;
1042     if (begin_vaddr < min_vaddr) {
1043        min_vaddr = begin_vaddr;
1044     }
1045     Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz;
1046     if (UNLIKELY(begin_vaddr > end_vaddr)) {
1047       std::ostringstream oss;
1048       oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex
1049           << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr
1050           << " in ELF file \"" << file_path_ << "\"";
1051       *error_msg = oss.str();
1052       *size = static_cast<size_t>(-1);
1053       return false;
1054     }
1055     if (end_vaddr > max_vaddr) {
1056       max_vaddr = end_vaddr;
1057     }
1058   }
1059   min_vaddr = RoundDown(min_vaddr, kPageSize);
1060   max_vaddr = RoundUp(max_vaddr, kPageSize);
1061   CHECK_LT(min_vaddr, max_vaddr) << file_path_;
1062   Elf_Addr loaded_size = max_vaddr - min_vaddr;
1063   // Check that the loaded_size fits in size_t.
1064   if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) {
1065     std::ostringstream oss;
1066     oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x"
1067         << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_path_ << "\"";
1068     *error_msg = oss.str();
1069     *size = static_cast<size_t>(-1);
1070     return false;
1071   }
1072   *size = loaded_size;
1073   return true;
1074 }
1075
1076 template <typename ElfTypes>
1077 bool ElfFileImpl<ElfTypes>::Load(File* file,
1078                                  bool executable,
1079                                  bool low_4gb,
1080                                  std::string* error_msg) {
1081   CHECK(program_header_only_) << file->GetPath();
1082
1083   if (executable) {
1084     InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags);
1085     if (elf_ISA != kRuntimeISA) {
1086       std::ostringstream oss;
1087       oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA;
1088       *error_msg = oss.str();
1089       return false;
1090     }
1091   }
1092
1093   bool reserved = false;
1094   for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1095     Elf_Phdr* program_header = GetProgramHeader(i);
1096     if (program_header == nullptr) {
1097       *error_msg = StringPrintf("No program header for entry %d in ELF file %s.",
1098                                 i, file->GetPath().c_str());
1099       return false;
1100     }
1101
1102     // Record .dynamic header information for later use
1103     if (program_header->p_type == PT_DYNAMIC) {
1104       dynamic_program_header_ = program_header;
1105       continue;
1106     }
1107
1108     // Not something to load, move on.
1109     if (program_header->p_type != PT_LOAD) {
1110       continue;
1111     }
1112
1113     // Found something to load.
1114
1115     // Before load the actual segments, reserve a contiguous chunk
1116     // of required size and address for all segments, but with no
1117     // permissions. We'll then carve that up with the proper
1118     // permissions as we load the actual segments. If p_vaddr is
1119     // non-zero, the segments require the specific address specified,
1120     // which either was specified in the file because we already set
1121     // base_address_ after the first zero segment).
1122     int64_t temp_file_length = file->GetLength();
1123     if (temp_file_length < 0) {
1124       errno = -temp_file_length;
1125       *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s",
1126                                 file->GetPath().c_str(), file->Fd(), strerror(errno));
1127       return false;
1128     }
1129     size_t file_length = static_cast<size_t>(temp_file_length);
1130     if (!reserved) {
1131       uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr);
1132       uint8_t* reserve_base_override = reserve_base;
1133       // Override the base (e.g. when compiling with --compile-pic)
1134       if (requested_base_ != nullptr) {
1135         reserve_base_override = requested_base_;
1136       }
1137       std::string reservation_name("ElfFile reservation for ");
1138       reservation_name += file->GetPath();
1139       size_t loaded_size;
1140       if (!GetLoadedSize(&loaded_size, error_msg)) {
1141         DCHECK(!error_msg->empty());
1142         return false;
1143       }
1144       std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(),
1145                                                            reserve_base_override,
1146                                                            loaded_size,
1147                                                            PROT_NONE,
1148                                                            low_4gb,
1149                                                            false,
1150                                                            error_msg));
1151       if (reserve.get() == nullptr) {
1152         *error_msg = StringPrintf("Failed to allocate %s: %s",
1153                                   reservation_name.c_str(), error_msg->c_str());
1154         return false;
1155       }
1156       reserved = true;
1157
1158       // Base address is the difference of actual mapped location and the p_vaddr
1159       base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin())
1160                        - reinterpret_cast<uintptr_t>(reserve_base));
1161       // By adding the p_vaddr of a section/symbol to base_address_ we will always get the
1162       // dynamic memory address of where that object is actually mapped
1163       //
1164       // TODO: base_address_ needs to be calculated in ::Open, otherwise
1165       // FindDynamicSymbolAddress returns the wrong values until Load is called.
1166       segments_.push_back(reserve.release());
1167     }
1168     // empty segment, nothing to map
1169     if (program_header->p_memsz == 0) {
1170       continue;
1171     }
1172     uint8_t* p_vaddr = base_address_ + program_header->p_vaddr;
1173     int prot = 0;
1174     if (executable && ((program_header->p_flags & PF_X) != 0)) {
1175       prot |= PROT_EXEC;
1176     }
1177     if ((program_header->p_flags & PF_W) != 0) {
1178       prot |= PROT_WRITE;
1179     }
1180     if ((program_header->p_flags & PF_R) != 0) {
1181       prot |= PROT_READ;
1182     }
1183     int flags = 0;
1184     if (writable_) {
1185       prot |= PROT_WRITE;
1186       flags |= MAP_SHARED;
1187     } else {
1188       flags |= MAP_PRIVATE;
1189     }
1190     if (program_header->p_filesz > program_header->p_memsz) {
1191       *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s",
1192                                 static_cast<uint64_t>(program_header->p_filesz),
1193                                 static_cast<uint64_t>(program_header->p_memsz),
1194                                 file->GetPath().c_str());
1195       return false;
1196     }
1197     if (program_header->p_filesz < program_header->p_memsz &&
1198         !IsAligned<kPageSize>(program_header->p_filesz)) {
1199       *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64
1200                                 " < %" PRIu64 "): %s",
1201                                 static_cast<uint64_t>(program_header->p_filesz),
1202                                 static_cast<uint64_t>(program_header->p_memsz),
1203                                 file->GetPath().c_str());
1204       return false;
1205     }
1206     if (file_length < (program_header->p_offset + program_header->p_filesz)) {
1207       *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment "
1208                                 "%d of %" PRIu64 " bytes: '%s'", file_length, i,
1209                                 static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz),
1210                                 file->GetPath().c_str());
1211       return false;
1212     }
1213     if (program_header->p_filesz != 0u) {
1214       std::unique_ptr<MemMap> segment(
1215           MemMap::MapFileAtAddress(p_vaddr,
1216                                    program_header->p_filesz,
1217                                    prot,
1218                                    flags,
1219                                    file->Fd(),
1220                                    program_header->p_offset,
1221                                    /*low4_gb*/false,
1222                                    /*reuse*/true,  // implies MAP_FIXED
1223                                    file->GetPath().c_str(),
1224                                    error_msg));
1225       if (segment.get() == nullptr) {
1226         *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s",
1227                                   i, file->GetPath().c_str(), error_msg->c_str());
1228         return false;
1229       }
1230       if (segment->Begin() != p_vaddr) {
1231         *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, "
1232                                   "instead mapped to %p",
1233                                   i, file->GetPath().c_str(), p_vaddr, segment->Begin());
1234         return false;
1235       }
1236       segments_.push_back(segment.release());
1237     }
1238     if (program_header->p_filesz < program_header->p_memsz) {
1239       std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s",
1240                                       static_cast<uint64_t>(i), file->GetPath().c_str());
1241       std::unique_ptr<MemMap> segment(
1242           MemMap::MapAnonymous(name.c_str(),
1243                                p_vaddr + program_header->p_filesz,
1244                                program_header->p_memsz - program_header->p_filesz,
1245                                prot, false, true /* reuse */, error_msg));
1246       if (segment == nullptr) {
1247         *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s",
1248                                   i, file->GetPath().c_str(), error_msg->c_str());
1249         return false;
1250       }
1251       if (segment->Begin() != p_vaddr) {
1252         *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s "
1253                                   "at expected address %p, instead mapped to %p",
1254                                   i, file->GetPath().c_str(), p_vaddr, segment->Begin());
1255         return false;
1256       }
1257       segments_.push_back(segment.release());
1258     }
1259   }
1260
1261   // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash
1262   uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr;
1263   if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) {
1264     *error_msg = StringPrintf("dynamic section address invalid in ELF file %s",
1265                               file->GetPath().c_str());
1266     return false;
1267   }
1268   dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr);
1269
1270   for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1271     Elf_Dyn& elf_dyn = GetDynamic(i);
1272     uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr;
1273     switch (elf_dyn.d_tag) {
1274       case DT_HASH: {
1275         if (!ValidPointer(d_ptr)) {
1276           *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1277                                     d_ptr, file->GetPath().c_str());
1278           return false;
1279         }
1280         hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr);
1281         break;
1282       }
1283       case DT_STRTAB: {
1284         if (!ValidPointer(d_ptr)) {
1285           *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1286                                     d_ptr, file->GetPath().c_str());
1287           return false;
1288         }
1289         dynstr_section_start_ = reinterpret_cast<char*>(d_ptr);
1290         break;
1291       }
1292       case DT_SYMTAB: {
1293         if (!ValidPointer(d_ptr)) {
1294           *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s",
1295                                     d_ptr, file->GetPath().c_str());
1296           return false;
1297         }
1298         dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr);
1299         break;
1300       }
1301       case DT_NULL: {
1302         if (GetDynamicNum() != i+1) {
1303           *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, "
1304                                     "expected %d as implied by size of PT_DYNAMIC segment in %s",
1305                                     i + 1, GetDynamicNum(), file->GetPath().c_str());
1306           return false;
1307         }
1308         break;
1309       }
1310     }
1311   }
1312
1313   // Check for the existence of some sections.
1314   if (!CheckSectionsExist(file, error_msg)) {
1315     return false;
1316   }
1317
1318   return true;
1319 }
1320
1321 template <typename ElfTypes>
1322 bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const {
1323   for (size_t i = 0; i < segments_.size(); ++i) {
1324     const MemMap* segment = segments_[i];
1325     if (segment->Begin() <= start && start < segment->End()) {
1326       return true;
1327     }
1328   }
1329   return false;
1330 }
1331
1332
1333 template <typename ElfTypes>
1334 typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName(
1335     const std::string& name) const {
1336   CHECK(!program_header_only_);
1337   Elf_Shdr* shstrtab_sec = GetSectionNameStringSection();
1338   if (shstrtab_sec == nullptr) {
1339     return nullptr;
1340   }
1341   for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) {
1342     Elf_Shdr* shdr = GetSectionHeader(i);
1343     if (shdr == nullptr) {
1344       return nullptr;
1345     }
1346     const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name);
1347     if (sec_name == nullptr) {
1348       continue;
1349     }
1350     if (name == sec_name) {
1351       return shdr;
1352     }
1353   }
1354   return nullptr;
1355 }
1356
1357 template <typename ElfTypes>
1358 bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) {
1359   if (base_address_delta == 0) {
1360     return true;
1361   }
1362   return ApplyOatPatchesTo(".debug_frame", base_address_delta) &&
1363          ApplyOatPatchesTo(".debug_info", base_address_delta) &&
1364          ApplyOatPatchesTo(".debug_line", base_address_delta);
1365 }
1366
1367 template <typename ElfTypes>
1368 bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo(
1369     const char* target_section_name, Elf_Addr delta) {
1370   auto target_section = FindSectionByName(target_section_name);
1371   if (target_section == nullptr) {
1372     return true;
1373   }
1374   std::string patches_name = target_section_name + std::string(".oat_patches");
1375   auto patches_section = FindSectionByName(patches_name.c_str());
1376   if (patches_section == nullptr) {
1377     LOG(ERROR) << patches_name << " section not found.";
1378     return false;
1379   }
1380   if (patches_section->sh_type != SHT_OAT_PATCH) {
1381     LOG(ERROR) << "Unexpected type of " << patches_name;
1382     return false;
1383   }
1384   ApplyOatPatches(
1385       Begin() + patches_section->sh_offset,
1386       Begin() + patches_section->sh_offset + patches_section->sh_size,
1387       delta,
1388       Begin() + target_section->sh_offset,
1389       Begin() + target_section->sh_offset + target_section->sh_size);
1390   return true;
1391 }
1392
1393 // Apply LEB128 encoded patches to given section.
1394 template <typename ElfTypes>
1395 void ElfFileImpl<ElfTypes>::ApplyOatPatches(
1396     const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta,
1397     uint8_t* to_patch, const uint8_t* to_patch_end) {
1398   typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress;
1399   while (patches < patches_end) {
1400     to_patch += DecodeUnsignedLeb128(&patches);
1401     DCHECK_LE(patches, patches_end) << "Unexpected end of patch list.";
1402     DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section.";
1403     *reinterpret_cast<UnalignedAddress*>(to_patch) += delta;
1404   }
1405 }
1406
1407 template <typename ElfTypes>
1408 bool ElfFileImpl<ElfTypes>::Strip(File* file, std::string* error_msg) {
1409   // ELF files produced by MCLinker look roughly like this
1410   //
1411   // +------------+
1412   // | Elf_Ehdr   | contains number of Elf_Shdr and offset to first
1413   // +------------+
1414   // | Elf_Phdr   | program headers
1415   // | Elf_Phdr   |
1416   // | ...        |
1417   // | Elf_Phdr   |
1418   // +------------+
1419   // | section    | mixture of needed and unneeded sections
1420   // +------------+
1421   // | section    |
1422   // +------------+
1423   // | ...        |
1424   // +------------+
1425   // | section    |
1426   // +------------+
1427   // | Elf_Shdr   | section headers
1428   // | Elf_Shdr   |
1429   // | ...        | contains offset to section start
1430   // | Elf_Shdr   |
1431   // +------------+
1432   //
1433   // To strip:
1434   // - leave the Elf_Ehdr and Elf_Phdr values in place.
1435   // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep
1436   // - move the sections are keeping up to fill in gaps of sections we want to strip
1437   // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr
1438   // - truncate rest of file
1439   //
1440
1441   std::vector<Elf_Shdr> section_headers;
1442   std::vector<Elf_Word> section_headers_original_indexes;
1443   section_headers.reserve(GetSectionHeaderNum());
1444
1445
1446   Elf_Shdr* string_section = GetSectionNameStringSection();
1447   CHECK(string_section != nullptr);
1448   for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1449     Elf_Shdr* sh = GetSectionHeader(i);
1450     CHECK(sh != nullptr);
1451     const char* name = GetString(*string_section, sh->sh_name);
1452     if (name == nullptr) {
1453       CHECK_EQ(0U, i);
1454       section_headers.push_back(*sh);
1455       section_headers_original_indexes.push_back(0);
1456       continue;
1457     }
1458     if (android::base::StartsWith(name, ".debug")
1459         || (strcmp(name, ".strtab") == 0)
1460         || (strcmp(name, ".symtab") == 0)) {
1461       continue;
1462     }
1463     section_headers.push_back(*sh);
1464     section_headers_original_indexes.push_back(i);
1465   }
1466   CHECK_NE(0U, section_headers.size());
1467   CHECK_EQ(section_headers.size(), section_headers_original_indexes.size());
1468
1469   // section 0 is the null section, sections start at offset of first section
1470   CHECK(GetSectionHeader(1) != nullptr);
1471   Elf_Off offset = GetSectionHeader(1)->sh_offset;
1472   for (size_t i = 1; i < section_headers.size(); i++) {
1473     Elf_Shdr& new_sh = section_headers[i];
1474     Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]);
1475     CHECK(old_sh != nullptr);
1476     CHECK_EQ(new_sh.sh_name, old_sh->sh_name);
1477     if (old_sh->sh_addralign > 1) {
1478       offset = RoundUp(offset, old_sh->sh_addralign);
1479     }
1480     if (old_sh->sh_offset == offset) {
1481       // already in place
1482       offset += old_sh->sh_size;
1483       continue;
1484     }
1485     // shift section earlier
1486     memmove(Begin() + offset,
1487             Begin() + old_sh->sh_offset,
1488             old_sh->sh_size);
1489     new_sh.sh_offset = offset;
1490     offset += old_sh->sh_size;
1491   }
1492
1493   Elf_Off shoff = offset;
1494   size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr);
1495   memcpy(Begin() + offset, &section_headers[0], section_headers_size_in_bytes);
1496   offset += section_headers_size_in_bytes;
1497
1498   GetHeader().e_shnum = section_headers.size();
1499   GetHeader().e_shoff = shoff;
1500   int result = ftruncate(file->Fd(), offset);
1501   if (result != 0) {
1502     *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
1503                               file->GetPath().c_str(), strerror(errno));
1504     return false;
1505   }
1506   return true;
1507 }
1508
1509 static const bool DEBUG_FIXUP = false;
1510
1511 template <typename ElfTypes>
1512 bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) {
1513   if (!FixupDynamic(base_address)) {
1514     LOG(WARNING) << "Failed to fixup .dynamic in " << file_path_;
1515     return false;
1516   }
1517   if (!FixupSectionHeaders(base_address)) {
1518     LOG(WARNING) << "Failed to fixup section headers in " << file_path_;
1519     return false;
1520   }
1521   if (!FixupProgramHeaders(base_address)) {
1522     LOG(WARNING) << "Failed to fixup program headers in " << file_path_;
1523     return false;
1524   }
1525   if (!FixupSymbols(base_address, true)) {
1526     LOG(WARNING) << "Failed to fixup .dynsym in " << file_path_;
1527     return false;
1528   }
1529   if (!FixupSymbols(base_address, false)) {
1530     LOG(WARNING) << "Failed to fixup .symtab in " << file_path_;
1531     return false;
1532   }
1533   if (!FixupRelocations(base_address)) {
1534     LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_path_;
1535     return false;
1536   }
1537   static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision.");
1538   if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) {
1539     LOG(WARNING) << "Failed to fixup debug sections in " << file_path_;
1540     return false;
1541   }
1542   return true;
1543 }
1544
1545 template <typename ElfTypes>
1546 bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) {
1547   for (Elf_Word i = 0; i < GetDynamicNum(); i++) {
1548     Elf_Dyn& elf_dyn = GetDynamic(i);
1549     Elf_Word d_tag = elf_dyn.d_tag;
1550     if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) {
1551       Elf_Addr d_ptr = elf_dyn.d_un.d_ptr;
1552       if (DEBUG_FIXUP) {
1553         LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1554                                   file_path_.c_str(), i,
1555                                   static_cast<uint64_t>(d_ptr),
1556                                   static_cast<uint64_t>(d_ptr + base_address));
1557       }
1558       d_ptr += base_address;
1559       elf_dyn.d_un.d_ptr = d_ptr;
1560     }
1561   }
1562   return true;
1563 }
1564
1565 template <typename ElfTypes>
1566 bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) {
1567   for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1568     Elf_Shdr* sh = GetSectionHeader(i);
1569     CHECK(sh != nullptr);
1570     // 0 implies that the section will not exist in the memory of the process
1571     if (sh->sh_addr == 0) {
1572       continue;
1573     }
1574     if (DEBUG_FIXUP) {
1575       LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1576                                 file_path_.c_str(), i,
1577                                 static_cast<uint64_t>(sh->sh_addr),
1578                                 static_cast<uint64_t>(sh->sh_addr + base_address));
1579     }
1580     sh->sh_addr += base_address;
1581   }
1582   return true;
1583 }
1584
1585 template <typename ElfTypes>
1586 bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) {
1587   // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now.
1588   for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) {
1589     Elf_Phdr* ph = GetProgramHeader(i);
1590     CHECK(ph != nullptr);
1591     CHECK_EQ(ph->p_vaddr, ph->p_paddr) << file_path_ << " i=" << i;
1592     CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
1593         << file_path_ << " i=" << i;
1594     if (DEBUG_FIXUP) {
1595       LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1596                                 file_path_.c_str(), i,
1597                                 static_cast<uint64_t>(ph->p_vaddr),
1598                                 static_cast<uint64_t>(ph->p_vaddr + base_address));
1599     }
1600     ph->p_vaddr += base_address;
1601     ph->p_paddr += base_address;
1602     CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1))))
1603         << file_path_ << " i=" << i;
1604   }
1605   return true;
1606 }
1607
1608 template <typename ElfTypes>
1609 bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) {
1610   Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB;
1611   // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile
1612   Elf_Shdr* symbol_section = FindSectionByType(section_type);
1613   if (symbol_section == nullptr) {
1614     // file is missing optional .symtab
1615     CHECK(!dynamic) << file_path_;
1616     return true;
1617   }
1618   for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) {
1619     Elf_Sym* symbol = GetSymbol(section_type, i);
1620     CHECK(symbol != nullptr);
1621     if (symbol->st_value != 0) {
1622       if (DEBUG_FIXUP) {
1623         LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1624                                   file_path_.c_str(), i,
1625                                   static_cast<uint64_t>(symbol->st_value),
1626                                   static_cast<uint64_t>(symbol->st_value + base_address));
1627       }
1628       symbol->st_value += base_address;
1629     }
1630   }
1631   return true;
1632 }
1633
1634 template <typename ElfTypes>
1635 bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) {
1636   for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) {
1637     Elf_Shdr* sh = GetSectionHeader(i);
1638     CHECK(sh != nullptr);
1639     if (sh->sh_type == SHT_REL) {
1640       for (uint32_t j = 0; j < GetRelNum(*sh); j++) {
1641         Elf_Rel& rel = GetRel(*sh, j);
1642         if (DEBUG_FIXUP) {
1643           LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1644                                     file_path_.c_str(), j,
1645                                     static_cast<uint64_t>(rel.r_offset),
1646                                     static_cast<uint64_t>(rel.r_offset + base_address));
1647         }
1648         rel.r_offset += base_address;
1649       }
1650     } else if (sh->sh_type == SHT_RELA) {
1651       for (uint32_t j = 0; j < GetRelaNum(*sh); j++) {
1652         Elf_Rela& rela = GetRela(*sh, j);
1653         if (DEBUG_FIXUP) {
1654           LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64,
1655                                     file_path_.c_str(), j,
1656                                     static_cast<uint64_t>(rela.r_offset),
1657                                     static_cast<uint64_t>(rela.r_offset + base_address));
1658         }
1659         rela.r_offset += base_address;
1660       }
1661     }
1662   }
1663   return true;
1664 }
1665
1666 // Explicit instantiations
1667 template class ElfFileImpl<ElfTypes32>;
1668 template class ElfFileImpl<ElfTypes64>;
1669
1670 ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) {
1671 }
1672
1673 ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) {
1674 }
1675
1676 ElfFile::~ElfFile() {
1677   // Should never have 32 and 64-bit impls.
1678   CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr);
1679 }
1680
1681 ElfFile* ElfFile::Open(File* file,
1682                        bool writable,
1683                        bool program_header_only,
1684                        bool low_4gb,
1685                        std::string* error_msg,
1686                        uint8_t* requested_base) {
1687   if (file->GetLength() < EI_NIDENT) {
1688     *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1689                               file->GetPath().c_str());
1690     return nullptr;
1691   }
1692   std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1693                                               PROT_READ,
1694                                               MAP_PRIVATE,
1695                                               file->Fd(),
1696                                               0,
1697                                               low_4gb,
1698                                               file->GetPath().c_str(),
1699                                               error_msg));
1700   if (map == nullptr && map->Size() != EI_NIDENT) {
1701     return nullptr;
1702   }
1703   uint8_t* header = map->Begin();
1704   if (header[EI_CLASS] == ELFCLASS64) {
1705     ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1706                                                        writable,
1707                                                        program_header_only,
1708                                                        low_4gb,
1709                                                        error_msg,
1710                                                        requested_base);
1711     if (elf_file_impl == nullptr) {
1712       return nullptr;
1713     }
1714     return new ElfFile(elf_file_impl);
1715   } else if (header[EI_CLASS] == ELFCLASS32) {
1716     ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1717                                                        writable,
1718                                                        program_header_only,
1719                                                        low_4gb,
1720                                                        error_msg,
1721                                                        requested_base);
1722     if (elf_file_impl == nullptr) {
1723       return nullptr;
1724     }
1725     return new ElfFile(elf_file_impl);
1726   } else {
1727     *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1728                               ELFCLASS32, ELFCLASS64,
1729                               file->GetPath().c_str(),
1730                               header[EI_CLASS]);
1731     return nullptr;
1732   }
1733 }
1734
1735 ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) {
1736   // low_4gb support not required for this path.
1737   constexpr bool low_4gb = false;
1738   if (file->GetLength() < EI_NIDENT) {
1739     *error_msg = StringPrintf("File %s is too short to be a valid ELF file",
1740                               file->GetPath().c_str());
1741     return nullptr;
1742   }
1743   std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT,
1744                                               PROT_READ,
1745                                               MAP_PRIVATE,
1746                                               file->Fd(),
1747                                               0,
1748                                               low_4gb,
1749                                               file->GetPath().c_str(),
1750                                               error_msg));
1751   if (map == nullptr && map->Size() != EI_NIDENT) {
1752     return nullptr;
1753   }
1754   uint8_t* header = map->Begin();
1755   if (header[EI_CLASS] == ELFCLASS64) {
1756     ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file,
1757                                                        mmap_prot,
1758                                                        mmap_flags,
1759                                                        low_4gb,
1760                                                        error_msg);
1761     if (elf_file_impl == nullptr) {
1762       return nullptr;
1763     }
1764     return new ElfFile(elf_file_impl);
1765   } else if (header[EI_CLASS] == ELFCLASS32) {
1766     ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file,
1767                                                        mmap_prot,
1768                                                        mmap_flags,
1769                                                        low_4gb,
1770                                                        error_msg);
1771     if (elf_file_impl == nullptr) {
1772       return nullptr;
1773     }
1774     return new ElfFile(elf_file_impl);
1775   } else {
1776     *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d",
1777                               ELFCLASS32, ELFCLASS64,
1778                               file->GetPath().c_str(),
1779                               header[EI_CLASS]);
1780     return nullptr;
1781   }
1782 }
1783
1784 #define DELEGATE_TO_IMPL(func, ...) \
1785   if (elf64_.get() != nullptr) { \
1786     return elf64_->func(__VA_ARGS__); \
1787   } else { \
1788     DCHECK(elf32_.get() != nullptr); \
1789     return elf32_->func(__VA_ARGS__); \
1790   }
1791
1792 bool ElfFile::Load(File* file, bool executable, bool low_4gb, std::string* error_msg) {
1793   DELEGATE_TO_IMPL(Load, file, executable, low_4gb, error_msg);
1794 }
1795
1796 const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const {
1797   DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name);
1798 }
1799
1800 size_t ElfFile::Size() const {
1801   DELEGATE_TO_IMPL(Size);
1802 }
1803
1804 uint8_t* ElfFile::Begin() const {
1805   DELEGATE_TO_IMPL(Begin);
1806 }
1807
1808 uint8_t* ElfFile::End() const {
1809   DELEGATE_TO_IMPL(End);
1810 }
1811
1812 const std::string& ElfFile::GetFilePath() const {
1813   DELEGATE_TO_IMPL(GetFilePath);
1814 }
1815
1816 bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset,
1817                                       uint64_t* size) const {
1818   if (elf32_.get() == nullptr) {
1819     CHECK(elf64_.get() != nullptr);
1820
1821     Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name);
1822     if (shdr == nullptr) {
1823       return false;
1824     }
1825     if (offset != nullptr) {
1826       *offset = shdr->sh_offset;
1827     }
1828     if (size != nullptr) {
1829       *size = shdr->sh_size;
1830     }
1831     return true;
1832   } else {
1833     Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name);
1834     if (shdr == nullptr) {
1835       return false;
1836     }
1837     if (offset != nullptr) {
1838       *offset = shdr->sh_offset;
1839     }
1840     if (size != nullptr) {
1841       *size = shdr->sh_size;
1842     }
1843     return true;
1844   }
1845 }
1846
1847 bool ElfFile::HasSection(const std::string& name) const {
1848   if (elf64_.get() != nullptr) {
1849     return elf64_->FindSectionByName(name) != nullptr;
1850   } else {
1851     return elf32_->FindSectionByName(name) != nullptr;
1852   }
1853 }
1854
1855 uint64_t ElfFile::FindSymbolAddress(unsigned section_type,
1856                                     const std::string& symbol_name,
1857                                     bool build_map) {
1858   DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map);
1859 }
1860
1861 bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const {
1862   DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg);
1863 }
1864
1865 bool ElfFile::Strip(File* file, std::string* error_msg) {
1866   std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, /*low_4gb*/false, error_msg));
1867   if (elf_file.get() == nullptr) {
1868     return false;
1869   }
1870
1871   if (elf_file->elf64_.get() != nullptr) {
1872     return elf_file->elf64_->Strip(file, error_msg);
1873   } else {
1874     return elf_file->elf32_->Strip(file, error_msg);
1875   }
1876 }
1877
1878 bool ElfFile::Fixup(uint64_t base_address) {
1879   if (elf64_.get() != nullptr) {
1880     return elf64_->Fixup(static_cast<Elf64_Addr>(base_address));
1881   } else {
1882     DCHECK(elf32_.get() != nullptr);
1883     CHECK(IsUint<32>(base_address)) << std::hex << base_address;
1884     return elf32_->Fixup(static_cast<Elf32_Addr>(base_address));
1885   }
1886   DELEGATE_TO_IMPL(Fixup, base_address);
1887 }
1888
1889 }  // namespace art