Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 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 | #include "elf_file.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "utils.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 25 | ElfFile::ElfFile() |
| 26 | : file_(NULL), |
| 27 | writable_(false), |
| 28 | program_header_only_(false), |
| 29 | header_(NULL), |
| 30 | base_address_(NULL), |
| 31 | program_headers_start_(NULL), |
| 32 | section_headers_start_(NULL), |
| 33 | dynamic_program_header_(NULL), |
| 34 | dynamic_section_start_(NULL), |
| 35 | symtab_section_start_(NULL), |
| 36 | dynsym_section_start_(NULL), |
| 37 | strtab_section_start_(NULL), |
| 38 | dynstr_section_start_(NULL), |
| 39 | hash_section_start_(NULL), |
| 40 | symtab_symbol_table_(NULL), |
| 41 | dynsym_symbol_table_(NULL) {} |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 42 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 43 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, |
| 44 | std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | UniquePtr<ElfFile> elf_file(new ElfFile()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 46 | if (!elf_file->Setup(file, writable, program_header_only, error_msg)) { |
| 47 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 48 | } |
| 49 | return elf_file.release(); |
| 50 | } |
| 51 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 52 | bool ElfFile::Setup(File* file, bool writable, bool program_header_only, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 53 | CHECK(file != NULL); |
| 54 | file_ = file; |
| 55 | writable_ = writable; |
| 56 | program_header_only_ = program_header_only; |
| 57 | |
| 58 | int prot; |
| 59 | int flags; |
| 60 | if (writable_) { |
| 61 | prot = PROT_READ | PROT_WRITE; |
| 62 | flags = MAP_SHARED; |
| 63 | } else { |
| 64 | prot = PROT_READ; |
| 65 | flags = MAP_PRIVATE; |
| 66 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 67 | int64_t temp_file_length = file_->GetLength(); |
| 68 | if (temp_file_length < 0) { |
| 69 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 70 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 71 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 72 | return false; |
| 73 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 74 | size_t file_length = static_cast<size_t>(temp_file_length); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 75 | if (file_length < sizeof(Elf32_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 76 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of " |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 77 | "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 78 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (program_header_only) { |
| 83 | // first just map ELF header to get program header size information |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 84 | size_t elf_header_size = sizeof(Elf32_Ehdr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 85 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 86 | file_->GetPath().c_str(), error_msg), |
| 87 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | // then remap to cover program header |
| 91 | size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 92 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 93 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 94 | "header of %zd bytes: '%s'", file_length, |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 95 | sizeof(Elf32_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 98 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 99 | file_->GetPath().c_str(), error_msg), |
| 100 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 101 | *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | } else { |
| 105 | // otherwise map entire file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 106 | if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 107 | file_->GetPath().c_str(), error_msg), |
| 108 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Either way, the program header is relative to the elf header |
| 115 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 116 | |
| 117 | if (!program_header_only) { |
| 118 | // Setup section headers. |
| 119 | section_headers_start_ = Begin() + GetHeader().e_shoff; |
| 120 | |
| 121 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 122 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 123 | if (dynamic_program_header_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 124 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 125 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 130 | = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 131 | |
| 132 | // Find other sections from section headers |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 133 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 134 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 135 | byte* section_addr = Begin() + section_header.sh_offset; |
| 136 | switch (section_header.sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 137 | case SHT_SYMTAB: { |
| 138 | symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 139 | break; |
| 140 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 141 | case SHT_DYNSYM: { |
| 142 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 143 | break; |
| 144 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 145 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 146 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 147 | if ((section_header.sh_flags & SHF_ALLOC) != 0) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 148 | dynstr_section_start_ = reinterpret_cast<char*>(section_addr); |
| 149 | } else { |
| 150 | strtab_section_start_ = reinterpret_cast<char*>(section_addr); |
| 151 | } |
| 152 | break; |
| 153 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 154 | case SHT_DYNAMIC: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 155 | if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) { |
| 156 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 157 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 158 | << reinterpret_cast<void*>(dynamic_section_start_) |
| 159 | << " != " << reinterpret_cast<void*>(section_addr); |
| 160 | return false; |
| 161 | } |
| 162 | break; |
| 163 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 164 | case SHT_HASH: { |
| 165 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | ElfFile::~ElfFile() { |
| 175 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 176 | delete symtab_symbol_table_; |
| 177 | delete dynsym_symbol_table_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 180 | bool ElfFile::SetMap(MemMap* map, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 181 | if (map == NULL) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 182 | // MemMap::Open should have already set an error. |
| 183 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | map_.reset(map); |
| 187 | CHECK(map_.get() != NULL) << file_->GetPath(); |
| 188 | CHECK(map_->Begin() != NULL) << file_->GetPath(); |
| 189 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 190 | header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin()); |
| 191 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 192 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 193 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 194 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 195 | *error_msg = StringPrintf("Failed to find ELF magic in %s: %c%c%c%c", |
| 196 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 197 | header_->e_ident[EI_MAG0], |
| 198 | header_->e_ident[EI_MAG1], |
| 199 | header_->e_ident[EI_MAG2], |
| 200 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | // TODO: remove these static_casts from enum when using -std=gnu++0x |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 206 | CHECK_EQ(static_cast<unsigned char>(ELFCLASS32), header_->e_ident[EI_CLASS]) << file_->GetPath(); |
| 207 | CHECK_EQ(static_cast<unsigned char>(ELFDATA2LSB), header_->e_ident[EI_DATA]) << file_->GetPath(); |
| 208 | CHECK_EQ(static_cast<unsigned char>(EV_CURRENT), header_->e_ident[EI_VERSION]) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 209 | |
| 210 | // TODO: remove these static_casts from enum when using -std=gnu++0x |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 211 | CHECK_EQ(static_cast<Elf32_Half>(ET_DYN), header_->e_type) << file_->GetPath(); |
| 212 | CHECK_EQ(static_cast<Elf32_Word>(EV_CURRENT), header_->e_version) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 213 | CHECK_EQ(0U, header_->e_entry) << file_->GetPath(); |
| 214 | |
| 215 | CHECK_NE(0U, header_->e_phoff) << file_->GetPath(); |
| 216 | CHECK_NE(0U, header_->e_shoff) << file_->GetPath(); |
| 217 | CHECK_NE(0U, header_->e_ehsize) << file_->GetPath(); |
| 218 | CHECK_NE(0U, header_->e_phentsize) << file_->GetPath(); |
| 219 | CHECK_NE(0U, header_->e_phnum) << file_->GetPath(); |
| 220 | CHECK_NE(0U, header_->e_shentsize) << file_->GetPath(); |
| 221 | CHECK_NE(0U, header_->e_shnum) << file_->GetPath(); |
| 222 | CHECK_NE(0U, header_->e_shstrndx) << file_->GetPath(); |
| 223 | CHECK_GE(header_->e_shnum, header_->e_shstrndx) << file_->GetPath(); |
| 224 | if (!program_header_only_) { |
| 225 | CHECK_GT(Size(), header_->e_phoff) << file_->GetPath(); |
| 226 | CHECK_GT(Size(), header_->e_shoff) << file_->GetPath(); |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 232 | Elf32_Ehdr& ElfFile::GetHeader() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 233 | CHECK(header_ != NULL); |
| 234 | return *header_; |
| 235 | } |
| 236 | |
| 237 | byte* ElfFile::GetProgramHeadersStart() { |
| 238 | CHECK(program_headers_start_ != NULL); |
| 239 | return program_headers_start_; |
| 240 | } |
| 241 | |
| 242 | byte* ElfFile::GetSectionHeadersStart() { |
| 243 | CHECK(section_headers_start_ != NULL); |
| 244 | return section_headers_start_; |
| 245 | } |
| 246 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 247 | Elf32_Phdr& ElfFile::GetDynamicProgramHeader() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 248 | CHECK(dynamic_program_header_ != NULL); |
| 249 | return *dynamic_program_header_; |
| 250 | } |
| 251 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 252 | Elf32_Dyn* ElfFile::GetDynamicSectionStart() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 253 | CHECK(dynamic_section_start_ != NULL); |
| 254 | return dynamic_section_start_; |
| 255 | } |
| 256 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 257 | Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 258 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 259 | Elf32_Sym* symbol_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 260 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 261 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 262 | symbol_section_start = symtab_section_start_; |
| 263 | break; |
| 264 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 265 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 266 | symbol_section_start = dynsym_section_start_; |
| 267 | break; |
| 268 | } |
| 269 | default: { |
| 270 | LOG(FATAL) << section_type; |
| 271 | symbol_section_start = NULL; |
| 272 | } |
| 273 | } |
| 274 | CHECK(symbol_section_start != NULL); |
| 275 | return symbol_section_start; |
| 276 | } |
| 277 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 278 | const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 279 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 280 | const char* string_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 281 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 282 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 283 | string_section_start = strtab_section_start_; |
| 284 | break; |
| 285 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 286 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 287 | string_section_start = dynstr_section_start_; |
| 288 | break; |
| 289 | } |
| 290 | default: { |
| 291 | LOG(FATAL) << section_type; |
| 292 | string_section_start = NULL; |
| 293 | } |
| 294 | } |
| 295 | CHECK(string_section_start != NULL); |
| 296 | return string_section_start; |
| 297 | } |
| 298 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 299 | const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 300 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 301 | if (i == 0) { |
| 302 | return NULL; |
| 303 | } |
| 304 | const char* string_section_start = GetStringSectionStart(section_type); |
| 305 | const char* string = string_section_start + i; |
| 306 | return string; |
| 307 | } |
| 308 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 309 | Elf32_Word* ElfFile::GetHashSectionStart() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 310 | CHECK(hash_section_start_ != NULL); |
| 311 | return hash_section_start_; |
| 312 | } |
| 313 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 314 | Elf32_Word ElfFile::GetHashBucketNum() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 315 | return GetHashSectionStart()[0]; |
| 316 | } |
| 317 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 318 | Elf32_Word ElfFile::GetHashChainNum() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 319 | return GetHashSectionStart()[1]; |
| 320 | } |
| 321 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 322 | Elf32_Word ElfFile::GetHashBucket(size_t i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 323 | CHECK_LT(i, GetHashBucketNum()); |
| 324 | // 0 is nbucket, 1 is nchain |
| 325 | return GetHashSectionStart()[2 + i]; |
| 326 | } |
| 327 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 328 | Elf32_Word ElfFile::GetHashChain(size_t i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 329 | CHECK_LT(i, GetHashChainNum()); |
| 330 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 331 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 332 | } |
| 333 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 334 | Elf32_Word ElfFile::GetProgramHeaderNum() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 335 | return GetHeader().e_phnum; |
| 336 | } |
| 337 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 338 | Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 339 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); |
| 340 | byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
| 341 | CHECK_LT(program_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 342 | return *reinterpret_cast<Elf32_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 345 | Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) { |
| 346 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 347 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 348 | if (program_header.p_type == type) { |
| 349 | return &program_header; |
| 350 | } |
| 351 | } |
| 352 | return NULL; |
| 353 | } |
| 354 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 355 | Elf32_Word ElfFile::GetSectionHeaderNum() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 356 | return GetHeader().e_shnum; |
| 357 | } |
| 358 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 359 | Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 360 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 361 | // Even if we Load(), it doesn't bring in all the sections. |
| 362 | CHECK(!program_header_only_) << file_->GetPath(); |
| 363 | CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath(); |
| 364 | byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
| 365 | CHECK_LT(section_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 366 | return *reinterpret_cast<Elf32_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 369 | Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 370 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 371 | // We could change this to switch on known types if they were detected during loading. |
| 372 | CHECK(!program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 373 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 374 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 375 | if (section_header.sh_type == type) { |
| 376 | return §ion_header; |
| 377 | } |
| 378 | } |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 383 | static unsigned elfhash(const char *_name) { |
| 384 | const unsigned char *name = (const unsigned char *) _name; |
| 385 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 386 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 387 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 388 | h = (h << 4) + *name++; |
| 389 | g = h & 0xf0000000; |
| 390 | h ^= g; |
| 391 | h ^= g >> 24; |
| 392 | } |
| 393 | return h; |
| 394 | } |
| 395 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 396 | Elf32_Shdr& ElfFile::GetSectionNameStringSection() { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 397 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 401 | Elf32_Word hash = elfhash(symbol_name.c_str()); |
| 402 | Elf32_Word bucket_index = hash % GetHashBucketNum(); |
| 403 | Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 404 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 405 | Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
| 406 | const char* name = GetString(SHT_DYNSYM, symbol.st_name); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 407 | if (symbol_name == name) { |
| 408 | return base_address_ + symbol.st_value; |
| 409 | } |
| 410 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index); |
| 411 | } |
| 412 | return NULL; |
| 413 | } |
| 414 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 415 | bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) { |
| 416 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 419 | Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 420 | CHECK(IsSymbolSectionType(section_header.sh_type)) << file_->GetPath() << " " << section_header.sh_type; |
| 421 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 422 | return section_header.sh_size / section_header.sh_entsize; |
| 423 | } |
| 424 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 425 | Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type, |
| 426 | Elf32_Word i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 427 | return *(GetSymbolSectionStart(section_type) + i); |
| 428 | } |
| 429 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 430 | ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 431 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 432 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 433 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 434 | return &symtab_symbol_table_; |
| 435 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 436 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 437 | return &dynsym_symbol_table_; |
| 438 | } |
| 439 | default: { |
| 440 | LOG(FATAL) << section_type; |
| 441 | return NULL; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 446 | Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type, |
| 447 | const std::string& symbol_name, |
| 448 | bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 449 | CHECK(!program_header_only_) << file_->GetPath(); |
| 450 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 451 | |
| 452 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
| 453 | if (*symbol_table != NULL || build_map) { |
| 454 | if (*symbol_table == NULL) { |
| 455 | DCHECK(build_map); |
| 456 | *symbol_table = new SymbolTable; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 457 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 458 | CHECK(symbol_section != NULL) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 459 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 460 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 461 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
| 462 | unsigned char type = ELF32_ST_TYPE(symbol.st_info); |
| 463 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 464 | continue; |
| 465 | } |
| 466 | const char* name = GetString(string_section, symbol.st_name); |
| 467 | if (name == NULL) { |
| 468 | continue; |
| 469 | } |
| 470 | std::pair<SymbolTable::iterator, bool> result = (*symbol_table)->insert(std::make_pair(name, &symbol)); |
| 471 | if (!result.second) { |
| 472 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
| 473 | CHECK_EQ(symbol.st_value, result.first->second->st_value); |
| 474 | CHECK_EQ(symbol.st_size, result.first->second->st_size); |
| 475 | CHECK_EQ(symbol.st_info, result.first->second->st_info); |
| 476 | CHECK_EQ(symbol.st_other, result.first->second->st_other); |
| 477 | CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | CHECK(*symbol_table != NULL); |
| 482 | SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
| 483 | if (it == (*symbol_table)->end()) { |
| 484 | return NULL; |
| 485 | } |
| 486 | return it->second; |
| 487 | } |
| 488 | |
| 489 | // Fall back to linear search |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 490 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 491 | CHECK(symbol_section != NULL) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 492 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 493 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 494 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 495 | const char* name = GetString(string_section, symbol.st_name); |
| 496 | if (name == NULL) { |
| 497 | continue; |
| 498 | } |
| 499 | if (symbol_name == name) { |
| 500 | return &symbol; |
| 501 | } |
| 502 | } |
| 503 | return NULL; |
| 504 | } |
| 505 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 506 | Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 507 | const std::string& symbol_name, |
| 508 | bool build_map) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 509 | Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 510 | if (symbol == NULL) { |
| 511 | return 0; |
| 512 | } |
| 513 | return symbol->st_value; |
| 514 | } |
| 515 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 516 | const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 517 | CHECK(!program_header_only_) << file_->GetPath(); |
| 518 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 519 | CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 520 | CHECK_LT(i, string_section.sh_size) << file_->GetPath(); |
| 521 | if (i == 0) { |
| 522 | return NULL; |
| 523 | } |
| 524 | byte* strings = Begin() + string_section.sh_offset; |
| 525 | byte* string = strings + i; |
| 526 | CHECK_LT(string, End()) << file_->GetPath(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 527 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 530 | Elf32_Word ElfFile::GetDynamicNum() { |
| 531 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 534 | Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 535 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 536 | return *(GetDynamicSectionStart() + i); |
| 537 | } |
| 538 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 539 | Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) { |
| 540 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 541 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 542 | if (elf_dyn.d_tag == type) { |
| 543 | return elf_dyn.d_un.d_val; |
| 544 | } |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 549 | Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) { |
| 550 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 551 | return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 554 | Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) { |
| 555 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 556 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 557 | return section_header.sh_size / section_header.sh_entsize; |
| 558 | } |
| 559 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 560 | Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) { |
| 561 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 562 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 563 | return *(GetRelSectionStart(section_header) + i); |
| 564 | } |
| 565 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 566 | Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) { |
| 567 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 568 | return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 571 | Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) { |
| 572 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 573 | return section_header.sh_size / section_header.sh_entsize; |
| 574 | } |
| 575 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 576 | Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) { |
| 577 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 578 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 579 | return *(GetRelaSectionStart(section_header) + i); |
| 580 | } |
| 581 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 582 | // Base on bionic phdr_table_get_load_size |
| 583 | size_t ElfFile::GetLoadedSize() { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 584 | Elf32_Addr min_vaddr = 0xFFFFFFFFu; |
| 585 | Elf32_Addr max_vaddr = 0x00000000u; |
| 586 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 587 | Elf32_Phdr& program_header = GetProgramHeader(i); |
| 588 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 589 | continue; |
| 590 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 591 | Elf32_Addr begin_vaddr = program_header.p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 592 | if (begin_vaddr < min_vaddr) { |
| 593 | min_vaddr = begin_vaddr; |
| 594 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 595 | Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 596 | if (end_vaddr > max_vaddr) { |
| 597 | max_vaddr = end_vaddr; |
| 598 | } |
| 599 | } |
| 600 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 601 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 602 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 603 | size_t loaded_size = max_vaddr - min_vaddr; |
| 604 | return loaded_size; |
| 605 | } |
| 606 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 607 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 608 | // TODO: actually return false error |
| 609 | CHECK(program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 610 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 611 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 612 | |
| 613 | // Record .dynamic header information for later use |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 614 | if (program_header.p_type == PT_DYNAMIC) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 615 | dynamic_program_header_ = &program_header; |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // Not something to load, move on. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 620 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 621 | continue; |
| 622 | } |
| 623 | |
| 624 | // Found something to load. |
| 625 | |
| 626 | // If p_vaddr is zero, it must be the first loadable segment, |
| 627 | // since they must be in order. Since it is zero, there isn't a |
| 628 | // specific address requested, so first request a contiguous chunk |
| 629 | // of required size for all segments, but with no |
| 630 | // permissions. We'll then carve that up with the proper |
| 631 | // permissions as we load the actual segments. If p_vaddr is |
| 632 | // non-zero, the segments require the specific address specified, |
| 633 | // which either was specified in the file because we already set |
| 634 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 635 | int64_t temp_file_length = file_->GetLength(); |
| 636 | if (temp_file_length < 0) { |
| 637 | errno = -temp_file_length; |
| 638 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 639 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 640 | return false; |
| 641 | } |
| 642 | size_t file_length = static_cast<size_t>(temp_file_length); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 643 | if (program_header.p_vaddr == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 644 | std::string reservation_name("ElfFile reservation for "); |
| 645 | reservation_name += file_->GetPath(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 646 | std::string error_msg; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 647 | UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 648 | NULL, GetLoadedSize(), PROT_NONE, false, |
| 649 | &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 650 | CHECK(reserve.get() != NULL) << file_->GetPath() << ": " << error_msg; |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 651 | base_address_ = reserve->Begin(); |
| 652 | segments_.push_back(reserve.release()); |
| 653 | } |
| 654 | // empty segment, nothing to map |
| 655 | if (program_header.p_memsz == 0) { |
| 656 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 657 | } |
| 658 | byte* p_vaddr = base_address_ + program_header.p_vaddr; |
| 659 | int prot = 0; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 660 | if (executable && ((program_header.p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 661 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 662 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 663 | if ((program_header.p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 664 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 665 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 666 | if ((program_header.p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 667 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 668 | } |
| 669 | int flags = MAP_FIXED; |
| 670 | if (writable_) { |
| 671 | prot |= PROT_WRITE; |
| 672 | flags |= MAP_SHARED; |
| 673 | } else { |
| 674 | flags |= MAP_PRIVATE; |
| 675 | } |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 676 | if (file_length < (program_header.p_offset + program_header.p_memsz)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 677 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 678 | "%d of %d bytes: '%s'", file_length, i, |
| 679 | program_header.p_offset + program_header.p_memsz, |
| 680 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 681 | return false; |
| 682 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 683 | UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
| 684 | program_header.p_memsz, |
| 685 | prot, flags, file_->Fd(), |
| 686 | program_header.p_offset, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 687 | true, |
| 688 | file_->GetPath().c_str(), |
| 689 | error_msg)); |
| 690 | CHECK(segment.get() != nullptr) << *error_msg; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 691 | CHECK_EQ(segment->Begin(), p_vaddr) << file_->GetPath(); |
| 692 | segments_.push_back(segment.release()); |
| 693 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 694 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 695 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
| 696 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 697 | = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr); |
| 698 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 699 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 700 | byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
| 701 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 702 | case DT_HASH: { |
| 703 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 704 | break; |
| 705 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 706 | case DT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 707 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 708 | break; |
| 709 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 710 | case DT_SYMTAB: { |
| 711 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 712 | break; |
| 713 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 714 | case DT_NULL: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 715 | CHECK_EQ(GetDynamicNum(), i+1); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 716 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return true; |
| 722 | } |
| 723 | |
| 724 | } // namespace art |