blob: 8a0a5f84c49a129d9bb93b456be4d35f58b00665 [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
Brian Carlstromc1409452014-02-26 14:06:23 -080042 const File& GetFile() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043 return *file_;
44 }
45
Brian Carlstromc1409452014-02-26 14:06:23 -080046 byte* Begin() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047 return map_->Begin();
48 }
49
Brian Carlstromc1409452014-02-26 14:06:23 -080050 byte* End() const {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051 return map_->End();
52 }
53
54 size_t Size() const {
55 return map_->Size();
56 }
57
Brian Carlstromc1409452014-02-26 14:06:23 -080058 Elf32_Ehdr& GetHeader() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059
Brian Carlstromc1409452014-02-26 14:06:23 -080060 Elf32_Word GetProgramHeaderNum() const;
61 Elf32_Phdr& GetProgramHeader(Elf32_Word) const;
62 Elf32_Phdr* FindProgamHeaderByType(Elf32_Word type) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063
Brian Carlstromc1409452014-02-26 14:06:23 -080064 Elf32_Word GetSectionHeaderNum() const;
65 Elf32_Shdr& GetSectionHeader(Elf32_Word) const;
66 Elf32_Shdr* FindSectionByType(Elf32_Word type) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067
Brian Carlstromc1409452014-02-26 14:06:23 -080068 Elf32_Shdr& GetSectionNameStringSection() const;
Brian Carlstrom265091e2013-01-30 14:08:26 -080069
70 // Find .dynsym using .hash for more efficient lookup than FindSymbolAddress.
Brian Carlstromc1409452014-02-26 14:06:23 -080071 const byte* FindDynamicSymbolAddress(const std::string& symbol_name) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072
Nicolas Geoffray50cfe742014-02-19 13:27:42 +000073 static bool IsSymbolSectionType(Elf32_Word section_type);
Brian Carlstromc1409452014-02-26 14:06:23 -080074 Elf32_Word GetSymbolNum(Elf32_Shdr&) const;
75 Elf32_Sym& GetSymbol(Elf32_Word section_type, Elf32_Word i) const;
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 Carlstromc1409452014-02-26 14:06:23 -080086 const std::string& symbol_name,
87 bool build_map);
Brian Carlstrom265091e2013-01-30 14:08:26 -080088
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 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 // Lookup a string given string section and offset. Returns NULL for
96 // special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -080097 const char* GetString(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -080098
99 // Lookup a string by section type. Returns NULL for special 0 offset.
Brian Carlstromc1409452014-02-26 14:06:23 -0800100 const char* GetString(Elf32_Word section_type, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101
Brian Carlstromc1409452014-02-26 14:06:23 -0800102 Elf32_Word GetDynamicNum() const;
103 Elf32_Dyn& GetDynamic(Elf32_Word) const;
104 Elf32_Word FindDynamicValueByType(Elf32_Sword type) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800105
Brian Carlstromc1409452014-02-26 14:06:23 -0800106 Elf32_Word GetRelNum(Elf32_Shdr&) const;
107 Elf32_Rel& GetRel(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800108
Brian Carlstromc1409452014-02-26 14:06:23 -0800109 Elf32_Word GetRelaNum(Elf32_Shdr&) const;
110 Elf32_Rela& GetRela(Elf32_Shdr&, Elf32_Word) const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111
112 // Returns the expected size when the file is loaded at runtime
Brian Carlstromc1409452014-02-26 14:06:23 -0800113 size_t GetLoadedSize() const;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114
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:
Brian Carlstromc1409452014-02-26 14:06:23 -0800120 ElfFile(File* file, bool writable, bool program_header_only);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800121
Brian Carlstromc1409452014-02-26 14:06:23 -0800122 bool Setup(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
Brian Carlstromc1409452014-02-26 14:06:23 -0800126 byte* GetProgramHeadersStart() const;
127 byte* GetSectionHeadersStart() const;
128 Elf32_Phdr& GetDynamicProgramHeader() const;
129 Elf32_Dyn* GetDynamicSectionStart() const;
130 Elf32_Sym* GetSymbolSectionStart(Elf32_Word section_type) const;
131 const char* GetStringSectionStart(Elf32_Word section_type) const;
132 Elf32_Rel* GetRelSectionStart(Elf32_Shdr&) const;
133 Elf32_Rela* GetRelaSectionStart(Elf32_Shdr&) const;
134 Elf32_Word* GetHashSectionStart() const;
135 Elf32_Word GetHashBucketNum() const;
136 Elf32_Word GetHashChainNum() const;
137 Elf32_Word GetHashBucket(size_t i) const;
138 Elf32_Word GetHashChain(size_t i) const;
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 Carlstromc1409452014-02-26 14:06:23 -0800143 bool ValidPointer(const byte* start) const;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800144
Brian Carlstromc1409452014-02-26 14:06:23 -0800145 const File* const file_;
146 const bool writable_;
147 const bool program_header_only_;
148
149 // ELF header mapping. If program_header_only_ is false, will
150 // actually point to the entire elf file.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800151 UniquePtr<MemMap> map_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000152 Elf32_Ehdr* header_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800153 std::vector<MemMap*> segments_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800154
Brian Carlstromc1409452014-02-26 14:06:23 -0800155 // Pointer to start of first PT_LOAD program segment after Load()
156 // when program_header_only_ is true.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800157 byte* base_address_;
158
159 // The program header should always available but use GetProgramHeadersStart() to be sure.
160 byte* program_headers_start_;
161
162 // Conditionally available values. Use accessors to ensure they exist if they are required.
163 byte* section_headers_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000164 Elf32_Phdr* dynamic_program_header_;
165 Elf32_Dyn* dynamic_section_start_;
166 Elf32_Sym* symtab_section_start_;
167 Elf32_Sym* dynsym_section_start_;
Brian Carlstromc1409452014-02-26 14:06:23 -0800168 char* strtab_section_start_;
169 char* dynstr_section_start_;
Nicolas Geoffray50cfe742014-02-19 13:27:42 +0000170 Elf32_Word* hash_section_start_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800171
Brian Carlstrom265091e2013-01-30 14:08:26 -0800172 SymbolTable* symtab_symbol_table_;
173 SymbolTable* dynsym_symbol_table_;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800174};
175
176} // namespace art
177
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700178#endif // ART_RUNTIME_ELF_FILE_H_