blob: d2a044e9cf64af3c8a2efb74331ffbe1aa68e568 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ELF_FILE_H_
18#define ART_RUNTIME_ELF_FILE_H_
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019
Brian Carlstrom265091e2013-01-30 14:08:26 -080020#include <map>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080021#include <vector>
22
Brian Carlstrom700c8d32012-11-05 10:42:02 -080023#include "base/unix_file/fd_file.h"
24#include "globals.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000025#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080026#include "mem_map.h"
27#include "os.h"
28#include "UniquePtr.h"
29
30namespace art {
31
Mark Mendellae9fd932014-02-10 16:14:35 -080032// Interface to GDB JIT for backtrace information.
33extern "C" {
34 struct JITCodeEntry;
35}
36
37
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038// Used for compile time and runtime for ElfFile access. Because of
39// the need for use at runtime, cannot directly use LLVM classes such as
40// ELFObjectFile.
41class ElfFile {
42 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070043 static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044 ~ElfFile();
45
46 // Load segments into memory based on PT_LOAD program headers
47
Brian Carlstromc1409452014-02-26 14:06:23 -080048 const File& GetFile() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049 return *file_;
50 }
51
Brian Carlstromc1409452014-02-26 14:06:23 -080052 byte* Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 return map_->Begin();
54 }
55
Brian Carlstromc1409452014-02-26 14:06:23 -080056 byte* End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080057 return map_->End();
58 }
59
60 size_t Size() const {
61 return map_->Size();
62 }
63
Brian Carlstromc1409452014-02-26 14:06:23 -080064 Elf32_Ehdr& GetHeader() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080065
Brian Carlstromc1409452014-02-26 14:06:23 -080066 Elf32_Word GetProgramHeaderNum() const;
67 Elf32_Phdr& GetProgramHeader(Elf32_Word) const;
68 Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069
Brian Carlstromc1409452014-02-26 14:06:23 -080070 Elf32_Word GetSectionHeaderNum() const;
71 Elf32_Shdr& GetSectionHeader(Elf32_Word) const;
72 Elf32_Shdr* FindSectionByType(Elf32_Word type) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080073
Brian Carlstromc1409452014-02-26 14:06:23 -080074 Elf32_Shdr& GetSectionNameStringSection() const;
Brian Carlstrom265091e2013-01-30 14:08:26 -080075
76 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Brian Carlstromc1409452014-02-26 14:06:23 -080077 const byte* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080078
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000079 static bool IsSymbolSectionType(Elf32_Word section_type);
Brian Carlstromc1409452014-02-26 14:06:23 -080080 Elf32_Word GetSymbolNum(Elf32_Shdr&) const;
81 Elf32_Sym& GetSymbol(Elf32_Word section_type, Elf32_Word i) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080082
Brian Carlstrom265091e2013-01-30 14:08:26 -080083 // Find symbol in specified table, returning NULL if it is not found.
84 //
85 // If build_map is true, builds a map to speed repeated access. The
86 // map does not included untyped symbol values (aka STT_NOTYPE)
87 // since they can contain duplicates. If build_map is false, the map
88 // will be used if it was already created. Typically build_map
89 // should be set unless only a small number of symbols will be
90 // looked up.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000091 Elf32_Sym* FindSymbolByName(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -080092 const std::string& symbol_name,
93 bool build_map);
Brian Carlstrom265091e2013-01-30 14:08:26 -080094
95 // Find address of symbol in specified table, returning 0 if it is
96 // not found. See FindSymbolByName for an explanation of build_map.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000097 Elf32_Addr FindSymbolAddress(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -080098 const std::string& symbol_name,
99 bool build_map);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800100
101 // Lookup a string given string section and offset. Returns NULL for
102 // special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -0800103 const char* GetString(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800104
105 // Lookup a string by section type. Returns NULL for special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -0800106 const char* GetString(Elf32_Word section_type, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800107
Brian Carlstromc1409452014-02-26 14:06:23 -0800108 Elf32_Word GetDynamicNum() const;
109 Elf32_Dyn& GetDynamic(Elf32_Word) const;
110 Elf32_Word FindDynamicValueByType(Elf32_Sword type) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800111
Brian Carlstromc1409452014-02-26 14:06:23 -0800112 Elf32_Word GetRelNum(Elf32_Shdr&) const;
113 Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800114
Brian Carlstromc1409452014-02-26 14:06:23 -0800115 Elf32_Word GetRelaNum(Elf32_Shdr&) const;
116 Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800117
118 // Returns the expected size when the file is loaded at runtime
Brian Carlstromc1409452014-02-26 14:06:23 -0800119 size_t GetLoadedSize() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800120
Brian Carlstromf1d34552013-07-12 20:22:23 -0700121 // Load segments into memory based on PT_LOAD program headers.
122 // executable is true at run time, false at compile time.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700123 bool Load(bool executable, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800124
125 private:
Brian Carlstromc1409452014-02-26 14:06:23 -0800126 ElfFile(File* file, bool writable, bool program_header_only);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800127
Brian Carlstromc1409452014-02-26 14:06:23 -0800128 bool Setup(std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800130 bool SetMap(MemMap* map, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800131
Brian Carlstromc1409452014-02-26 14:06:23 -0800132 byte* GetProgramHeadersStart() const;
133 byte* GetSectionHeadersStart() const;
134 Elf32_Phdr& GetDynamicProgramHeader() const;
135 Elf32_Dyn* GetDynamicSectionStart() const;
136 Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type) const;
137 const char* GetStringSectionStart(Elf32_Word section_type) const;
138 Elf32_Rel* GetRelSectionStart(Elf32_Shdr&) const;
139 Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&) const;
140 Elf32_Word* GetHashSectionStart() const;
141 Elf32_Word GetHashBucketNum() const;
142 Elf32_Word GetHashChainNum() const;
143 Elf32_Word GetHashBucket(size_t i) const;
144 Elf32_Word GetHashChain(size_t i) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800145
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000146 typedef std::map<std::string, Elf32_Sym*> SymbolTable;
147 SymbolTable** GetSymbolTable(Elf32_Word section_type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800148
Brian Carlstromc1409452014-02-26 14:06:23 -0800149 bool ValidPointer(const byte* start) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800150
Brian Carlstromc1409452014-02-26 14:06:23 -0800151 const File* const file_;
152 const bool writable_;
153 const bool program_header_only_;
154
155 // ELF header mapping. If program_header_only_ is false, will
156 // actually point to the entire elf file.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800157 UniquePtr<MemMap> map_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000158 Elf32_Ehdr* header_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800159 std::vector<MemMap*> segments_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800160
Brian Carlstromc1409452014-02-26 14:06:23 -0800161 // Pointer to start of first PT_LOAD program segment after Load()
162 // when program_header_only_ is true.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800163 byte* base_address_;
164
165 // The program header should always available but use GetProgramHeadersStart() to be sure.
166 byte* program_headers_start_;
167
168 // Conditionally available values. Use accessors to ensure they exist if they are required.
169 byte* section_headers_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000170 Elf32_Phdr* dynamic_program_header_;
171 Elf32_Dyn* dynamic_section_start_;
172 Elf32_Sym* symtab_section_start_;
173 Elf32_Sym* dynsym_section_start_;
Brian Carlstromc1409452014-02-26 14:06:23 -0800174 char* strtab_section_start_;
175 char* dynstr_section_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000176 Elf32_Word* hash_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800177
Brian Carlstrom265091e2013-01-30 14:08:26 -0800178 SymbolTable* symtab_symbol_table_;
179 SymbolTable* dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800180
181 // Support for GDB JIT
182 byte* jit_elf_image_;
183 JITCodeEntry* jit_gdb_entry_;
184 void GdbJITSupport();
185 // Is this an OAT file with debug information in it?
186 static constexpr uint32_t kExpectedSectionsInOATFile = 12;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800187};
188
189} // namespace art
190
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700191#endif // ART_RUNTIME_ELF_FILE_H_