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 #ifndef ART_RUNTIME_OAT_ELF_FILE_H_ 18 #define ART_RUNTIME_OAT_ELF_FILE_H_ 19 20 #include <cstddef> 21 #include <string> 22 #include <vector> 23 24 #include "android-base/logging.h" 25 #include "base/macros.h" 26 #include "base/mem_map.h" 27 #include "base/os.h" 28 #include "elf/elf_utils.h" 29 30 namespace art HIDDEN { 31 32 template <typename ElfTypes> 33 class ElfFileImpl; 34 35 // Explicitly instantiated in elf_file.cc 36 using ElfFileImpl32 = ElfFileImpl<ElfTypes32>; 37 using ElfFileImpl64 = ElfFileImpl<ElfTypes64>; 38 39 // Used for compile time and runtime for ElfFile access. Because of 40 // the need for use at runtime, cannot directly use LLVM classes such as 41 // ELFObjectFile. 42 class ElfFile { 43 public: 44 // Loads the program headers. 45 // Does not take the ownership of the file. It must stay alive during the `Load` call. 46 static ElfFile* Open(File* file, 47 off_t start, 48 size_t file_length, 49 const std::string& file_location, 50 bool low_4gb, 51 /*out*/ std::string* error_msg); 52 53 static ElfFile* Open(File* file, 54 bool low_4gb, 55 /*out*/ std::string* error_msg); 56 57 virtual ~ElfFile() = default; 58 59 // Load segments into memory based on PT_LOAD program headers. 60 virtual bool Load(bool executable, 61 bool low_4gb, 62 /*inout*/ MemMap* reservation, 63 /*out*/ std::string* error_msg) = 0; 64 65 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name) const = 0; 66 67 // Returns the location of the ELF file, for debugging purposes only. 68 // Note that the location is not necessarily a path to a file on disk. It can also be a zip entry 69 // inside a zip file. GetFileLocation()70 const std::string& GetFileLocation() const { return file_location_; } 71 GetBaseAddress()72 uint8_t* GetBaseAddress() const { return base_address_; } 73 Begin()74 uint8_t* Begin() const { return map_.Begin(); } 75 End()76 uint8_t* End() const { return map_.End(); } 77 Size()78 size_t Size() const { return map_.Size(); } 79 80 virtual bool GetLoadedSize(size_t* size, std::string* error_msg) const = 0; 81 82 virtual size_t GetElfSegmentAlignmentFromFile() const = 0; 83 84 virtual bool Is64Bit() const = 0; 85 86 protected: ElfFile(File * file,off_t start,size_t file_length,const std::string & file_location)87 ElfFile(File* file, off_t start, size_t file_length, const std::string& file_location) 88 : file_(file), start_(start), file_length_(file_length), file_location_(file_location) { 89 CHECK(file != nullptr); 90 } 91 92 File* const file_; 93 const off_t start_; 94 const size_t file_length_; 95 const std::string file_location_; 96 97 // ELF header mapping. If program_header_only_ is false, will 98 // actually point to the entire elf file. 99 MemMap map_; 100 std::vector<MemMap> segments_; 101 102 // Pointer to start of first PT_LOAD program segment after Load() 103 // when program_header_only_ is true. 104 uint8_t* base_address_ = nullptr; 105 106 // The program header should always available but use GetProgramHeadersStart() to be sure. 107 uint8_t* program_headers_start_ = nullptr; 108 109 private: 110 DISALLOW_COPY_AND_ASSIGN(ElfFile); 111 }; 112 113 } // namespace art 114 115 #endif // ART_RUNTIME_OAT_ELF_FILE_H_ 116