blob: 496690b7484807fa0756478238277317beed62b5 [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>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022#include <vector>
23
Brian Carlstrom700c8d32012-11-05 10:42:02 -080024#include "base/unix_file/fd_file.h"
25#include "globals.h"
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000026#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080027#include "mem_map.h"
28#include "os.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029
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);
Alex Light3470ab42014-06-18 10:35:45 -070044 // Open with specific mmap flags, Always maps in the whole file, not just the
45 // program header sections.
46 static ElfFile* Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047 ~ElfFile();
48
49 // Load segments into memory based on PT_LOAD program headers
50
Brian Carlstromc1409452014-02-26 14:06:23 -080051 const File& GetFile() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080052 return *file_;
53 }
54
Brian Carlstromc1409452014-02-26 14:06:23 -080055 byte* Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 return map_->Begin();
57 }
58
Brian Carlstromc1409452014-02-26 14:06:23 -080059 byte* End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080060 return map_->End();
61 }
62
63 size_t Size() const {
64 return map_->Size();
65 }
66
Brian Carlstromc1409452014-02-26 14:06:23 -080067 Elf32_Ehdr& GetHeader() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080068
Brian Carlstromc1409452014-02-26 14:06:23 -080069 Elf32_Word GetProgramHeaderNum() const;
70 Elf32_Phdr& GetProgramHeader(Elf32_Word) const;
71 Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072
Brian Carlstromc1409452014-02-26 14:06:23 -080073 Elf32_Word GetSectionHeaderNum() const;
74 Elf32_Shdr& GetSectionHeader(Elf32_Word) const;
75 Elf32_Shdr* FindSectionByType(Elf32_Word type) const;
Alex Light3470ab42014-06-18 10:35:45 -070076 Elf32_Shdr* FindSectionByName(const std::string& name) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080077
Brian Carlstromc1409452014-02-26 14:06:23 -080078 Elf32_Shdr& GetSectionNameStringSection() const;
Brian Carlstrom265091e2013-01-30 14:08:26 -080079
80 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Brian Carlstromc1409452014-02-26 14:06:23 -080081 const byte* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Alex Light3470ab42014-06-18 10:35:45 -070082 const Elf32_Sym* FindDynamicSymbol(const std::string& symbol_name) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080083
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000084 static bool IsSymbolSectionType(Elf32_Word section_type);
Brian Carlstromc1409452014-02-26 14:06:23 -080085 Elf32_Word GetSymbolNum(Elf32_Shdr&) const;
86 Elf32_Sym& GetSymbol(Elf32_Word section_type, Elf32_Word i) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087
Alex Light3470ab42014-06-18 10:35:45 -070088 // Find symbol in specified table, returning nullptr if it is not found.
Brian Carlstrom265091e2013-01-30 14:08:26 -080089 //
90 // If build_map is true, builds a map to speed repeated access. The
91 // map does not included untyped symbol values (aka STT_NOTYPE)
92 // since they can contain duplicates. If build_map is false, the map
93 // will be used if it was already created. Typically build_map
94 // should be set unless only a small number of symbols will be
95 // looked up.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000096 Elf32_Sym* FindSymbolByName(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -080097 const std::string& symbol_name,
98 bool build_map);
Brian Carlstrom265091e2013-01-30 14:08:26 -080099
100 // Find address of symbol in specified table, returning 0 if it is
101 // not found. See FindSymbolByName for an explanation of build_map.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000102 Elf32_Addr FindSymbolAddress(Elf32_Word section_type,
Brian Carlstromc1409452014-02-26 14:06:23 -0800103 const std::string& symbol_name,
104 bool build_map);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800105
Alex Light3470ab42014-06-18 10:35:45 -0700106 // Lookup a string given string section and offset. Returns nullptr for
Brian Carlstrom265091e2013-01-30 14:08:26 -0800107 // special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -0800108 const char* GetString(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800109
Alex Light3470ab42014-06-18 10:35:45 -0700110 // Lookup a string by section type. Returns nullptr for special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -0800111 const char* GetString(Elf32_Word section_type, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800112
Brian Carlstromc1409452014-02-26 14:06:23 -0800113 Elf32_Word GetDynamicNum() const;
114 Elf32_Dyn& GetDynamic(Elf32_Word) const;
115 Elf32_Word FindDynamicValueByType(Elf32_Sword type) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800116
Brian Carlstromc1409452014-02-26 14:06:23 -0800117 Elf32_Word GetRelNum(Elf32_Shdr&) const;
118 Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800119
Brian Carlstromc1409452014-02-26 14:06:23 -0800120 Elf32_Word GetRelaNum(Elf32_Shdr&) const;
121 Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800122
123 // Returns the expected size when the file is loaded at runtime
Brian Carlstromc1409452014-02-26 14:06:23 -0800124 size_t GetLoadedSize() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125
Brian Carlstromf1d34552013-07-12 20:22:23 -0700126 // Load segments into memory based on PT_LOAD program headers.
127 // executable is true at run time, false at compile time.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700128 bool Load(bool executable, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800129
130 private:
Brian Carlstromc1409452014-02-26 14:06:23 -0800131 ElfFile(File* file, bool writable, bool program_header_only);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800132
Alex Light3470ab42014-06-18 10:35:45 -0700133 bool Setup(int prot, int flags, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800134
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800135 bool SetMap(MemMap* map, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800136
Brian Carlstromc1409452014-02-26 14:06:23 -0800137 byte* GetProgramHeadersStart() const;
138 byte* GetSectionHeadersStart() const;
139 Elf32_Phdr& GetDynamicProgramHeader() const;
140 Elf32_Dyn* GetDynamicSectionStart() const;
141 Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type) const;
142 const char* GetStringSectionStart(Elf32_Word section_type) const;
143 Elf32_Rel* GetRelSectionStart(Elf32_Shdr&) const;
144 Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&) const;
145 Elf32_Word* GetHashSectionStart() const;
146 Elf32_Word GetHashBucketNum() const;
147 Elf32_Word GetHashChainNum() const;
148 Elf32_Word GetHashBucket(size_t i) const;
149 Elf32_Word GetHashChain(size_t i) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000151 typedef std::map<std::string, Elf32_Sym*> SymbolTable;
152 SymbolTable** GetSymbolTable(Elf32_Word section_type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800153
Brian Carlstromc1409452014-02-26 14:06:23 -0800154 bool ValidPointer(const byte* start) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800155
Brian Carlstromc1409452014-02-26 14:06:23 -0800156 const File* const file_;
157 const bool writable_;
158 const bool program_header_only_;
159
160 // ELF header mapping. If program_header_only_ is false, will
161 // actually point to the entire elf file.
Ian Rogers700a4022014-05-19 16:49:03 -0700162 std::unique_ptr<MemMap> map_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000163 Elf32_Ehdr* header_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800164 std::vector<MemMap*> segments_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800165
Brian Carlstromc1409452014-02-26 14:06:23 -0800166 // Pointer to start of first PT_LOAD program segment after Load()
167 // when program_header_only_ is true.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800168 byte* base_address_;
169
170 // The program header should always available but use GetProgramHeadersStart() to be sure.
171 byte* program_headers_start_;
172
173 // Conditionally available values. Use accessors to ensure they exist if they are required.
174 byte* section_headers_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000175 Elf32_Phdr* dynamic_program_header_;
176 Elf32_Dyn* dynamic_section_start_;
177 Elf32_Sym* symtab_section_start_;
178 Elf32_Sym* dynsym_section_start_;
Brian Carlstromc1409452014-02-26 14:06:23 -0800179 char* strtab_section_start_;
180 char* dynstr_section_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000181 Elf32_Word* hash_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800182
Brian Carlstrom265091e2013-01-30 14:08:26 -0800183 SymbolTable* symtab_symbol_table_;
184 SymbolTable* dynsym_symbol_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800185
186 // Support for GDB JIT
187 byte* jit_elf_image_;
188 JITCodeEntry* jit_gdb_entry_;
Alex Light3470ab42014-06-18 10:35:45 -0700189 std::unique_ptr<ElfFile> gdb_file_mapping_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800190 void GdbJITSupport();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800191};
192
193} // namespace art
194
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700195#endif // ART_RUNTIME_ELF_FILE_H_