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 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Nicolas Geoffray | a7f198c | 2014-03-10 11:12:54 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 25 | #include "base/stringprintf.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 28 | #include "elf_file_impl.h" |
| 29 | #include "elf_utils.h" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 30 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 35 | template <typename ElfTypes> |
| 36 | ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable, |
| 37 | bool program_header_only, |
| 38 | uint8_t* requested_base) |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 39 | : file_(file), |
| 40 | writable_(writable), |
| 41 | program_header_only_(program_header_only), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 42 | header_(nullptr), |
| 43 | base_address_(nullptr), |
| 44 | program_headers_start_(nullptr), |
| 45 | section_headers_start_(nullptr), |
| 46 | dynamic_program_header_(nullptr), |
| 47 | dynamic_section_start_(nullptr), |
| 48 | symtab_section_start_(nullptr), |
| 49 | dynsym_section_start_(nullptr), |
| 50 | strtab_section_start_(nullptr), |
| 51 | dynstr_section_start_(nullptr), |
| 52 | hash_section_start_(nullptr), |
| 53 | symtab_symbol_table_(nullptr), |
| 54 | dynsym_symbol_table_(nullptr), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 55 | requested_base_(requested_base) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 56 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 57 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 58 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 59 | template <typename ElfTypes> |
| 60 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open( |
| 61 | File* file, bool writable, bool program_header_only, |
| 62 | std::string* error_msg, uint8_t* requested_base) { |
| 63 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes> |
| 64 | (file, writable, program_header_only, requested_base)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 65 | int prot; |
| 66 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 67 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 68 | prot = PROT_READ | PROT_WRITE; |
| 69 | flags = MAP_SHARED; |
| 70 | } else { |
| 71 | prot = PROT_READ; |
| 72 | flags = MAP_PRIVATE; |
| 73 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 74 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 75 | return nullptr; |
| 76 | } |
| 77 | return elf_file.release(); |
| 78 | } |
| 79 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 80 | template <typename ElfTypes> |
| 81 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open( |
| 82 | File* file, int prot, int flags, std::string* error_msg) { |
| 83 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes> |
| 84 | (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false, |
| 85 | /*requested_base*/nullptr)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 86 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 87 | return nullptr; |
| 88 | } |
| 89 | return elf_file.release(); |
| 90 | } |
| 91 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 92 | template <typename ElfTypes> |
| 93 | bool ElfFileImpl<ElfTypes>::Setup(int prot, int flags, std::string* error_msg) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 94 | int64_t temp_file_length = file_->GetLength(); |
| 95 | if (temp_file_length < 0) { |
| 96 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 97 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 98 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 101 | size_t file_length = static_cast<size_t>(temp_file_length); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 102 | if (file_length < sizeof(Elf_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 103 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of " |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 104 | "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 105 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 109 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 110 | // first just map ELF header to get program header size information |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 111 | size_t elf_header_size = sizeof(Elf_Ehdr); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 112 | if (!SetMap(MemMap::MapFile(elf_header_size, |
| 113 | prot, |
| 114 | flags, |
| 115 | file_->Fd(), |
| 116 | 0, |
| 117 | /*low4_gb*/false, |
| 118 | file_->GetPath().c_str(), |
| 119 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 120 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | // then remap to cover program header |
| 124 | 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] | 125 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 126 | *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] | 127 | "header of %zd bytes: '%s'", file_length, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 128 | sizeof(Elf_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 131 | if (!SetMap(MemMap::MapFile(program_header_size, |
| 132 | prot, |
| 133 | flags, |
| 134 | file_->Fd(), |
| 135 | 0, |
| 136 | /*low4_gb*/false, |
| 137 | file_->GetPath().c_str(), |
| 138 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 139 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 140 | *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] | 141 | return false; |
| 142 | } |
| 143 | } else { |
| 144 | // otherwise map entire file |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 145 | if (!SetMap(MemMap::MapFile(file_->GetLength(), |
| 146 | prot, |
| 147 | flags, |
| 148 | file_->Fd(), |
| 149 | 0, |
| 150 | /*low4_gb*/false, |
| 151 | file_->GetPath().c_str(), |
| 152 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 153 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 154 | *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] | 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 159 | if (program_header_only_) { |
| 160 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 161 | } else { |
| 162 | if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) { |
| 163 | return false; |
| 164 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 165 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 166 | // Setup section headers. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 167 | if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Find shstrtab. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 172 | Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 173 | if (shstrtab_section_header == nullptr) { |
| 174 | *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'", |
| 175 | file_->GetPath().c_str()); |
| 176 | return false; |
| 177 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 178 | |
| 179 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 180 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 181 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 182 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 183 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 187 | if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 188 | reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 191 | |
| 192 | // Find other sections from section headers |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 193 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 194 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 195 | if (section_header == nullptr) { |
| 196 | *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'", |
| 197 | i, file_->GetPath().c_str()); |
| 198 | return false; |
| 199 | } |
| 200 | switch (section_header->sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 201 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 202 | if (!CheckAndSet(section_header->sh_offset, "symtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 203 | reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 206 | break; |
| 207 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 208 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 209 | if (!CheckAndSet(section_header->sh_offset, "dynsym", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 210 | reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 213 | break; |
| 214 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 215 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 216 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 217 | if ((section_header->sh_flags & SHF_ALLOC) != 0) { |
| 218 | // Check that this is named ".dynstr" and ignore otherwise. |
| 219 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 220 | if (strncmp(".dynstr", header_name, 8) == 0) { |
| 221 | if (!CheckAndSet(section_header->sh_offset, "dynstr", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 222 | reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 226 | } else { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 227 | // Check that this is named ".strtab" and ignore otherwise. |
| 228 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 229 | if (strncmp(".strtab", header_name, 8) == 0) { |
| 230 | if (!CheckAndSet(section_header->sh_offset, "strtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 231 | reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 238 | case SHT_DYNAMIC: { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 239 | if (reinterpret_cast<uint8_t*>(dynamic_section_start_) != |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 240 | Begin() + section_header->sh_offset) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 241 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 242 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 243 | << reinterpret_cast<void*>(dynamic_section_start_) |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 244 | << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | break; |
| 248 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 249 | case SHT_HASH: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 250 | if (!CheckAndSet(section_header->sh_offset, "hash section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 251 | reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 252 | return false; |
| 253 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 258 | |
| 259 | // Check for the existence of some sections. |
| 260 | if (!CheckSectionsExist(error_msg)) { |
| 261 | return false; |
| 262 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 263 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 264 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 268 | template <typename ElfTypes> |
| 269 | ElfFileImpl<ElfTypes>::~ElfFileImpl() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 270 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 271 | delete symtab_symbol_table_; |
| 272 | delete dynsym_symbol_table_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 273 | } |
| 274 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 275 | template <typename ElfTypes> |
| 276 | bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label, |
| 277 | uint8_t** target, std::string* error_msg) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 278 | if (Begin() + offset >= End()) { |
| 279 | *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label, |
| 280 | file_->GetPath().c_str()); |
| 281 | return false; |
| 282 | } |
| 283 | *target = Begin() + offset; |
| 284 | return true; |
| 285 | } |
| 286 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 287 | template <typename ElfTypes> |
| 288 | bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source, |
| 289 | const uint8_t* target) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 290 | // Only works in whole-program mode, as we need to iterate over the sections. |
| 291 | // Note that we normally can't search by type, as duplicates are allowed for most section types. |
| 292 | if (program_header_only_) { |
| 293 | return true; |
| 294 | } |
| 295 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 296 | Elf_Shdr* source_section = nullptr; |
| 297 | Elf_Word target_index = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 298 | bool target_found = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 299 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 300 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 301 | |
| 302 | if (Begin() + section_header->sh_offset == source) { |
| 303 | // Found the source. |
| 304 | source_section = section_header; |
| 305 | if (target_index) { |
| 306 | break; |
| 307 | } |
| 308 | } else if (Begin() + section_header->sh_offset == target) { |
| 309 | target_index = i; |
| 310 | target_found = true; |
| 311 | if (source_section != nullptr) { |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return target_found && source_section != nullptr && source_section->sh_link == target_index; |
| 318 | } |
| 319 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 320 | template <typename ElfTypes> |
| 321 | bool ElfFileImpl<ElfTypes>::CheckSectionsExist(std::string* error_msg) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 322 | if (!program_header_only_) { |
| 323 | // If in full mode, need section headers. |
| 324 | if (section_headers_start_ == nullptr) { |
| 325 | *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str()); |
| 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // This is redundant, but defensive. |
| 331 | if (dynamic_program_header_ == nullptr) { |
| 332 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 333 | file_->GetPath().c_str()); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | // Need a dynamic section. This is redundant, but defensive. |
| 338 | if (dynamic_section_start_ == nullptr) { |
| 339 | *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'", |
| 340 | file_->GetPath().c_str()); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // Symtab validation. These is not really a hard failure, as we are currently not using the |
| 345 | // symtab internally, but it's nice to be defensive. |
| 346 | if (symtab_section_start_ != nullptr) { |
| 347 | // When there's a symtab, there should be a strtab. |
| 348 | if (strtab_section_start_ == nullptr) { |
| 349 | *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str()); |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | // The symtab should link to the strtab. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 354 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_), |
| 355 | reinterpret_cast<const uint8_t*>(strtab_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 356 | *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'", |
| 357 | file_->GetPath().c_str()); |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // We always need a dynstr & dynsym. |
| 363 | if (dynstr_section_start_ == nullptr) { |
| 364 | *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str()); |
| 365 | return false; |
| 366 | } |
| 367 | if (dynsym_section_start_ == nullptr) { |
| 368 | *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str()); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | // Need a hash section for dynamic symbol lookup. |
| 373 | if (hash_section_start_ == nullptr) { |
| 374 | *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'", |
| 375 | file_->GetPath().c_str()); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | // And the hash section should be linking to the dynsym. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 380 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_), |
| 381 | reinterpret_cast<const uint8_t*>(dynsym_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 382 | *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'", |
| 383 | file_->GetPath().c_str()); |
| 384 | return false; |
| 385 | } |
| 386 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 387 | // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for |
| 388 | // us). This is usually the last in an oat file, and a good indicator of whether writing was |
| 389 | // successful (or the process crashed and left garbage). |
| 390 | if (program_header_only_) { |
| 391 | // It might not be mapped, but we can compare against the file size. |
| 392 | int64_t offset = static_cast<int64_t>(GetHeader().e_shoff + |
| 393 | (GetHeader().e_shstrndx * GetHeader().e_shentsize)); |
| 394 | if (offset >= file_->GetLength()) { |
| 395 | *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'", |
| 396 | file_->GetPath().c_str()); |
| 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 404 | template <typename ElfTypes> |
| 405 | bool ElfFileImpl<ElfTypes>::SetMap(MemMap* map, std::string* error_msg) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 406 | if (map == nullptr) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 407 | // MemMap::Open should have already set an error. |
| 408 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | map_.reset(map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 412 | CHECK(map_.get() != nullptr) << file_->GetPath(); |
| 413 | CHECK(map_->Begin() != nullptr) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 414 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 415 | header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin()); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 416 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 417 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 418 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 419 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 420 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 421 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 422 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 423 | header_->e_ident[EI_MAG0], |
| 424 | header_->e_ident[EI_MAG1], |
| 425 | header_->e_ident[EI_MAG2], |
| 426 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 427 | return false; |
| 428 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 429 | uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32; |
| 430 | if (elf_class != header_->e_ident[EI_CLASS]) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 431 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 432 | elf_class, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 433 | file_->GetPath().c_str(), |
| 434 | header_->e_ident[EI_CLASS]); |
| 435 | return false; |
| 436 | } |
| 437 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 438 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 439 | ELFDATA2LSB, |
| 440 | file_->GetPath().c_str(), |
| 441 | header_->e_ident[EI_CLASS]); |
| 442 | return false; |
| 443 | } |
| 444 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 445 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 446 | EV_CURRENT, |
| 447 | file_->GetPath().c_str(), |
| 448 | header_->e_ident[EI_CLASS]); |
| 449 | return false; |
| 450 | } |
| 451 | if (ET_DYN != header_->e_type) { |
| 452 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 453 | ET_DYN, |
| 454 | file_->GetPath().c_str(), |
| 455 | header_->e_type); |
| 456 | return false; |
| 457 | } |
| 458 | if (EV_CURRENT != header_->e_version) { |
| 459 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 460 | EV_CURRENT, |
| 461 | file_->GetPath().c_str(), |
| 462 | header_->e_version); |
| 463 | return false; |
| 464 | } |
| 465 | if (0 != header_->e_entry) { |
| 466 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 467 | 0, |
| 468 | file_->GetPath().c_str(), |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 469 | static_cast<int32_t>(header_->e_entry)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | if (0 == header_->e_phoff) { |
| 473 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 474 | file_->GetPath().c_str()); |
| 475 | return false; |
| 476 | } |
| 477 | if (0 == header_->e_shoff) { |
| 478 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 479 | file_->GetPath().c_str()); |
| 480 | return false; |
| 481 | } |
| 482 | if (0 == header_->e_ehsize) { |
| 483 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 484 | file_->GetPath().c_str()); |
| 485 | return false; |
| 486 | } |
| 487 | if (0 == header_->e_phentsize) { |
| 488 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 489 | file_->GetPath().c_str()); |
| 490 | return false; |
| 491 | } |
| 492 | if (0 == header_->e_phnum) { |
| 493 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 494 | file_->GetPath().c_str()); |
| 495 | return false; |
| 496 | } |
| 497 | if (0 == header_->e_shentsize) { |
| 498 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 499 | file_->GetPath().c_str()); |
| 500 | return false; |
| 501 | } |
| 502 | if (0 == header_->e_shnum) { |
| 503 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 504 | file_->GetPath().c_str()); |
| 505 | return false; |
| 506 | } |
| 507 | if (0 == header_->e_shstrndx) { |
| 508 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 509 | file_->GetPath().c_str()); |
| 510 | return false; |
| 511 | } |
| 512 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 513 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 514 | header_->e_shstrndx, |
| 515 | header_->e_shnum, |
| 516 | file_->GetPath().c_str()); |
| 517 | return false; |
| 518 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 519 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 520 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 521 | if (header_->e_phoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 522 | *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s", |
| 523 | static_cast<uint64_t>(header_->e_phoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 524 | Size(), |
| 525 | file_->GetPath().c_str()); |
| 526 | return false; |
| 527 | } |
| 528 | if (header_->e_shoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 529 | *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s", |
| 530 | static_cast<uint64_t>(header_->e_shoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 531 | Size(), |
| 532 | file_->GetPath().c_str()); |
| 533 | return false; |
| 534 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 535 | } |
| 536 | return true; |
| 537 | } |
| 538 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 539 | template <typename ElfTypes> |
| 540 | typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 541 | CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 542 | return *header_; |
| 543 | } |
| 544 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 545 | template <typename ElfTypes> |
| 546 | uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 547 | CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity |
| 548 | // check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 549 | return program_headers_start_; |
| 550 | } |
| 551 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 552 | template <typename ElfTypes> |
| 553 | uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 554 | CHECK(!program_header_only_); // Only used in "full" mode. |
| 555 | CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 556 | return section_headers_start_; |
| 557 | } |
| 558 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 559 | template <typename ElfTypes> |
| 560 | typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 561 | CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 562 | return *dynamic_program_header_; |
| 563 | } |
| 564 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 565 | template <typename ElfTypes> |
| 566 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 567 | CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 568 | return dynamic_section_start_; |
| 569 | } |
| 570 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 571 | template <typename ElfTypes> |
| 572 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart( |
| 573 | Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 574 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 575 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 576 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 577 | return symtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 578 | break; |
| 579 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 580 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 581 | return dynsym_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | default: { |
| 585 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 586 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 589 | } |
| 590 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 591 | template <typename ElfTypes> |
| 592 | const char* ElfFileImpl<ElfTypes>::GetStringSectionStart( |
| 593 | Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 594 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 595 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 596 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 597 | return strtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 598 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 599 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 600 | return dynstr_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 601 | } |
| 602 | default: { |
| 603 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 604 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 607 | } |
| 608 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 609 | template <typename ElfTypes> |
| 610 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type, |
| 611 | Elf_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 612 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 613 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 614 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 615 | } |
| 616 | const char* string_section_start = GetStringSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 617 | if (string_section_start == nullptr) { |
| 618 | return nullptr; |
| 619 | } |
| 620 | return string_section_start + i; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 623 | // WARNING: The following methods do not check for an error condition (non-existent hash section). |
| 624 | // It is the caller's job to do this. |
| 625 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 626 | template <typename ElfTypes> |
| 627 | typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 628 | return hash_section_start_; |
| 629 | } |
| 630 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 631 | template <typename ElfTypes> |
| 632 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 633 | return GetHashSectionStart()[0]; |
| 634 | } |
| 635 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 636 | template <typename ElfTypes> |
| 637 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 638 | return GetHashSectionStart()[1]; |
| 639 | } |
| 640 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 641 | template <typename ElfTypes> |
| 642 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 643 | if (i >= GetHashBucketNum()) { |
| 644 | *ok = false; |
| 645 | return 0; |
| 646 | } |
| 647 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 648 | // 0 is nbucket, 1 is nchain |
| 649 | return GetHashSectionStart()[2 + i]; |
| 650 | } |
| 651 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 652 | template <typename ElfTypes> |
| 653 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const { |
Yevgeny Rouban | acb0138 | 2014-11-24 13:40:56 +0600 | [diff] [blame] | 654 | if (i >= GetHashChainNum()) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 655 | *ok = false; |
| 656 | return 0; |
| 657 | } |
| 658 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 659 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 660 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 661 | } |
| 662 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 663 | template <typename ElfTypes> |
| 664 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 665 | return GetHeader().e_phnum; |
| 666 | } |
| 667 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 668 | template <typename ElfTypes> |
| 669 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 670 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 671 | uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 672 | if (program_header >= End()) { |
| 673 | return nullptr; // Failure condition. |
| 674 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 675 | return reinterpret_cast<Elf_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 676 | } |
| 677 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 678 | template <typename ElfTypes> |
| 679 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 680 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 681 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 682 | if (program_header->p_type == type) { |
| 683 | return program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 686 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 687 | } |
| 688 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 689 | template <typename ElfTypes> |
| 690 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 691 | return GetHeader().e_shnum; |
| 692 | } |
| 693 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 694 | template <typename ElfTypes> |
| 695 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 696 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 697 | // Even if we Load(), it doesn't bring in all the sections. |
| 698 | CHECK(!program_header_only_) << file_->GetPath(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 699 | if (i >= GetSectionHeaderNum()) { |
| 700 | return nullptr; // Failure condition. |
| 701 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 702 | uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 703 | if (section_header >= End()) { |
| 704 | return nullptr; // Failure condition. |
| 705 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 706 | return reinterpret_cast<Elf_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 707 | } |
| 708 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 709 | template <typename ElfTypes> |
| 710 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 711 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 712 | // We could change this to switch on known types if they were detected during loading. |
| 713 | CHECK(!program_header_only_) << file_->GetPath(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 714 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 715 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 716 | if (section_header->sh_type == type) { |
| 717 | return section_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 720 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 724 | static unsigned elfhash(const char *_name) { |
| 725 | const unsigned char *name = (const unsigned char *) _name; |
| 726 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 727 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 728 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 729 | h = (h << 4) + *name++; |
| 730 | g = h & 0xf0000000; |
| 731 | h ^= g; |
| 732 | h ^= g >> 24; |
| 733 | } |
| 734 | return h; |
| 735 | } |
| 736 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 737 | template <typename ElfTypes> |
| 738 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 739 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 740 | } |
| 741 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 742 | template <typename ElfTypes> |
| 743 | const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress( |
| 744 | const std::string& symbol_name) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 745 | // Check that we have a hash section. |
| 746 | if (GetHashSectionStart() == nullptr) { |
| 747 | return nullptr; // Failure condition. |
| 748 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 749 | const Elf_Sym* sym = FindDynamicSymbol(symbol_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 750 | if (sym != nullptr) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 751 | // TODO: we need to change this to calculate base_address_ in ::Open, |
| 752 | // otherwise it will be wrongly 0 if ::Load has not yet been called. |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 753 | return base_address_ + sym->st_value; |
| 754 | } else { |
| 755 | return nullptr; |
| 756 | } |
| 757 | } |
| 758 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 759 | // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 760 | template <typename ElfTypes> |
| 761 | const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol( |
| 762 | const std::string& symbol_name) const { |
Andreas Gampe | c48b206 | 2014-09-08 23:39:45 -0700 | [diff] [blame] | 763 | if (GetHashBucketNum() == 0) { |
| 764 | // No dynamic symbols at all. |
| 765 | return nullptr; |
| 766 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 767 | Elf_Word hash = elfhash(symbol_name.c_str()); |
| 768 | Elf_Word bucket_index = hash % GetHashBucketNum(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 769 | bool ok; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 770 | Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 771 | if (!ok) { |
| 772 | return nullptr; |
| 773 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 774 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 775 | Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 776 | if (symbol == nullptr) { |
| 777 | return nullptr; // Failure condition. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 778 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 779 | const char* name = GetString(SHT_DYNSYM, symbol->st_name); |
| 780 | if (symbol_name == name) { |
| 781 | return symbol; |
| 782 | } |
| 783 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok); |
| 784 | if (!ok) { |
| 785 | return nullptr; |
| 786 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 787 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 788 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 789 | } |
| 790 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 791 | template <typename ElfTypes> |
| 792 | bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 793 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
| 794 | } |
| 795 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 796 | template <typename ElfTypes> |
| 797 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 798 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 799 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 800 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 801 | return section_header.sh_size / section_header.sh_entsize; |
| 802 | } |
| 803 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 804 | template <typename ElfTypes> |
| 805 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 806 | Elf_Sym* sym_start = GetSymbolSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 807 | if (sym_start == nullptr) { |
| 808 | return nullptr; |
| 809 | } |
| 810 | return sym_start + i; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 811 | } |
| 812 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 813 | template <typename ElfTypes> |
| 814 | typename ElfFileImpl<ElfTypes>::SymbolTable** |
| 815 | ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 816 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 817 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 818 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 819 | return &symtab_symbol_table_; |
| 820 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 821 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 822 | return &dynsym_symbol_table_; |
| 823 | } |
| 824 | default: { |
| 825 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 826 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 831 | template <typename ElfTypes> |
| 832 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName( |
| 833 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 834 | CHECK(!program_header_only_) << file_->GetPath(); |
| 835 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 836 | |
| 837 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 838 | if (*symbol_table != nullptr || build_map) { |
| 839 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 840 | DCHECK(build_map); |
| 841 | *symbol_table = new SymbolTable; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 842 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 843 | if (symbol_section == nullptr) { |
| 844 | return nullptr; // Failure condition. |
| 845 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 846 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 847 | if (string_section == nullptr) { |
| 848 | return nullptr; // Failure condition. |
| 849 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 850 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 851 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 852 | if (symbol == nullptr) { |
| 853 | return nullptr; // Failure condition. |
| 854 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 855 | unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) |
| 856 | ? ELF64_ST_TYPE(symbol->st_info) |
| 857 | : ELF32_ST_TYPE(symbol->st_info); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 858 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 859 | continue; |
| 860 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 861 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 862 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 863 | continue; |
| 864 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 865 | std::pair<typename SymbolTable::iterator, bool> result = |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 866 | (*symbol_table)->insert(std::make_pair(name, symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 867 | if (!result.second) { |
| 868 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 869 | if ((symbol->st_value != result.first->second->st_value) || |
| 870 | (symbol->st_size != result.first->second->st_size) || |
| 871 | (symbol->st_info != result.first->second->st_info) || |
| 872 | (symbol->st_other != result.first->second->st_other) || |
| 873 | (symbol->st_shndx != result.first->second->st_shndx)) { |
| 874 | return nullptr; // Failure condition. |
| 875 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 879 | CHECK(*symbol_table != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 880 | typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 881 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 882 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 883 | } |
| 884 | return it->second; |
| 885 | } |
| 886 | |
| 887 | // Fall back to linear search |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 888 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 889 | if (symbol_section == nullptr) { |
| 890 | return nullptr; |
| 891 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 892 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 893 | if (string_section == nullptr) { |
| 894 | return nullptr; |
| 895 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 896 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 897 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 898 | if (symbol == nullptr) { |
| 899 | return nullptr; // Failure condition. |
| 900 | } |
| 901 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 902 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 903 | continue; |
| 904 | } |
| 905 | if (symbol_name == name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 906 | return symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 909 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 910 | } |
| 911 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 912 | template <typename ElfTypes> |
| 913 | typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress( |
| 914 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 915 | Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 916 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | return symbol->st_value; |
| 920 | } |
| 921 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 922 | template <typename ElfTypes> |
| 923 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section, |
| 924 | Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 925 | CHECK(!program_header_only_) << file_->GetPath(); |
| 926 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 927 | if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 928 | return nullptr; // Failure condition. |
| 929 | } |
| 930 | if (i >= string_section.sh_size) { |
| 931 | return nullptr; |
| 932 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 933 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 934 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 935 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 936 | uint8_t* strings = Begin() + string_section.sh_offset; |
| 937 | uint8_t* string = strings + i; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 938 | if (string >= End()) { |
| 939 | return nullptr; |
| 940 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 941 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 942 | } |
| 943 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 944 | template <typename ElfTypes> |
| 945 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 946 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 947 | } |
| 948 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 949 | template <typename ElfTypes> |
| 950 | typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 951 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 952 | return *(GetDynamicSectionStart() + i); |
| 953 | } |
| 954 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 955 | template <typename ElfTypes> |
| 956 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 957 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 958 | Elf_Dyn* dyn = &GetDynamic(i); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 959 | if (dyn->d_tag == type) { |
| 960 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 963 | return nullptr; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 964 | } |
| 965 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 966 | template <typename ElfTypes> |
| 967 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 968 | Elf_Dyn* dyn = FindDynamicByType(type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 969 | if (dyn == nullptr) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 970 | return 0; |
| 971 | } else { |
| 972 | return dyn->d_un.d_val; |
| 973 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 974 | } |
| 975 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 976 | template <typename ElfTypes> |
| 977 | typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 978 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 979 | return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 980 | } |
| 981 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 982 | template <typename ElfTypes> |
| 983 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 984 | 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] | 985 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 986 | return section_header.sh_size / section_header.sh_entsize; |
| 987 | } |
| 988 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 989 | template <typename ElfTypes> |
| 990 | typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 991 | 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] | 992 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 993 | return *(GetRelSectionStart(section_header) + i); |
| 994 | } |
| 995 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 996 | template <typename ElfTypes> |
| 997 | typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 998 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 999 | return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1002 | template <typename ElfTypes> |
| 1003 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1004 | 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] | 1005 | return section_header.sh_size / section_header.sh_entsize; |
| 1006 | } |
| 1007 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1008 | template <typename ElfTypes> |
| 1009 | typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1010 | 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] | 1011 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 1012 | return *(GetRelaSectionStart(section_header) + i); |
| 1013 | } |
| 1014 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1015 | // Base on bionic phdr_table_get_load_size |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1016 | template <typename ElfTypes> |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1017 | bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const { |
| 1018 | Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1); |
| 1019 | Elf_Addr max_vaddr = 0u; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1020 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1021 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1022 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1025 | Elf_Addr begin_vaddr = program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1026 | if (begin_vaddr < min_vaddr) { |
| 1027 | min_vaddr = begin_vaddr; |
| 1028 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1029 | Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz; |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1030 | if (UNLIKELY(begin_vaddr > end_vaddr)) { |
| 1031 | std::ostringstream oss; |
| 1032 | oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex |
| 1033 | << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr |
| 1034 | << " in ELF file \"" << file_->GetPath() << "\""; |
| 1035 | *error_msg = oss.str(); |
| 1036 | *size = static_cast<size_t>(-1); |
| 1037 | return false; |
| 1038 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1039 | if (end_vaddr > max_vaddr) { |
| 1040 | max_vaddr = end_vaddr; |
| 1041 | } |
| 1042 | } |
| 1043 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 1044 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 1045 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1046 | Elf_Addr loaded_size = max_vaddr - min_vaddr; |
| 1047 | // Check that the loaded_size fits in size_t. |
| 1048 | if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) { |
| 1049 | std::ostringstream oss; |
| 1050 | oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x" |
| 1051 | << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_->GetPath() << "\""; |
| 1052 | *error_msg = oss.str(); |
| 1053 | *size = static_cast<size_t>(-1); |
| 1054 | return false; |
| 1055 | } |
| 1056 | *size = loaded_size; |
| 1057 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1060 | template <typename ElfTypes> |
| 1061 | bool ElfFileImpl<ElfTypes>::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1062 | CHECK(program_header_only_) << file_->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (executable) { |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 1065 | InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1066 | if (elf_ISA != kRuntimeISA) { |
| 1067 | std::ostringstream oss; |
| 1068 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 1069 | *error_msg = oss.str(); |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1074 | bool reserved = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1075 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1076 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1077 | if (program_header == nullptr) { |
| 1078 | *error_msg = StringPrintf("No program header for entry %d in ELF file %s.", |
| 1079 | i, file_->GetPath().c_str()); |
| 1080 | return false; |
| 1081 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1082 | |
| 1083 | // Record .dynamic header information for later use |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1084 | if (program_header->p_type == PT_DYNAMIC) { |
| 1085 | dynamic_program_header_ = program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1086 | continue; |
| 1087 | } |
| 1088 | |
| 1089 | // Not something to load, move on. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1090 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | // Found something to load. |
| 1095 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1096 | // Before load the actual segments, reserve a contiguous chunk |
| 1097 | // of required size and address for all segments, but with no |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1098 | // permissions. We'll then carve that up with the proper |
| 1099 | // permissions as we load the actual segments. If p_vaddr is |
| 1100 | // non-zero, the segments require the specific address specified, |
| 1101 | // which either was specified in the file because we already set |
| 1102 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1103 | int64_t temp_file_length = file_->GetLength(); |
| 1104 | if (temp_file_length < 0) { |
| 1105 | errno = -temp_file_length; |
| 1106 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 1107 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 1108 | return false; |
| 1109 | } |
| 1110 | size_t file_length = static_cast<size_t>(temp_file_length); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1111 | if (!reserved) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1112 | uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr); |
| 1113 | uint8_t* reserve_base_override = reserve_base; |
| 1114 | // Override the base (e.g. when compiling with --compile-pic) |
| 1115 | if (requested_base_ != nullptr) { |
| 1116 | reserve_base_override = requested_base_; |
| 1117 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1118 | std::string reservation_name("ElfFile reservation for "); |
| 1119 | reservation_name += file_->GetPath(); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1120 | size_t loaded_size; |
| 1121 | if (!GetLoadedSize(&loaded_size, error_msg)) { |
| 1122 | DCHECK(!error_msg->empty()); |
| 1123 | return false; |
| 1124 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1125 | std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1126 | reserve_base_override, |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1127 | loaded_size, PROT_NONE, false, false, |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1128 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1129 | if (reserve.get() == nullptr) { |
| 1130 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 1131 | reservation_name.c_str(), error_msg->c_str()); |
| 1132 | return false; |
| 1133 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1134 | reserved = true; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1135 | |
| 1136 | // Base address is the difference of actual mapped location and the p_vaddr |
| 1137 | base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin()) |
| 1138 | - reinterpret_cast<uintptr_t>(reserve_base)); |
| 1139 | // By adding the p_vaddr of a section/symbol to base_address_ we will always get the |
| 1140 | // dynamic memory address of where that object is actually mapped |
| 1141 | // |
| 1142 | // TODO: base_address_ needs to be calculated in ::Open, otherwise |
| 1143 | // FindDynamicSymbolAddress returns the wrong values until Load is called. |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1144 | segments_.push_back(reserve.release()); |
| 1145 | } |
| 1146 | // empty segment, nothing to map |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1147 | if (program_header->p_memsz == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1148 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1149 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1150 | uint8_t* p_vaddr = base_address_ + program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1151 | int prot = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1152 | if (executable && ((program_header->p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1153 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1154 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1155 | if ((program_header->p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1156 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1157 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1158 | if ((program_header->p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1159 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1160 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1161 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1162 | if (writable_) { |
| 1163 | prot |= PROT_WRITE; |
| 1164 | flags |= MAP_SHARED; |
| 1165 | } else { |
| 1166 | flags |= MAP_PRIVATE; |
| 1167 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1168 | if (program_header->p_filesz > program_header->p_memsz) { |
| 1169 | *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s", |
| 1170 | static_cast<uint64_t>(program_header->p_filesz), |
| 1171 | static_cast<uint64_t>(program_header->p_memsz), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1172 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1175 | if (program_header->p_filesz < program_header->p_memsz && |
| 1176 | !IsAligned<kPageSize>(program_header->p_filesz)) { |
| 1177 | *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64 |
| 1178 | " < %" PRIu64 "): %s", |
| 1179 | static_cast<uint64_t>(program_header->p_filesz), |
| 1180 | static_cast<uint64_t>(program_header->p_memsz), |
| 1181 | file_->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1184 | if (file_length < (program_header->p_offset + program_header->p_filesz)) { |
| 1185 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
| 1186 | "%d of %" PRIu64 " bytes: '%s'", file_length, i, |
| 1187 | static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz), |
| 1188 | file_->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1191 | if (program_header->p_filesz != 0u) { |
| 1192 | std::unique_ptr<MemMap> segment( |
| 1193 | MemMap::MapFileAtAddress(p_vaddr, |
| 1194 | program_header->p_filesz, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1195 | prot, |
| 1196 | flags, |
| 1197 | file_->Fd(), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1198 | program_header->p_offset, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1199 | /*low4_gb*/false, |
| 1200 | /*reuse*/true, // implies MAP_FIXED |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1201 | file_->GetPath().c_str(), |
| 1202 | error_msg)); |
| 1203 | if (segment.get() == nullptr) { |
| 1204 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 1205 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1206 | return false; |
| 1207 | } |
| 1208 | if (segment->Begin() != p_vaddr) { |
| 1209 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 1210 | "instead mapped to %p", |
| 1211 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1212 | return false; |
| 1213 | } |
| 1214 | segments_.push_back(segment.release()); |
| 1215 | } |
| 1216 | if (program_header->p_filesz < program_header->p_memsz) { |
| 1217 | std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s", |
| 1218 | static_cast<uint64_t>(i), file_->GetPath().c_str()); |
| 1219 | std::unique_ptr<MemMap> segment( |
| 1220 | MemMap::MapAnonymous(name.c_str(), |
| 1221 | p_vaddr + program_header->p_filesz, |
| 1222 | program_header->p_memsz - program_header->p_filesz, |
| 1223 | prot, false, true /* reuse */, error_msg)); |
| 1224 | if (segment == nullptr) { |
| 1225 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s", |
| 1226 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1227 | return false; |
| 1228 | } |
| 1229 | if (segment->Begin() != p_vaddr) { |
| 1230 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s " |
| 1231 | "at expected address %p, instead mapped to %p", |
| 1232 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1233 | return false; |
| 1234 | } |
| 1235 | segments_.push_back(segment.release()); |
| 1236 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1237 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1238 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1239 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1240 | uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1241 | if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) { |
| 1242 | *error_msg = StringPrintf("dynamic section address invalid in ELF file %s", |
| 1243 | file_->GetPath().c_str()); |
| 1244 | return false; |
| 1245 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1246 | dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1247 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1248 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1249 | Elf_Dyn& elf_dyn = GetDynamic(i); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1250 | uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1251 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1252 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1253 | if (!ValidPointer(d_ptr)) { |
| 1254 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1255 | d_ptr, file_->GetPath().c_str()); |
| 1256 | return false; |
| 1257 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1258 | hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1259 | break; |
| 1260 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1261 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1262 | if (!ValidPointer(d_ptr)) { |
| 1263 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1264 | d_ptr, file_->GetPath().c_str()); |
| 1265 | return false; |
| 1266 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1267 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 1268 | break; |
| 1269 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1270 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1271 | if (!ValidPointer(d_ptr)) { |
| 1272 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1273 | d_ptr, file_->GetPath().c_str()); |
| 1274 | return false; |
| 1275 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1276 | dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1277 | break; |
| 1278 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1279 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1280 | if (GetDynamicNum() != i+1) { |
| 1281 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 1282 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 1283 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 1284 | return false; |
| 1285 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1286 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1291 | // Check for the existence of some sections. |
| 1292 | if (!CheckSectionsExist(error_msg)) { |
| 1293 | return false; |
| 1294 | } |
| 1295 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1296 | return true; |
| 1297 | } |
| 1298 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1299 | template <typename ElfTypes> |
| 1300 | bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1301 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 1302 | const MemMap* segment = segments_[i]; |
| 1303 | if (segment->Begin() <= start && start < segment->End()) { |
| 1304 | return true; |
| 1305 | } |
| 1306 | } |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1310 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1311 | template <typename ElfTypes> |
| 1312 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName( |
| 1313 | const std::string& name) const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1314 | CHECK(!program_header_only_); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1315 | Elf_Shdr* shstrtab_sec = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1316 | if (shstrtab_sec == nullptr) { |
| 1317 | return nullptr; |
| 1318 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1319 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1320 | Elf_Shdr* shdr = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1321 | if (shdr == nullptr) { |
| 1322 | return nullptr; |
| 1323 | } |
| 1324 | const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1325 | if (sec_name == nullptr) { |
| 1326 | continue; |
| 1327 | } |
| 1328 | if (name == sec_name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1329 | return shdr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1335 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1336 | bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1337 | if (base_address_delta == 0) { |
| 1338 | return true; |
| 1339 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1340 | return ApplyOatPatchesTo(".debug_frame", base_address_delta) && |
| 1341 | ApplyOatPatchesTo(".debug_info", base_address_delta) && |
| 1342 | ApplyOatPatchesTo(".debug_line", base_address_delta); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1343 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1344 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1345 | template <typename ElfTypes> |
| 1346 | bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo( |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1347 | const char* target_section_name, Elf_Addr delta) { |
| 1348 | auto target_section = FindSectionByName(target_section_name); |
| 1349 | if (target_section == nullptr) { |
| 1350 | return true; |
| 1351 | } |
| 1352 | std::string patches_name = target_section_name + std::string(".oat_patches"); |
| 1353 | auto patches_section = FindSectionByName(patches_name.c_str()); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1354 | if (patches_section == nullptr) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1355 | LOG(ERROR) << patches_name << " section not found."; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1358 | if (patches_section->sh_type != SHT_OAT_PATCH) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1359 | LOG(ERROR) << "Unexpected type of " << patches_name; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1360 | return false; |
| 1361 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1362 | ApplyOatPatches( |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1363 | Begin() + patches_section->sh_offset, |
| 1364 | Begin() + patches_section->sh_offset + patches_section->sh_size, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1365 | delta, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1366 | Begin() + target_section->sh_offset, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1367 | Begin() + target_section->sh_offset + target_section->sh_size); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1368 | return true; |
| 1369 | } |
| 1370 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1371 | // Apply LEB128 encoded patches to given section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1372 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1373 | void ElfFileImpl<ElfTypes>::ApplyOatPatches( |
| 1374 | const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta, |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1375 | uint8_t* to_patch, const uint8_t* to_patch_end) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1376 | typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress; |
| 1377 | while (patches < patches_end) { |
| 1378 | to_patch += DecodeUnsignedLeb128(&patches); |
| 1379 | DCHECK_LE(patches, patches_end) << "Unexpected end of patch list."; |
| 1380 | DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section."; |
| 1381 | *reinterpret_cast<UnalignedAddress*>(to_patch) += delta; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1382 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1383 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1384 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1385 | template <typename ElfTypes> |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1386 | bool ElfFileImpl<ElfTypes>::Strip(std::string* error_msg) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1387 | // ELF files produced by MCLinker look roughly like this |
| 1388 | // |
| 1389 | // +------------+ |
| 1390 | // | Elf_Ehdr | contains number of Elf_Shdr and offset to first |
| 1391 | // +------------+ |
| 1392 | // | Elf_Phdr | program headers |
| 1393 | // | Elf_Phdr | |
| 1394 | // | ... | |
| 1395 | // | Elf_Phdr | |
| 1396 | // +------------+ |
| 1397 | // | section | mixture of needed and unneeded sections |
| 1398 | // +------------+ |
| 1399 | // | section | |
| 1400 | // +------------+ |
| 1401 | // | ... | |
| 1402 | // +------------+ |
| 1403 | // | section | |
| 1404 | // +------------+ |
| 1405 | // | Elf_Shdr | section headers |
| 1406 | // | Elf_Shdr | |
| 1407 | // | ... | contains offset to section start |
| 1408 | // | Elf_Shdr | |
| 1409 | // +------------+ |
| 1410 | // |
| 1411 | // To strip: |
| 1412 | // - leave the Elf_Ehdr and Elf_Phdr values in place. |
| 1413 | // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep |
| 1414 | // - move the sections are keeping up to fill in gaps of sections we want to strip |
| 1415 | // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr |
| 1416 | // - truncate rest of file |
| 1417 | // |
| 1418 | |
| 1419 | std::vector<Elf_Shdr> section_headers; |
| 1420 | std::vector<Elf_Word> section_headers_original_indexes; |
| 1421 | section_headers.reserve(GetSectionHeaderNum()); |
| 1422 | |
| 1423 | |
| 1424 | Elf_Shdr* string_section = GetSectionNameStringSection(); |
| 1425 | CHECK(string_section != nullptr); |
| 1426 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1427 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1428 | CHECK(sh != nullptr); |
| 1429 | const char* name = GetString(*string_section, sh->sh_name); |
| 1430 | if (name == nullptr) { |
| 1431 | CHECK_EQ(0U, i); |
| 1432 | section_headers.push_back(*sh); |
| 1433 | section_headers_original_indexes.push_back(0); |
| 1434 | continue; |
| 1435 | } |
| 1436 | if (StartsWith(name, ".debug") |
| 1437 | || (strcmp(name, ".strtab") == 0) |
| 1438 | || (strcmp(name, ".symtab") == 0)) { |
| 1439 | continue; |
| 1440 | } |
| 1441 | section_headers.push_back(*sh); |
| 1442 | section_headers_original_indexes.push_back(i); |
| 1443 | } |
| 1444 | CHECK_NE(0U, section_headers.size()); |
| 1445 | CHECK_EQ(section_headers.size(), section_headers_original_indexes.size()); |
| 1446 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1447 | // section 0 is the null section, sections start at offset of first section |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1448 | CHECK(GetSectionHeader(1) != nullptr); |
| 1449 | Elf_Off offset = GetSectionHeader(1)->sh_offset; |
| 1450 | for (size_t i = 1; i < section_headers.size(); i++) { |
| 1451 | Elf_Shdr& new_sh = section_headers[i]; |
| 1452 | Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]); |
| 1453 | CHECK(old_sh != nullptr); |
| 1454 | CHECK_EQ(new_sh.sh_name, old_sh->sh_name); |
| 1455 | if (old_sh->sh_addralign > 1) { |
| 1456 | offset = RoundUp(offset, old_sh->sh_addralign); |
| 1457 | } |
| 1458 | if (old_sh->sh_offset == offset) { |
| 1459 | // already in place |
| 1460 | offset += old_sh->sh_size; |
| 1461 | continue; |
| 1462 | } |
| 1463 | // shift section earlier |
| 1464 | memmove(Begin() + offset, |
| 1465 | Begin() + old_sh->sh_offset, |
| 1466 | old_sh->sh_size); |
| 1467 | new_sh.sh_offset = offset; |
| 1468 | offset += old_sh->sh_size; |
| 1469 | } |
| 1470 | |
| 1471 | Elf_Off shoff = offset; |
| 1472 | size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr); |
| 1473 | memcpy(Begin() + offset, §ion_headers[0], section_headers_size_in_bytes); |
| 1474 | offset += section_headers_size_in_bytes; |
| 1475 | |
| 1476 | GetHeader().e_shnum = section_headers.size(); |
| 1477 | GetHeader().e_shoff = shoff; |
| 1478 | int result = ftruncate(file_->Fd(), offset); |
| 1479 | if (result != 0) { |
| 1480 | *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s", |
| 1481 | file_->GetPath().c_str(), strerror(errno)); |
| 1482 | return false; |
| 1483 | } |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | static const bool DEBUG_FIXUP = false; |
| 1488 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1489 | template <typename ElfTypes> |
| 1490 | bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1491 | if (!FixupDynamic(base_address)) { |
| 1492 | LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath(); |
| 1493 | return false; |
| 1494 | } |
| 1495 | if (!FixupSectionHeaders(base_address)) { |
| 1496 | LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath(); |
| 1497 | return false; |
| 1498 | } |
| 1499 | if (!FixupProgramHeaders(base_address)) { |
| 1500 | LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath(); |
| 1501 | return false; |
| 1502 | } |
| 1503 | if (!FixupSymbols(base_address, true)) { |
| 1504 | LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath(); |
| 1505 | return false; |
| 1506 | } |
| 1507 | if (!FixupSymbols(base_address, false)) { |
| 1508 | LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath(); |
| 1509 | return false; |
| 1510 | } |
| 1511 | if (!FixupRelocations(base_address)) { |
| 1512 | LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath(); |
| 1513 | return false; |
| 1514 | } |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1515 | static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision."); |
| 1516 | if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1517 | LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath(); |
| 1518 | return false; |
| 1519 | } |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1523 | template <typename ElfTypes> |
| 1524 | bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1525 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1526 | Elf_Dyn& elf_dyn = GetDynamic(i); |
| 1527 | Elf_Word d_tag = elf_dyn.d_tag; |
| 1528 | if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) { |
| 1529 | Elf_Addr d_ptr = elf_dyn.d_un.d_ptr; |
| 1530 | if (DEBUG_FIXUP) { |
| 1531 | LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1532 | GetFile().GetPath().c_str(), i, |
| 1533 | static_cast<uint64_t>(d_ptr), |
| 1534 | static_cast<uint64_t>(d_ptr + base_address)); |
| 1535 | } |
| 1536 | d_ptr += base_address; |
| 1537 | elf_dyn.d_un.d_ptr = d_ptr; |
| 1538 | } |
| 1539 | } |
| 1540 | return true; |
| 1541 | } |
| 1542 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1543 | template <typename ElfTypes> |
| 1544 | bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1545 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1546 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1547 | CHECK(sh != nullptr); |
| 1548 | // 0 implies that the section will not exist in the memory of the process |
| 1549 | if (sh->sh_addr == 0) { |
| 1550 | continue; |
| 1551 | } |
| 1552 | if (DEBUG_FIXUP) { |
| 1553 | LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1554 | GetFile().GetPath().c_str(), i, |
| 1555 | static_cast<uint64_t>(sh->sh_addr), |
| 1556 | static_cast<uint64_t>(sh->sh_addr + base_address)); |
| 1557 | } |
| 1558 | sh->sh_addr += base_address; |
| 1559 | } |
| 1560 | return true; |
| 1561 | } |
| 1562 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1563 | template <typename ElfTypes> |
| 1564 | bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1565 | // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now. |
| 1566 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1567 | Elf_Phdr* ph = GetProgramHeader(i); |
| 1568 | CHECK(ph != nullptr); |
| 1569 | CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i; |
| 1570 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 1571 | << GetFile().GetPath() << " i=" << i; |
| 1572 | if (DEBUG_FIXUP) { |
| 1573 | LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1574 | GetFile().GetPath().c_str(), i, |
| 1575 | static_cast<uint64_t>(ph->p_vaddr), |
| 1576 | static_cast<uint64_t>(ph->p_vaddr + base_address)); |
| 1577 | } |
| 1578 | ph->p_vaddr += base_address; |
| 1579 | ph->p_paddr += base_address; |
| 1580 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 1581 | << GetFile().GetPath() << " i=" << i; |
| 1582 | } |
| 1583 | return true; |
| 1584 | } |
| 1585 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1586 | template <typename ElfTypes> |
| 1587 | bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1588 | Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB; |
| 1589 | // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile |
| 1590 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
| 1591 | if (symbol_section == nullptr) { |
| 1592 | // file is missing optional .symtab |
| 1593 | CHECK(!dynamic) << GetFile().GetPath(); |
| 1594 | return true; |
| 1595 | } |
| 1596 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
| 1597 | Elf_Sym* symbol = GetSymbol(section_type, i); |
| 1598 | CHECK(symbol != nullptr); |
| 1599 | if (symbol->st_value != 0) { |
| 1600 | if (DEBUG_FIXUP) { |
| 1601 | LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1602 | GetFile().GetPath().c_str(), i, |
| 1603 | static_cast<uint64_t>(symbol->st_value), |
| 1604 | static_cast<uint64_t>(symbol->st_value + base_address)); |
| 1605 | } |
| 1606 | symbol->st_value += base_address; |
| 1607 | } |
| 1608 | } |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1612 | template <typename ElfTypes> |
| 1613 | bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1614 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1615 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1616 | CHECK(sh != nullptr); |
| 1617 | if (sh->sh_type == SHT_REL) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1618 | for (uint32_t j = 0; j < GetRelNum(*sh); j++) { |
| 1619 | Elf_Rel& rel = GetRel(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1620 | if (DEBUG_FIXUP) { |
| 1621 | LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1622 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1623 | static_cast<uint64_t>(rel.r_offset), |
| 1624 | static_cast<uint64_t>(rel.r_offset + base_address)); |
| 1625 | } |
| 1626 | rel.r_offset += base_address; |
| 1627 | } |
| 1628 | } else if (sh->sh_type == SHT_RELA) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1629 | for (uint32_t j = 0; j < GetRelaNum(*sh); j++) { |
| 1630 | Elf_Rela& rela = GetRela(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1631 | if (DEBUG_FIXUP) { |
| 1632 | LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1633 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1634 | static_cast<uint64_t>(rela.r_offset), |
| 1635 | static_cast<uint64_t>(rela.r_offset + base_address)); |
| 1636 | } |
| 1637 | rela.r_offset += base_address; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1645 | template class ElfFileImpl<ElfTypes32>; |
| 1646 | template class ElfFileImpl<ElfTypes64>; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1647 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1648 | ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1651 | ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | ElfFile::~ElfFile() { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1655 | // Should never have 32 and 64-bit impls. |
| 1656 | CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1659 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg, |
| 1660 | uint8_t* requested_base) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1661 | if (file->GetLength() < EI_NIDENT) { |
| 1662 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1663 | file->GetPath().c_str()); |
| 1664 | return nullptr; |
| 1665 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1666 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, |
| 1667 | PROT_READ, |
| 1668 | MAP_PRIVATE, |
| 1669 | file->Fd(), |
| 1670 | 0, |
| 1671 | /*low4_gb*/false, |
| 1672 | file->GetPath().c_str(), |
| 1673 | error_msg)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1674 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 1675 | return nullptr; |
| 1676 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1677 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1678 | if (header[EI_CLASS] == ELFCLASS64) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1679 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only, |
| 1680 | error_msg, requested_base); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1681 | if (elf_file_impl == nullptr) |
| 1682 | return nullptr; |
| 1683 | return new ElfFile(elf_file_impl); |
| 1684 | } else if (header[EI_CLASS] == ELFCLASS32) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1685 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only, |
| 1686 | error_msg, requested_base); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1687 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1688 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1689 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1690 | return new ElfFile(elf_file_impl); |
| 1691 | } else { |
| 1692 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1693 | ELFCLASS32, ELFCLASS64, |
| 1694 | file->GetPath().c_str(), |
| 1695 | header[EI_CLASS]); |
| 1696 | return nullptr; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) { |
| 1701 | if (file->GetLength() < EI_NIDENT) { |
| 1702 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1703 | file->GetPath().c_str()); |
| 1704 | return nullptr; |
| 1705 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1706 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, |
| 1707 | PROT_READ, |
| 1708 | MAP_PRIVATE, |
| 1709 | file->Fd(), |
| 1710 | 0, |
| 1711 | /*low4_gb*/false, |
| 1712 | file->GetPath().c_str(), |
| 1713 | error_msg)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1714 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 1715 | return nullptr; |
| 1716 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1717 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1718 | if (header[EI_CLASS] == ELFCLASS64) { |
| 1719 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1720 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1721 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1722 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1723 | return new ElfFile(elf_file_impl); |
| 1724 | } else if (header[EI_CLASS] == ELFCLASS32) { |
| 1725 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1726 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1727 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1728 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1729 | return new ElfFile(elf_file_impl); |
| 1730 | } else { |
| 1731 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1732 | ELFCLASS32, ELFCLASS64, |
| 1733 | file->GetPath().c_str(), |
| 1734 | header[EI_CLASS]); |
| 1735 | return nullptr; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | #define DELEGATE_TO_IMPL(func, ...) \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1740 | if (elf64_.get() != nullptr) { \ |
| 1741 | return elf64_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1742 | } else { \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1743 | DCHECK(elf32_.get() != nullptr); \ |
| 1744 | return elf32_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
| 1748 | DELEGATE_TO_IMPL(Load, executable, error_msg); |
| 1749 | } |
| 1750 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1751 | const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1752 | DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name); |
| 1753 | } |
| 1754 | |
| 1755 | size_t ElfFile::Size() const { |
| 1756 | DELEGATE_TO_IMPL(Size); |
| 1757 | } |
| 1758 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1759 | uint8_t* ElfFile::Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1760 | DELEGATE_TO_IMPL(Begin); |
| 1761 | } |
| 1762 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1763 | uint8_t* ElfFile::End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1764 | DELEGATE_TO_IMPL(End); |
| 1765 | } |
| 1766 | |
| 1767 | const File& ElfFile::GetFile() const { |
| 1768 | DELEGATE_TO_IMPL(GetFile); |
| 1769 | } |
| 1770 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1771 | bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, |
| 1772 | uint64_t* size) const { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1773 | if (elf32_.get() == nullptr) { |
| 1774 | CHECK(elf64_.get() != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1775 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1776 | Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name); |
| 1777 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1778 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1779 | } |
| 1780 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1781 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1782 | } |
| 1783 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1784 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1785 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1786 | return true; |
| 1787 | } else { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1788 | Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name); |
| 1789 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1790 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1791 | } |
| 1792 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1793 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1794 | } |
| 1795 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1796 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1797 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1798 | return true; |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | uint64_t ElfFile::FindSymbolAddress(unsigned section_type, |
| 1803 | const std::string& symbol_name, |
| 1804 | bool build_map) { |
| 1805 | DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map); |
| 1806 | } |
| 1807 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1808 | bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const { |
| 1809 | DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | bool ElfFile::Strip(File* file, std::string* error_msg) { |
| 1813 | std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg)); |
| 1814 | if (elf_file.get() == nullptr) { |
| 1815 | return false; |
| 1816 | } |
| 1817 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1818 | if (elf_file->elf64_.get() != nullptr) |
| 1819 | return elf_file->elf64_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1820 | else |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1821 | return elf_file->elf32_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1824 | bool ElfFile::Fixup(uint64_t base_address) { |
| 1825 | if (elf64_.get() != nullptr) { |
| 1826 | return elf64_->Fixup(static_cast<Elf64_Addr>(base_address)); |
| 1827 | } else { |
| 1828 | DCHECK(elf32_.get() != nullptr); |
| 1829 | CHECK(IsUint<32>(base_address)) << std::hex << base_address; |
| 1830 | return elf32_->Fixup(static_cast<Elf32_Addr>(base_address)); |
| 1831 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1832 | DELEGATE_TO_IMPL(Fixup, base_address); |
| 1833 | } |
| 1834 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1835 | } // namespace art |