blob: baf4356aee19897cc281943b6a06c5620dcc5481 [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
32// Used for compile time and runtime for ElfFile access. Because of
33// the need for use at runtime, cannot directly use LLVM classes such as
34// ELFObjectFile.
35class ElfFile {
36 public:
Ian Rogers8d31bbd2013-10-13 10:44:14 -070037 static ElfFile* Open(File* file, bool writable, bool program_header_only, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038 ~ElfFile();
39
40 // Load segments into memory based on PT_LOAD program headers
41
42 File& GetFile() const {
43 return *file_;
44 }
45
46 byte* Begin() {
47 return map_->Begin();
48 }
49
50 byte* End() {
51 return map_->End();
52 }
53
54 size_t Size() const {
55 return map_->Size();
56 }
57
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000058 Elf32_Ehdr& GetHeader();
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000060 Elf32_Word GetProgramHeaderNum();
61 Elf32_Phdr& GetProgramHeader(Elf32_Word);
62 Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000064 Elf32_Word GetSectionHeaderNum();
65 Elf32_Shdr& GetSectionHeader(Elf32_Word);
66 Elf32_Shdr* FindSectionByType(Elf32_Word type);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000068 Elf32_Shdr& GetSectionNameStringSection();
Brian Carlstrom265091e2013-01-30 14:08:26 -080069
70 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Brian Carlstrom700c8d32012-11-05 10:42:02 -080071 byte* FindDynamicSymbolAddress(const std::string& symbol_name);
72
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000073 static bool IsSymbolSectionType(Elf32_Word section_type);
74 Elf32_Word GetSymbolNum(Elf32_Shdr&);
75 Elf32_Sym& GetSymbol(Elf32_Word section_type, Elf32_Word i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076
Brian Carlstrom265091e2013-01-30 14:08:26 -080077 // Find symbol in specified table, returning NULL if it is not found.
78 //
79 // If build_map is true, builds a map to speed repeated access. The
80 // map does not included untyped symbol values (aka STT_NOTYPE)
81 // since they can contain duplicates. If build_map is false, the map
82 // will be used if it was already created. Typically build_map
83 // should be set unless only a small number of symbols will be
84 // looked up.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000085 Elf32_Sym* FindSymbolByName(Elf32_Word section_type,
Brian Carlstrom265091e2013-01-30 14:08:26 -080086 const std::string& symbol_name,
87 bool build_map);
88
89 // Find address of symbol in specified table, returning 0 if it is
90 // not found. See FindSymbolByName for an explanation of build_map.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000091 Elf32_Addr FindSymbolAddress(Elf32_Word section_type,
Brian Carlstrom265091e2013-01-30 14:08:26 -080092 const std::string& symbol_name,
93 bool build_map);
94
95 // Lookup a string given string section and offset. Returns NULL for
96 // special 0 offset.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000097 const char* GetString(Elf32_Shdr&, Elf32_Word);
Brian Carlstrom265091e2013-01-30 14:08:26 -080098
99 // Lookup a string by section type. Returns NULL for special 0 offset.
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000100 const char* GetString(Elf32_Word section_type, Elf32_Word);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000102 Elf32_Word GetDynamicNum();
103 Elf32_Dyn& GetDynamic(Elf32_Word);
104 Elf32_Word FindDynamicValueByType(Elf32_Sword type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800105
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000106 Elf32_Word GetRelNum(Elf32_Shdr&);
107 Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800108
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000109 Elf32_Word GetRelaNum(Elf32_Shdr&);
110 Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111
112 // Returns the expected size when the file is loaded at runtime
113 size_t GetLoadedSize();
114
Brian Carlstromf1d34552013-07-12 20:22:23 -0700115 // Load segments into memory based on PT_LOAD program headers.
116 // executable is true at run time, false at compile time.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 bool Load(bool executable, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800118
119 private:
120 ElfFile();
121
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700122 bool Setup(File* file, bool writable, bool program_header_only, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123
Brian Carlstromd0c09dc2013-11-06 18:25:35 -0800124 bool SetMap(MemMap* map, std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800125
126 byte* GetProgramHeadersStart();
127 byte* GetSectionHeadersStart();
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000128 Elf32_Phdr& GetDynamicProgramHeader();
129 Elf32_Dyn* GetDynamicSectionStart();
130 Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type);
131 const char* GetStringSectionStart(Elf32_Word section_type);
132 Elf32_Rel* GetRelSectionStart(Elf32_Shdr&);
133 Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&);
134 Elf32_Word* GetHashSectionStart();
135 Elf32_Word GetHashBucketNum();
136 Elf32_Word GetHashChainNum();
137 Elf32_Word GetHashBucket(size_t i);
138 Elf32_Word GetHashChain(size_t i);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000140 typedef std::map<std::string, Elf32_Sym*> SymbolTable;
141 SymbolTable** GetSymbolTable(Elf32_Word section_type);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800142
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800143 File* file_;
144 bool writable_;
145 bool program_header_only_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800146
147 // ELF header mapping. If program_header_only_ is false, will actually point to the entire elf file.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 UniquePtr<MemMap> map_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000149 Elf32_Ehdr* header_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800150 std::vector<MemMap*> segments_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800151
152 // Pointer to start of first PT_LOAD program segment after Load() when program_header_only_ is true.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 byte* base_address_;
154
155 // The program header should always available but use GetProgramHeadersStart() to be sure.
156 byte* program_headers_start_;
157
158 // Conditionally available values. Use accessors to ensure they exist if they are required.
159 byte* section_headers_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000160 Elf32_Phdr* dynamic_program_header_;
161 Elf32_Dyn* dynamic_section_start_;
162 Elf32_Sym* symtab_section_start_;
163 Elf32_Sym* dynsym_section_start_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800164 const char* strtab_section_start_;
165 const char* dynstr_section_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000166 Elf32_Word* hash_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800167
Brian Carlstrom265091e2013-01-30 14:08:26 -0800168 SymbolTable* symtab_symbol_table_;
169 SymbolTable* dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800170};
171
172} // namespace art
173
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700174#endif // ART_RUNTIME_ELF_FILE_H_