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