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