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 | |
| 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 | |
| 41 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only) { |
| 42 | UniquePtr<ElfFile> elf_file(new ElfFile()); |
| 43 | if (!elf_file->Setup(file, writable, program_header_only)) { |
| 44 | return NULL; |
| 45 | } |
| 46 | return elf_file.release(); |
| 47 | } |
| 48 | |
| 49 | bool ElfFile::Setup(File* file, bool writable, bool program_header_only) { |
| 50 | CHECK(file != NULL); |
| 51 | file_ = file; |
| 52 | writable_ = writable; |
| 53 | program_header_only_ = program_header_only; |
| 54 | |
| 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 | } |
| 64 | if (file->GetLength() < sizeof(llvm::ELF::Elf32_Ehdr)) { |
| 65 | LOG(WARNING) << "File not large enough to contain ELF header: " << file->GetPath(); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (program_header_only) { |
| 70 | // first just map ELF header to get program header size information |
| 71 | size_t elf_header_size = sizeof(llvm::ELF::Elf32_Ehdr); |
| 72 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file->Fd(), 0))) { |
| 73 | LOG(WARNING) << "Failed to map ELF header: " << file->GetPath(); |
| 74 | return false; |
| 75 | } |
| 76 | // then remap to cover program header |
| 77 | size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum); |
| 78 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file->Fd(), 0))) { |
| 79 | LOG(WARNING) << "Failed to map ELF program headers: " << file->GetPath(); |
| 80 | return false; |
| 81 | } |
| 82 | } else { |
| 83 | // otherwise map entire file |
| 84 | if (!SetMap(MemMap::MapFile(file->GetLength(), prot, flags, file->Fd(), 0))) { |
| 85 | LOG(WARNING) << "Failed to map ELF file: " << file->GetPath(); |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Either way, the program header is relative to the elf header |
| 91 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 92 | |
| 93 | if (!program_header_only) { |
| 94 | // Setup section headers. |
| 95 | section_headers_start_ = Begin() + GetHeader().e_shoff; |
| 96 | |
| 97 | // Find .dynamic section info from program header |
| 98 | dynamic_program_header_ = FindProgamHeaderByType(llvm::ELF::PT_DYNAMIC); |
| 99 | if (dynamic_program_header_ == NULL) { |
| 100 | LOG(WARNING) << "Failed to find PT_DYNAMIC program header in ELF file: " << file->GetPath(); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | dynamic_section_start_ |
| 105 | = reinterpret_cast<llvm::ELF::Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset); |
| 106 | |
| 107 | // Find other sections from section headers |
| 108 | for (llvm::ELF::Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 109 | llvm::ELF::Elf32_Shdr& section_header = GetSectionHeader(i); |
| 110 | byte* section_addr = Begin() + section_header.sh_offset; |
| 111 | switch (section_header.sh_type) { |
| 112 | case llvm::ELF::SHT_SYMTAB: { |
| 113 | symtab_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(section_addr); |
| 114 | break; |
| 115 | } |
| 116 | case llvm::ELF::SHT_DYNSYM: { |
| 117 | dynsym_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(section_addr); |
| 118 | break; |
| 119 | } |
| 120 | case llvm::ELF::SHT_STRTAB: { |
| 121 | // TODO: base these off of sh_link from .symtab and .dynsym above |
| 122 | if ((section_header.sh_flags & llvm::ELF::SHF_ALLOC) != 0) { |
| 123 | dynstr_section_start_ = reinterpret_cast<char*>(section_addr); |
| 124 | } else { |
| 125 | strtab_section_start_ = reinterpret_cast<char*>(section_addr); |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | case llvm::ELF::SHT_DYNAMIC: { |
| 130 | if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) { |
| 131 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
| 132 | << file->GetPath() << ": " << std::hex |
| 133 | << reinterpret_cast<void*>(dynamic_section_start_) |
| 134 | << " != " << reinterpret_cast<void*>(section_addr); |
| 135 | return false; |
| 136 | } |
| 137 | break; |
| 138 | } |
| 139 | case llvm::ELF::SHT_HASH: { |
| 140 | hash_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Word*>(section_addr); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | ElfFile::~ElfFile() { |
| 150 | STLDeleteElements(&segments_); |
| 151 | } |
| 152 | |
| 153 | bool ElfFile::SetMap(MemMap* map) { |
| 154 | if (map == NULL) { |
| 155 | // MemMap::Open should have already logged |
| 156 | return false; |
| 157 | } |
| 158 | map_.reset(map); |
| 159 | CHECK(map_.get() != NULL) << file_->GetPath(); |
| 160 | CHECK(map_->Begin() != NULL) << file_->GetPath(); |
| 161 | |
| 162 | header_ = reinterpret_cast<llvm::ELF::Elf32_Ehdr*>(map_->Begin()); |
| 163 | if ((llvm::ELF::ElfMagic[0] != header_->e_ident[llvm::ELF::EI_MAG0]) |
| 164 | || (llvm::ELF::ElfMagic[1] != header_->e_ident[llvm::ELF::EI_MAG1]) |
| 165 | || (llvm::ELF::ElfMagic[2] != header_->e_ident[llvm::ELF::EI_MAG2]) |
| 166 | || (llvm::ELF::ElfMagic[3] != header_->e_ident[llvm::ELF::EI_MAG3])) { |
| 167 | LOG(WARNING) << "Failed to find ELF magic in " << file_->GetPath() |
| 168 | << ": " << std::hex |
| 169 | << static_cast<uint8_t>(header_->e_ident[llvm::ELF::EI_MAG0]) |
| 170 | << static_cast<uint8_t>(header_->e_ident[llvm::ELF::EI_MAG1]) |
| 171 | << static_cast<uint8_t>(header_->e_ident[llvm::ELF::EI_MAG2]) |
| 172 | << static_cast<uint8_t>(header_->e_ident[llvm::ELF::EI_MAG3]); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | // TODO: remove these static_casts from enum when using -std=gnu++0x |
| 178 | CHECK_EQ(static_cast<unsigned char>(llvm::ELF::ELFCLASS32), header_->e_ident[llvm::ELF::EI_CLASS]) << file_->GetPath(); |
| 179 | CHECK_EQ(static_cast<unsigned char>(llvm::ELF::ELFDATA2LSB), header_->e_ident[llvm::ELF::EI_DATA]) << file_->GetPath(); |
| 180 | CHECK_EQ(static_cast<unsigned char>(llvm::ELF::EV_CURRENT), header_->e_ident[llvm::ELF::EI_VERSION]) << file_->GetPath(); |
| 181 | |
| 182 | // TODO: remove these static_casts from enum when using -std=gnu++0x |
| 183 | CHECK_EQ(static_cast<llvm::ELF::Elf32_Half>(llvm::ELF::ET_DYN), header_->e_type) << file_->GetPath(); |
| 184 | CHECK_EQ(static_cast<llvm::ELF::Elf32_Word>(llvm::ELF::EV_CURRENT), header_->e_version) << file_->GetPath(); |
| 185 | CHECK_EQ(0U, header_->e_entry) << file_->GetPath(); |
| 186 | |
| 187 | CHECK_NE(0U, header_->e_phoff) << file_->GetPath(); |
| 188 | CHECK_NE(0U, header_->e_shoff) << file_->GetPath(); |
| 189 | CHECK_NE(0U, header_->e_ehsize) << file_->GetPath(); |
| 190 | CHECK_NE(0U, header_->e_phentsize) << file_->GetPath(); |
| 191 | CHECK_NE(0U, header_->e_phnum) << file_->GetPath(); |
| 192 | CHECK_NE(0U, header_->e_shentsize) << file_->GetPath(); |
| 193 | CHECK_NE(0U, header_->e_shnum) << file_->GetPath(); |
| 194 | CHECK_NE(0U, header_->e_shstrndx) << file_->GetPath(); |
| 195 | CHECK_GE(header_->e_shnum, header_->e_shstrndx) << file_->GetPath(); |
| 196 | if (!program_header_only_) { |
| 197 | CHECK_GT(Size(), header_->e_phoff) << file_->GetPath(); |
| 198 | CHECK_GT(Size(), header_->e_shoff) << file_->GetPath(); |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | llvm::ELF::Elf32_Ehdr& ElfFile::GetHeader() { |
| 205 | CHECK(header_ != NULL); |
| 206 | return *header_; |
| 207 | } |
| 208 | |
| 209 | byte* ElfFile::GetProgramHeadersStart() { |
| 210 | CHECK(program_headers_start_ != NULL); |
| 211 | return program_headers_start_; |
| 212 | } |
| 213 | |
| 214 | byte* ElfFile::GetSectionHeadersStart() { |
| 215 | CHECK(section_headers_start_ != NULL); |
| 216 | return section_headers_start_; |
| 217 | } |
| 218 | |
| 219 | llvm::ELF::Elf32_Phdr& ElfFile::GetDynamicProgramHeader() { |
| 220 | CHECK(dynamic_program_header_ != NULL); |
| 221 | return *dynamic_program_header_; |
| 222 | } |
| 223 | |
| 224 | llvm::ELF::Elf32_Dyn* ElfFile::GetDynamicSectionStart() { |
| 225 | CHECK(dynamic_section_start_ != NULL); |
| 226 | return dynamic_section_start_; |
| 227 | } |
| 228 | |
| 229 | llvm::ELF::Elf32_Sym* ElfFile::GetSymbolSectionStart(llvm::ELF::Elf32_Word section_type) { |
| 230 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 231 | llvm::ELF::Elf32_Sym* symbol_section_start; |
| 232 | switch (section_type) { |
| 233 | case llvm::ELF::SHT_SYMTAB: { |
| 234 | symbol_section_start = symtab_section_start_; |
| 235 | break; |
| 236 | } |
| 237 | case llvm::ELF::SHT_DYNSYM: { |
| 238 | symbol_section_start = dynsym_section_start_; |
| 239 | break; |
| 240 | } |
| 241 | default: { |
| 242 | LOG(FATAL) << section_type; |
| 243 | symbol_section_start = NULL; |
| 244 | } |
| 245 | } |
| 246 | CHECK(symbol_section_start != NULL); |
| 247 | return symbol_section_start; |
| 248 | } |
| 249 | |
| 250 | char* ElfFile::GetSymbolStringSectionStart(llvm::ELF::Elf32_Word section_type) { |
| 251 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 252 | char* string_section_start; |
| 253 | switch (section_type) { |
| 254 | case llvm::ELF::SHT_SYMTAB: { |
| 255 | string_section_start = strtab_section_start_; |
| 256 | break; |
| 257 | } |
| 258 | case llvm::ELF::SHT_DYNSYM: { |
| 259 | string_section_start = dynstr_section_start_; |
| 260 | break; |
| 261 | } |
| 262 | default: { |
| 263 | LOG(FATAL) << section_type; |
| 264 | string_section_start = NULL; |
| 265 | } |
| 266 | } |
| 267 | CHECK(string_section_start != NULL); |
| 268 | return string_section_start; |
| 269 | } |
| 270 | |
| 271 | llvm::ELF::Elf32_Word* ElfFile::GetHashSectionStart() { |
| 272 | CHECK(hash_section_start_ != NULL); |
| 273 | return hash_section_start_; |
| 274 | } |
| 275 | |
| 276 | llvm::ELF::Elf32_Word ElfFile::GetHashBucketNum() { |
| 277 | return GetHashSectionStart()[0]; |
| 278 | } |
| 279 | |
| 280 | llvm::ELF::Elf32_Word ElfFile::GetHashChainNum() { |
| 281 | return GetHashSectionStart()[1]; |
| 282 | } |
| 283 | |
| 284 | llvm::ELF::Elf32_Word ElfFile::GetHashBucket(size_t i) { |
| 285 | CHECK_LT(i, GetHashBucketNum()); |
| 286 | // 0 is nbucket, 1 is nchain |
| 287 | return GetHashSectionStart()[2 + i]; |
| 288 | } |
| 289 | |
| 290 | llvm::ELF::Elf32_Word ElfFile::GetHashChain(size_t i) { |
| 291 | CHECK_LT(i, GetHashChainNum()); |
| 292 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 293 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 294 | } |
| 295 | |
| 296 | llvm::ELF::Elf32_Word ElfFile::GetProgramHeaderNum() { |
| 297 | return GetHeader().e_phnum; |
| 298 | } |
| 299 | |
| 300 | llvm::ELF::Elf32_Phdr& ElfFile::GetProgramHeader(llvm::ELF::Elf32_Word i) { |
| 301 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); |
| 302 | byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
| 303 | CHECK_LT(program_header, End()) << file_->GetPath(); |
| 304 | return *reinterpret_cast<llvm::ELF::Elf32_Phdr*>(program_header); |
| 305 | } |
| 306 | |
| 307 | llvm::ELF::Elf32_Phdr* ElfFile::FindProgamHeaderByType(llvm::ELF::Elf32_Word type) { |
| 308 | for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 309 | llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i); |
| 310 | if (program_header.p_type == type) { |
| 311 | return &program_header; |
| 312 | } |
| 313 | } |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | llvm::ELF::Elf32_Word ElfFile::GetSectionHeaderNum() { |
| 318 | return GetHeader().e_shnum; |
| 319 | } |
| 320 | |
| 321 | llvm::ELF::Elf32_Shdr& ElfFile::GetSectionHeader(llvm::ELF::Elf32_Word i) { |
| 322 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 323 | // Even if we Load(), it doesn't bring in all the sections. |
| 324 | CHECK(!program_header_only_) << file_->GetPath(); |
| 325 | CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath(); |
| 326 | byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
| 327 | CHECK_LT(section_header, End()) << file_->GetPath(); |
| 328 | return *reinterpret_cast<llvm::ELF::Elf32_Shdr*>(section_header); |
| 329 | } |
| 330 | |
| 331 | llvm::ELF::Elf32_Shdr* ElfFile::FindSectionByType(llvm::ELF::Elf32_Word type) { |
| 332 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 333 | // We could change this to switch on known types if they were detected during loading. |
| 334 | CHECK(!program_header_only_) << file_->GetPath(); |
| 335 | for (llvm::ELF::Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 336 | llvm::ELF::Elf32_Shdr& section_header = GetSectionHeader(i); |
| 337 | if (section_header.sh_type == type) { |
| 338 | return §ion_header; |
| 339 | } |
| 340 | } |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | // from bionic |
| 345 | static unsigned elfhash(const char *_name) |
| 346 | { |
| 347 | const unsigned char *name = (const unsigned char *) _name; |
| 348 | unsigned h = 0, g; |
| 349 | |
| 350 | while(*name) { |
| 351 | h = (h << 4) + *name++; |
| 352 | g = h & 0xf0000000; |
| 353 | h ^= g; |
| 354 | h ^= g >> 24; |
| 355 | } |
| 356 | return h; |
| 357 | } |
| 358 | |
| 359 | byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) { |
| 360 | llvm::ELF::Elf32_Word hash = elfhash(symbol_name.c_str()); |
| 361 | llvm::ELF::Elf32_Word bucket_index = hash % GetHashBucketNum(); |
| 362 | llvm::ELF::Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index); |
| 363 | char* symbol_string_section_start = GetSymbolStringSectionStart(llvm::ELF::SHT_DYNSYM); |
| 364 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
| 365 | llvm::ELF::Elf32_Sym& symbol = GetSymbol(llvm::ELF::SHT_DYNSYM, symbol_and_chain_index); |
| 366 | char* name = symbol_string_section_start + symbol.st_name; |
| 367 | if (symbol_name == name) { |
| 368 | return base_address_ + symbol.st_value; |
| 369 | } |
| 370 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index); |
| 371 | } |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | bool ElfFile::IsSymbolSectionType(llvm::ELF::Elf32_Word section_type) { |
| 376 | return ((section_type == llvm::ELF::SHT_SYMTAB) || (section_type == llvm::ELF::SHT_DYNSYM)); |
| 377 | } |
| 378 | |
| 379 | llvm::ELF::Elf32_Word ElfFile::GetSymbolNum(llvm::ELF::Elf32_Shdr& section_header) { |
| 380 | CHECK(IsSymbolSectionType(section_header.sh_type)) << file_->GetPath() << " " << section_header.sh_type; |
| 381 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 382 | return section_header.sh_size / section_header.sh_entsize; |
| 383 | } |
| 384 | |
| 385 | llvm::ELF::Elf32_Sym& ElfFile::GetSymbol(llvm::ELF::Elf32_Word section_type, |
| 386 | llvm::ELF::Elf32_Word i) { |
| 387 | return *(GetSymbolSectionStart(section_type) + i); |
| 388 | } |
| 389 | |
| 390 | llvm::ELF::Elf32_Sym* ElfFile::FindSymbolByName(llvm::ELF::Elf32_Word section_type, |
| 391 | const std::string& symbol_name) { |
| 392 | CHECK(!program_header_only_) << file_->GetPath(); |
| 393 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 394 | llvm::ELF::Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
| 395 | CHECK(symbol_section != NULL) << file_->GetPath(); |
| 396 | llvm::ELF::Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
| 397 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
| 398 | llvm::ELF::Elf32_Sym& symbol = GetSymbol(section_type, i); |
| 399 | const char* name = GetString(string_section, symbol.st_name); |
| 400 | if (name == NULL) { |
| 401 | continue; |
| 402 | } |
| 403 | if (symbol_name == name) { |
| 404 | return &symbol; |
| 405 | } |
| 406 | } |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | llvm::ELF::Elf32_Addr ElfFile::FindSymbolAddress(llvm::ELF::Elf32_Word section_type, |
| 411 | const std::string& symbol_name) { |
| 412 | llvm::ELF::Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name); |
| 413 | if (symbol == NULL) { |
| 414 | return 0; |
| 415 | } |
| 416 | return symbol->st_value; |
| 417 | } |
| 418 | |
| 419 | char* ElfFile::GetString(llvm::ELF::Elf32_Shdr& string_section, llvm::ELF::Elf32_Word i) { |
| 420 | CHECK(!program_header_only_) << file_->GetPath(); |
| 421 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
| 422 | CHECK_EQ(static_cast<llvm::ELF::Elf32_Word>(llvm::ELF::SHT_STRTAB), string_section.sh_type) << file_->GetPath(); |
| 423 | CHECK_LT(i, string_section.sh_size) << file_->GetPath(); |
| 424 | if (i == 0) { |
| 425 | return NULL; |
| 426 | } |
| 427 | byte* strings = Begin() + string_section.sh_offset; |
| 428 | byte* string = strings + i; |
| 429 | CHECK_LT(string, End()) << file_->GetPath(); |
| 430 | return reinterpret_cast<char*>(string); |
| 431 | } |
| 432 | |
| 433 | llvm::ELF::Elf32_Word ElfFile::GetDynamicNum() { |
| 434 | return GetDynamicProgramHeader().p_filesz / sizeof(llvm::ELF::Elf32_Dyn); |
| 435 | } |
| 436 | |
| 437 | llvm::ELF::Elf32_Dyn& ElfFile::GetDynamic(llvm::ELF::Elf32_Word i) { |
| 438 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 439 | return *(GetDynamicSectionStart() + i); |
| 440 | } |
| 441 | |
| 442 | // Base on bionic phdr_table_get_load_size |
| 443 | size_t ElfFile::GetLoadedSize() { |
| 444 | llvm::ELF::Elf32_Addr min_vaddr = 0xFFFFFFFFu; |
| 445 | llvm::ELF::Elf32_Addr max_vaddr = 0x00000000u; |
| 446 | for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 447 | llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i); |
| 448 | if (program_header.p_type != llvm::ELF::PT_LOAD) { |
| 449 | continue; |
| 450 | } |
| 451 | llvm::ELF::Elf32_Addr begin_vaddr = program_header.p_vaddr; |
| 452 | if (begin_vaddr < min_vaddr) { |
| 453 | min_vaddr = begin_vaddr; |
| 454 | } |
| 455 | llvm::ELF::Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz; |
| 456 | if (end_vaddr > max_vaddr) { |
| 457 | max_vaddr = end_vaddr; |
| 458 | } |
| 459 | } |
| 460 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 461 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 462 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 463 | size_t loaded_size = max_vaddr - min_vaddr; |
| 464 | return loaded_size; |
| 465 | } |
| 466 | |
| 467 | bool ElfFile::Load() { |
| 468 | // TODO: actually return false error |
| 469 | CHECK(program_header_only_) << file_->GetPath(); |
| 470 | for (llvm::ELF::Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 471 | llvm::ELF::Elf32_Phdr& program_header = GetProgramHeader(i); |
| 472 | |
| 473 | // Record .dynamic header information for later use |
| 474 | if (program_header.p_type == llvm::ELF::PT_DYNAMIC) { |
| 475 | dynamic_program_header_ = &program_header; |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | // Not something to load, move on. |
| 480 | if (program_header.p_type != llvm::ELF::PT_LOAD) { |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | // Found something to load. |
| 485 | |
| 486 | // If p_vaddr is zero, it must be the first loadable segment, |
| 487 | // since they must be in order. Since it is zero, there isn't a |
| 488 | // specific address requested, so first request a contiguous chunk |
| 489 | // of required size for all segments, but with no |
| 490 | // permissions. We'll then carve that up with the proper |
| 491 | // permissions as we load the actual segments. If p_vaddr is |
| 492 | // non-zero, the segments require the specific address specified, |
| 493 | // which either was specified in the file because we already set |
| 494 | // base_address_ after the first zero segment). |
| 495 | if (program_header.p_vaddr == 0) { |
| 496 | std::string reservation_name("ElfFile reservation for "); |
| 497 | reservation_name += file_->GetPath(); |
| 498 | UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
| 499 | NULL, GetLoadedSize(), PROT_NONE)); |
| 500 | CHECK(reserve.get() != NULL) << file_->GetPath(); |
| 501 | base_address_ = reserve->Begin(); |
| 502 | segments_.push_back(reserve.release()); |
| 503 | } |
| 504 | byte* p_vaddr = base_address_ + program_header.p_vaddr; |
| 505 | int prot = 0; |
| 506 | if ((program_header.p_flags & llvm::ELF::PF_X) != 0) { |
| 507 | prot |= PROT_EXEC; |
| 508 | } |
| 509 | if ((program_header.p_flags & llvm::ELF::PF_W) != 0) { |
| 510 | prot |= PROT_WRITE; |
| 511 | } |
| 512 | if ((program_header.p_flags & llvm::ELF::PF_R) != 0) { |
| 513 | prot |= PROT_READ; |
| 514 | } |
| 515 | int flags = MAP_FIXED; |
| 516 | if (writable_) { |
| 517 | prot |= PROT_WRITE; |
| 518 | flags |= MAP_SHARED; |
| 519 | } else { |
| 520 | flags |= MAP_PRIVATE; |
| 521 | } |
| 522 | UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
| 523 | program_header.p_memsz, |
| 524 | prot, flags, file_->Fd(), |
| 525 | program_header.p_offset, |
| 526 | true)); |
| 527 | CHECK(segment.get() != NULL) << file_->GetPath(); |
| 528 | CHECK_EQ(segment->Begin(), p_vaddr) << file_->GetPath(); |
| 529 | segments_.push_back(segment.release()); |
| 530 | } |
| 531 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
| 532 | dynamic_section_start_ |
| 533 | = reinterpret_cast<llvm::ELF::Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr); |
| 534 | for (llvm::ELF::Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 535 | llvm::ELF::Elf32_Dyn& elf_dyn = GetDynamic(i); |
| 536 | byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
| 537 | switch (elf_dyn.d_tag) { |
| 538 | case llvm::ELF::DT_HASH: { |
| 539 | hash_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Word*>(d_ptr); |
| 540 | break; |
| 541 | } |
| 542 | case llvm::ELF::DT_STRTAB: { |
| 543 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 544 | break; |
| 545 | } |
| 546 | case llvm::ELF::DT_SYMTAB: { |
| 547 | dynsym_section_start_ = reinterpret_cast<llvm::ELF::Elf32_Sym*>(d_ptr); |
| 548 | break; |
| 549 | } |
| 550 | case llvm::ELF::DT_NULL: { |
| 551 | CHECK_EQ(GetDynamicNum(), i+1); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | } // namespace art |