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