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