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